Issue with 2D Tables and Variables?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Adrei
Prole
Posts: 2
Joined: Tue Sep 13, 2016 8:34 am

Issue with 2D Tables and Variables?

Post by Adrei »

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!
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Issue with 2D Tables and Variables?

Post by Plu »

Judging by the pieces of code you posted above, the problem might be that you don't make new objects, but constantly re-use existing ones.

This is because your generate_object() function re-uses the existing global Object variable, which will result in the Class table having a bunch of entries that all point to the same Object.

Try it like this to create a fresh object on every call:

Code: Select all

Class = {}

function generate_object()
  Object = {}
  Object.x = 5
  Object.y = 3
  table.insert(Class, Object)
end
This should resolve your issue. It is important to understand that in Lua, tables are never copied, they are always assigned by reference. So doing this:

Code: Select all

t = { "a table" }
a = t
b = t
Gives you one table, and updating any of t, a or b will also update all the others, because they are the same thing. If you want to have a different table, you must start by creating a new table using the "variable = {}" syntax and the putting whatever values you want into it.
Adrei
Prole
Posts: 2
Joined: Tue Sep 13, 2016 8:34 am

Re: Issue with 2D Tables and Variables?

Post by Adrei »

That seems like a really logical answer as to why it wasn't working this morning. For some reason in my head I assumed once an object was stuffed inside it's class it would retain everything it needed if i wanted to create a new one, it never dawned on me a new object was never actually being made.

I'll see if i can work that into my code once i've cleaned it up and hopefully it'll fix it!

Thanks a lot for that!
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Issue with 2D Tables and Variables?

Post by zorg »

Plu wrote:

Code: Select all

Class = {}

function generate_object()
  Object = {}
  Object.x = 5
  Object.y = 3
  table.insert(Class, Object)
end
One minor thing, this still barfs into the global environment; putting the local keyword before the Object = {} line will fix that, granted if you don't want a global pointer to the last created object... and if you do, it would still be wise to do it some other way.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 23 guests