Hi there!
I’m currently using a function that requires a listener. Within the listener, I want to return 2 values in order to keep going on with my code. I think I’ve pinpointed my issue but I have no idea on how to resolve it.
Here are 2 exemples. The 1st one is working as expected, the 2nd doesn’t.
-- Exemple A:
local function returnFunctionA()
local returnValue = true
return returnValue
end
-- returnFunctionA() is immediately called
local returnValue = returnFunctionA()
-- returnFunctionA() returns a value that can be instantely checked
if returnValue == true then
-- Keep doing stuff
print("Keep doing stuff")
end
-- Exemple B:
local function returnFunctionB()
local returnValue = true
return returnValue
end
-- returnFunctionB() is called after 10ms
local returnValueB = timer.performWithDelay(10, returnFunctionB)
-- returnFunctionB() can't return a value
if returnValueB == true then
-- Keep doing stuff
print("Should keep doing stuff, but doesn't work")
end
So, from what I can understand: once the timers is triggered, the callback function is properly called (returnFunctionB()). But then, it returns a value (returnValue). Since this function was called as a callback function, I have no way to declare the two variables that should receive those returned value.
Any clue on how I could do this?
I could use some “global” variables instead of using returned values, and use a timer to regularely check them but I don’t think it’s the cleanest approach I could use.
Also, I’ve heard about coroutones but I have never tried using them. Would it be helpful in my case?
Any clue?