Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Calling Function with Table?

Asked by 8 years ago

Basically what I'm trying to do is take a value from a table based on which global variables are set to true and use it to call a function. Here's what I've got so far...

CheckMode = {"Normal", "Classic"}
ModeFunctions = {Normal, Classic}
--colored not needed table

function execute(func)
    func()
end
function Normal()
    --there's stuff in here, but it's not relevant to this post.
    --I'm just showing you this exists
end
function Classic()
    --same as above
end

checked = false
foundmode = false
while true do
    if checked == false then --Have we already done this?
        for i,v in pairs(CheckMode) do
            if _G[CheckMode[i]] == true then
                currentmode = i
                checked = true
                foundmode = true
            elseif foundmode == false then
                warn ("Mode Function not found! Assuming to be Colored...")
                currentmode = 1
                checked = true
            elseif foundmode == true then
                print "Already found mode!"
            end
        end
    end
    execute(ModeFunctions[currentmode]) --Attempt to call local 'func' (a nil value)
    --execute(Normal)
    wait (.5)
end

The line that errors is execute(ModeFunctions[currentmode]). But strangely enough, brute forcing the function works fine: execute(Normal)

Anybody have the solution for this? Thank you!

0
Try moving line 2 to line 15, I think what's happening is the function does not exist when the table is created and therefore the values in the table are nil values. General_Scripter 425 — 8y
0
This was the solution, thank you! ShiningWindow 127 — 8y

2 answers

Log in to vote
2
Answered by 8 years ago

Try moving line 2 to line 15, I think what's happening is the function does not exist when the table is created and therefore the values in the table are nil values.

Ad
Log in to vote
0
Answered by 8 years ago

It was exactly as UndeniableLimited said! The table needed to moved.

Answer this question