I need help with something.
Ive been wondering this for quite some time now, and now i need it so a quick response would be quite helpful. How do you return for the function behind the function? I dont really know how to explain it, just look this example.
function func1()
print 'Hello'
local function func2()
local something = "hello"
warn "Gday'"
return something
end
end
How do i make func1, return func2 when func2 returns something? Basically, stop the whole function and return something when func2 returns.
You can store a variable of containing func2
and then after func2
returned something, you tell func1
to return that variable, that is the variable containing func2
.
You can return a function call which will resolve the entire chain, but I’m curious as to why you have a function inside of a function for returning. Anyways, return func2()
inside of func1 should work:
1 Like
I did think of that but theres an if statement and it doesnt always return. Anything for that?
I’ll need an example of what you mean, by default a function will just return nil if it has no return.
function func1()
print 'Hello'
local insertvaluethatisdependantonwhatauserdoes
local function func2()
warn "Gday'"
if something then
return somethinginsertvaluethatisdependantonwhatauserdoes
else
print 'hi"
end
end
end
Why are the functions inside of each other? Why do you need them both to be a function? What is your actual intent, since I can’t seem to make sense of the question?
Hmm i accidentally looked over that reply… ill attempt it
Ill just do the other thing first
You still could return func2()
, but if it goes to the else
, the returned value will just be nil
. If you do return func2, the call to func2 is synchronous anyways, so if you return func2()
from inside the call to func1()
, you can be sure func2
ran completely, even if func1
’s return value is nil
unless there are coroutines or any other type of asynchronous call.
Old misinterpreted reply
This looks like you want to create a function and return it from another function, so it would be something like a function factory.
This:
local function sayHello()
print("hello")
end
Is a fancier way of saying this:
local sayHello = function()
print("hello")
end
So just like how you can return any other variable:
local function giveHello()
local result = "hello"
return result
end
giveHello() -- returns a string
You can return a function:
local function giveGreeter()
local result = function()
print("hello")
end
return result
end
And you can do so in the way you’re used to too:
local function giveGreeter()
local function result()
print("hello")
end
return result
end
-- usage
local greet = giveGreeter()
greet()
Hopefully that helps!
Edit: Wait a second, OP didn’t ask for this!
I feel like the best way to do it is to have a repeat loop in the second function that waits for something
to be the right value:
local function func1()
local function func2()
repeat
something = math.random()
until something > 0.5
return something
end
return func2()
end
I feel like what @ItzMeZeus_IGotHacked said helped and fixed it, but thank you all for your help!
1 Like