Encountering some bugs with lists in classes

I'm encountering some bugs with lists inside classes, with a list in one instance of a class changing when a list in another instance of the class is changed. Here's an example from the console:





players[] is a list of player classes and so is parents[]. Each player class has a list conGenotype[]. When I modify conGenotype in players[1] the list also gets modified in parents[1]. They aren't the same instance, and no code is running to change either of the values (the project was paused while I was using the console), although players[1].conGenotype is set to parents[1].conGenotype at an earlier point (I think this is somehow causing the issue). Changing players[1].conGenotype does not change parents[2].conGenotype or any other values in parents[].

Also, I've found out that making a function return during a for loop causes weird outputs, even if the loop is broken after returning.

I'll be greatful for any help / suggestions on how to solve the issue or work around it. Thanks in advance!

Most likely, your two lists are the same list ; by that I mean that parents[1].conGenotype and players[1].conGenotype hold references to the very same list. You can check that by typing:

parents[1].conGenotype == players[1].conGenotype

The result should be 1 (true, same list). Now ask yourself at which point of your program you are creating a new object and wish this object would have its own, independent list ; at that point, you cannot just assign a list to a property of the object (which would just create a new reference to an existing list), you have to create a new list and duplicate the contents of the source list into the new list (a dedicated function duplicateList could be useful for doing that probably).

I hope this helps! If you need further help on this, maybe try to reduce this to a few lines of code exhibiting the problem and post that here, so that we can see how you create parents and players and their conGenotype lists.

Thanks for the help!

It does seem that the lists were the same, I'd tried parents[1].conGenotype == players[1].conGenotype before but thought it tested for if the contents of the lists were identical, not whether the lists were the same thing. I've managed to fix that with some dupe functions, but my code still appears to be broken in some unknown manner. I'll check to see if it's some missed error or found another obscure feature.

For the second glitch, this code is able to replicate it:

init = function()
  stuff = [1,2,3,4,5,6,7,8]

glitchy = function()
  for i in stuff
    if i == 5 then

It returns [0,0,0,0,5,0,0,0] instead of 5, which can be annoying since it's not immediately obvious what the problem is. This also happens if break is inserted after the return(), which should cancel the loop and cut off the returning list.

It would be nice if the need to duplicate lists rather than setting values to them was in the documentation somewhere, to help clear things up if anyone else encounters the same issue.

It returns [0,0,0,0,5,0,0,0] instead of 5

Thanks for spotting this! That is actually a microScript bug I wasn't aware of, and a pretty bad one. It is now fixed.


Post a reply



Validate your e-mail address to participate in the community