Page 2 of 2

Re: Same randomness between instances of the same class

Posted: Fri Mar 02, 2018 10:58 pm
by pgimeno
You declared local RandomEntity = {}. That's one single table. Every time you call RandomEntity:new(), the value of self will always be that table, unless you reassign it again as I did.

I found the problem by printing something within the timer. The timer was only executed once, even if I expected it to be executed in pairs, and that hinted to the problem. Maybe you can use a similar technique. Print self inside the timer or at the points where you get the same random numbers, and see how many times they are executed. I no longer think this is a problem with the random generators.

Re: Same randomness between instances of the same class

Posted: Tue Mar 06, 2018 9:47 pm
by scissors61
zorg wrote: Fri Mar 02, 2018 10:57 pm The colon syntax implicitly adds a first parameter called self to the function definition in question, and when you call such a function, if that happens with the colon syntax as well, (iirc) the last table that contains the function itself will be passed into it as "self";
Thanks, now I understand the use of the colon syntax better, and how it was an error in my code. I thought it was just a way of avoiding writing self, but it's more than that. While creating a function the colon syntax adds self as a parameter, but the weird thing if you use self as a table you don't have to write "self = {}" and there won't be an index table error.
pgimeno wrote: Fri Mar 02, 2018 10:58 pm Maybe you can use a similar technique. Print self inside the timer or at the points where you get the same random numbers, and see how many times they are executed. I no longer think this is a problem with the random generators.
Yes, it is not a problem of random generators anymore. But even if I solved the problem in the example, I still cannot solve it in my code. I get the same tweens even if the randomness is different between instances. The problem of showing a piece of my code is that it is a very personalized structure that might be complicated to understand, but I'm going to post it anyways if anyone wants to check it. The code for the enemy is placed in src.modules.enemy, the class is created in src.Enemy, the instance of the entity/class is created at gamestates.random_enemy_gamestate

Re: Same randomness between instances of the same class

Posted: Tue Mar 06, 2018 11:15 pm
by pgimeno
I added a print in the ball loading code, and it gets called twice. Similarly, the code to move the enemy is called twice per frame with two different tables. I don't know if that is normal, but it doesn't sound right.

It seems to me that at some point, the code is failing to distinguish the ball and the enemy, and it's treating the ball as both ball and enemy, and same for the enemy. No idea where, though.