how to assign lua variable by reference

后端 未结 2 1500
花落未央
花落未央 2020-12-19 09:29

How can I assign a variable by reference in Lua to another one?

For example: want to do equivalent of \"a = b\" where a will then be a pointer to b

Backgr

2条回答
  •  萌比男神i
    2020-12-19 09:47

    It's true the variables are just names for values. But you can compute variable names, if you are dealing with keys in a table. This means you can use key names to perform your conditional logic.

    myvars = { a=1, b=2, c=3.14 }
    
    function choose(input)
        print(myvars[input])
    end
    
    choose('a')
    choose('b')
    a = 'a'
    choose(a)
    b = a
    choose(b)
    a = 'c'
    choose(a)
    choose(b)
    

    Output:

    1
    2
    1
    1
    3.14
    1
    

提交回复
热议问题