Issue with 2D Tables and Variables?
Posted: Tue Sep 13, 2016 8:48 am
Hey there, i'm kinda new when it comes to Lua as a whole and i've tried reading the documentation around this but it just goes straight through me no matter how hard I try and i've had an issue with something i've been working on that doesn't appear to make any apparent sense to me.
Basically i've got a table of tables to replicate an object/class and i've got a series of variables to initialize the object for example:
Class = {}
Object = {}
function generate_object()
object.x = 5
object.y = 3
table.insert{Class, Object}
end
So by my logic at that point (and printing to the console the contents of the class table says the same) Class[1].x = 5 and y = 3.
The problem occurs when i've tried to change those variables elsewhere, i figure it's probably something to do with scope but can't tell what or if i'm missing something fundamental but even if try to change them outside of the function in the update or something like that, it changes the variable for every object in the class rather than just the one i'm trying to change i.e.
Class[1].x = 3
print (Class[2].x)
Returns 3 when it should be 5 (assuming i've ran generate_object twice first anyway so the second object even exists)
I'd have linked my actual code up here but frankly it's become a huge mess from me trying to fix it, implement tests and changing scope left right and center so i've tried to put a condensed version here. I hope it makes sense what i'm trying to do anyway if not i'll get the code cleaned up and upload actual snippets to see if there's something i've broken elsewhere and i've not noticed it.
Apologies if this is something that's been addressed commonly elsewhere, I couldn't see it after a bit of searching so thought i'd ask!
Basically i've got a table of tables to replicate an object/class and i've got a series of variables to initialize the object for example:
Class = {}
Object = {}
function generate_object()
object.x = 5
object.y = 3
table.insert{Class, Object}
end
So by my logic at that point (and printing to the console the contents of the class table says the same) Class[1].x = 5 and y = 3.
The problem occurs when i've tried to change those variables elsewhere, i figure it's probably something to do with scope but can't tell what or if i'm missing something fundamental but even if try to change them outside of the function in the update or something like that, it changes the variable for every object in the class rather than just the one i'm trying to change i.e.
Class[1].x = 3
print (Class[2].x)
Returns 3 when it should be 5 (assuming i've ran generate_object twice first anyway so the second object even exists)
I'd have linked my actual code up here but frankly it's become a huge mess from me trying to fix it, implement tests and changing scope left right and center so i've tried to put a condensed version here. I hope it makes sense what i'm trying to do anyway if not i'll get the code cleaned up and upload actual snippets to see if there's something i've broken elsewhere and i've not noticed it.
Apologies if this is something that's been addressed commonly elsewhere, I couldn't see it after a bit of searching so thought i'd ask!