Calling A Function In An Assignment Statement

Hi, how can I get an assignment statement to return the output of a function like in the example below?

-- Clarified example
local emptyVar = nil
local varExample = if (emptyVar) then "false" else function()
	return "I'd like this to return"
end
print(varExample)

-- What I need it for, probably don't look at this tbh
local spawningData = allBulletsInCoeval[possiblyMissingLocationData]
allBulletsInCoeval[possiblyMissingLocationData] = if not spawningData then {0, 0, 0} else if (possiblyMissingLocationData == 1) then function()
	local rotationalAbode = {}
	for rotationalComponent = 1, 3 do
		table.insert(rotationalAbode, math.rad(spawningData[rotationalComponent]))
	end
	return rotationalAbode
end

I don’t think it’s possible the way your doing it, however, you could do it like this:

function rotateAbode() --Call this what you want
	local rotationalAbode = {}
	for rotationalComponent = 1, 3 do
		table.insert(rotationalAbode, math.rad(spawningData[rotationalComponent]))
	end
	return rotationalAbode
end
allBulletsInCoeval[possiblyMissingLocationData] = if not spawningData then {0, 0, 0} else if (possiblyMissingLocationData == 1) then rotateAbode()

I’ll keep looking into it though.

1 Like

You can wrap the function in parentheses then call it like so:

local s = if nil then 'a' else (function()
	return 'so cool'
end)()
print(s) --> so cool
1 Like

Actually I found out how, I’ll post code in a sec.
Edit: NVM, @7z99 found what I was about to say just before me.

1 Like

Thanks, this is what I am looking for. I tried to get

local emptyVar = nil
local varExample = if (emptyVar) then "false" else if (1 + 1 == 2) then (function()
	return "I'd like this to return"
end)()
print(varExample)

to work but it errors now

1 Like

ah nevermind I forget that it must end with else

1 Like

Not sure if it’s what you’re looking for but you can do nested if-then-else statements but they’re really ugly:

local var = if someVar then false else if 1 + 1 == 2 then (function() return "thing" end)() else false

Elseif would be a (not) much more readable option here:

local var = if someVar then false elseif 1 + 1 == 2 then (function() return "thing" end)() else false
1 Like

Oh, I have seen others use elseif before but I hadn’t thought much about it. Is elseif more efficient than else if?

1 Like

I always thought having two ends after an else if was weird but maybe it is because I am not using elseif

Not sure about performance but I do think elseif would be more readable. All depends on your style tho:

local var = if a then 
    'a'
elseif b then
    'b'
else
    'c'

is the same as doing

local var = if a then
    'a'
else
    if b then
        'b'
    else
        'c'

This is especially apparent when we need several conditions to be checked:

local a = if a then
    b
elseif c then
    d
elseif e then
    f
else
    g

is the same as doing:

local a = if a then
    b
else
    if c then
        d
    else
        if e then
            f
        else
            g

but if it comes to that, you will probably be better off making a matching function than making a bunch of if-statements for every possible option

1 Like