S Simon Kinsey v2 Member Licensed User Longtime User Feb 2, 2015 #1 I seem to be having problems with this easter egg game, that I think is due to an array of egg objects getting corrupted. The game works fine, until the values in the array are reset to their original values with the reset button. The code gives an error message when array item (0,0) changes from its initial value, set up in a nested loop. The error occurs in the Egg_stack module. The code is modified from a prototype I wrote and tested in C, so I know that this part of the program works OK when compiled in C. I have attached the source code. Attachments easter game.zip easter game.zip 5.9 KB · Views: 677 Easter files.zip Easter files.zip 222.3 KB · Views: 514
I seem to be having problems with this easter egg game, that I think is due to an array of egg objects getting corrupted. The game works fine, until the values in the array are reset to their original values with the reset button. The code gives an error message when array item (0,0) changes from its initial value, set up in a nested loop. The error occurs in the Egg_stack module. The code is modified from a prototype I wrote and tested in C, so I know that this part of the program works OK when compiled in C. I have attached the source code.
Erel B4X founder Staff member Licensed User Longtime User Feb 3, 2015 #2 Please post the error message. I don't know whether it is related or not, but please read the "common mistake" explained here: https://www.b4x.com/android/forum/threads/variables-objects-in-basic4android.8385/#content
Please post the error message. I don't know whether it is related or not, but please read the "common mistake" explained here: https://www.b4x.com/android/forum/threads/variables-objects-in-basic4android.8385/#content
S Simon Kinsey v2 Member Licensed User Longtime User Feb 3, 2015 #3 No, this isn't a problem of 'aliasing', where a variable refers to an object rather than being an object. The only code where the array of objects changes value is here: Code: For n=0 To colourCount - 1 For m=0 To EGGSEND Dim c, t As Int t = {calculated value} c = {calculated value} stack(n,m).Initialize(t, c) next next where {calculated value} is a function that calcultes the value from a string, not relevant to this problem. Stepping through the code, initially, stack(0,0) has the correct value of (0,0). However, sometimes, when n=1 and m=0, stack(0,0) returns to its previous value of (1,2). This is not a consistent problem. At other times I have run the code and stack(0,0) keeps its value. It depends on whether the player of the game got very far in the game before restarting it.
No, this isn't a problem of 'aliasing', where a variable refers to an object rather than being an object. The only code where the array of objects changes value is here: Code: For n=0 To colourCount - 1 For m=0 To EGGSEND Dim c, t As Int t = {calculated value} c = {calculated value} stack(n,m).Initialize(t, c) next next where {calculated value} is a function that calcultes the value from a string, not relevant to this problem. Stepping through the code, initially, stack(0,0) has the correct value of (0,0). However, sometimes, when n=1 and m=0, stack(0,0) returns to its previous value of (1,2). This is not a consistent problem. At other times I have run the code and stack(0,0) keeps its value. It depends on whether the player of the game got very far in the game before restarting it.
S Simon Kinsey v2 Member Licensed User Longtime User Feb 3, 2015 #4 However, the following does work: Code: For n=0 To colourCount - 1 For m=0 To EGGSEND Dim c, t As Int Dim e as Egg t = {calculated value} c = {calculated value} e.Initialize(t,c) stack(n,m) = e next next so it seems that when an object is initialized, the previous values are not properly destroyed beforehand. Is this right?
However, the following does work: Code: For n=0 To colourCount - 1 For m=0 To EGGSEND Dim c, t As Int Dim e as Egg t = {calculated value} c = {calculated value} e.Initialize(t,c) stack(n,m) = e next next so it seems that when an object is initialized, the previous values are not properly destroyed beforehand. Is this right?
Erel B4X founder Staff member Licensed User Longtime User Feb 3, 2015 #5 This is actually the same issue as the one I was referring. Only when you call Dim e As Egg you are creating a new instance.
This is actually the same issue as the one I was referring. Only when you call Dim e As Egg you are creating a new instance.
S Simon Kinsey v2 Member Licensed User Longtime User Feb 3, 2015 #6 So you cannot re-initialise the same instance to new values? OK, thank you Erel.
Erel B4X founder Staff member Licensed User Longtime User Feb 3, 2015 #7 You can. But you aren't creating a new instance so all the references still point to the same single object.
You can. But you aren't creating a new instance so all the references still point to the same single object.
S Simon Kinsey v2 Member Licensed User Longtime User Feb 3, 2015 #8 Yes, Erel, I see you are right. The flaw in my code is that in C I was able to move data structures - eggs - where in Java and B4J I move references to objects. When the game restarts I only re-instantiate some of the array elements, and these are now pointing to different objects. There is no bug.
Yes, Erel, I see you are right. The flaw in my code is that in C I was able to move data structures - eggs - where in Java and B4J I move references to objects. When the game restarts I only re-instantiate some of the array elements, and these are now pointing to different objects. There is no bug.