Same randomness between instances of the same class

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.
User avatar
pgimeno
Party member
Posts: 1143
Joined: Sun Oct 18, 2015 2:58 pm

Re: Same randomness between instances of the same class

Post by pgimeno » Fri Mar 02, 2018 10:58 pm

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.
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories
The Microsoft Github repositories I had have been closed after the acquisition and will be removed in the near future.

User avatar
scissors61
Citizen
Posts: 76
Joined: Fri Jan 08, 2016 10:16 am

Re: Same randomness between instances of the same class

Post by scissors61 » Tue Mar 06, 2018 9:47 pm

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
Attachments
personalized_unsolved.love
(1.55 MiB) Downloaded 9 times
solved_example.love
(3.16 KiB) Downloaded 7 times

User avatar
pgimeno
Party member
Posts: 1143
Joined: Sun Oct 18, 2015 2:58 pm

Re: Same randomness between instances of the same class

Post by pgimeno » Tue Mar 06, 2018 11:15 pm

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.
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories
The Microsoft Github repositories I had have been closed after the acquisition and will be removed in the near future.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests