English Limits in Lua tables? Need solution.

4 replies
Goto Page
To the start Previous 1 Next To the start
Up
Gaiyos
Security Supporter
Offline Off
Hi, I need to send table_index to local function, but I can't add any elements with
table.insert
. I need something like this:
Code:
1
2
3
4
5
6
7
8
function xd(t)
 t = {4}
end


tab = {}
xd(tab)
print(#tab)


This function should add new element to table but it doesn't (no errors). There should be 1 element in table but it's still 0. I can't use
table.insert
because won't be good solution for me. Any ideas?
IMG:http://i.imgur.com/YFN2ICK.png
IMG:http://i.imgur.com/TCHCwEk.png
IMG:http://i.imgur.com/NKscL7A.png
18.04.17 02:58:14 pm
Up
Yates
Reviewer
Offline Off
What you're trying to do cannot be done like that directly, but you can use a trick by calling the function and passing the variable name.

Code:
1
2
3
4
5
6
7
8
function test(y)
    _G[y] = 5
end

x = 0
print(x) -- prints 0
test("x")
print(x) -- prints 5


There is a way to get the original variable name passed to a function from within test, but you'd have to use debug in Lua and that is not recommended.

I don't know exactly what you're trying, but maybe you can use a return instead?

http://stackoverflow.com/questions/40933174/
18.04.17 05:26:02 pm
Up
Starkkz
Moderator
Offline Off
@user Yates: He's trying to modify the table as if it was a var pointer object. Lua doesn't support this feature though, you would have to modify your object via
rawset()
.

Either way clean it and then do the setup/copy.
lol
18.04.17 05:37:08 pm
Up
Yates
Reviewer
Offline Off
@user Starkkz: What I meant when I asked what he's exactly trying is the reason behind trying to do this code-wise. If I didn't know what he was actually trying I couldn't recommend him alternative solutions
18.04.17 09:19:03 pm
Up
Gaiyos
Security Supporter
Offline Off
@user Starkkz: Oh, that's what I need.

Thanks you guys for willingness to help!

More >
IMG:http://i.imgur.com/YFN2ICK.png
IMG:http://i.imgur.com/TCHCwEk.png
IMG:http://i.imgur.com/NKscL7A.png
To the start Previous 1 Next To the start