1
function AIORotation:SetRotation(val)
    if val == "abcd" then
        self.db.profile.activeRotation = AIORotation.aaaa:new()
    elseif val == "dddd" then
        self.db.profile.activeRotation = AIORotation.dddd:new()
    end
end

I am passing a string into this function and want to call a function based off the string name. Is something like this possible without a bunch of if statements?

So ideally, it would be something like this

function AIORotation:SetRotation(val)
    self.db.profile.activeRotation = AIORotation.<INSERT_VAL_HERE>:new()
end

But I'm not sure if something like this is possible in lua.

1 Answer 1

3

You need to use [] syntax. [] syntax allows you to use any variable, expression (that evaluates on runtime) or constant.

function AIORotation:SetRotation(val)
    self.db.profile.activeRotation = AIORotation[val]:new()
end

.var syntax is actually identical to ["var"] (syntactic sugar).

Read move about table syntax: https://www.lua.org/pil/2.5.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.