Page 1 of 1

problem with player colliding with objects (for loop)

Posted: Thu Jul 11, 2019 12:26 am
by Lokiarti
hello, I am very new to gamedev so sorry for asking rather silly questions (sorry for my bad english as well).

so, recently I've started working on a little project and ran into a problem which I can't understand. I've created a simple "game" to recreate this thing (I'll attach it in my post). now, let me explain what I'm facing:


I have, say, two objects on the scene. they have a variable printed above both of them to show if the player collides with them (returns true or false). I also have the same-ish thing for the player (if a certain variable is true, the player will say his line). there's a "for" loop going through the objects' table, checking if the object collides with hero (I use some copypasted code for collision check).

the problem is that, when the player collides with either first or second object, this object's variable becomes true/false depending on the player's position (this part is working correctly), but the player himself only speaks when colliding with the second object. I assume that the loop somehow doesn't work with player's table variables correctly and uses only the second (I guess, the last in table) object's coordinates.

please check my code, I've ran out of ideas. thanks!

Re: problem with player colliding with objects (for loop)

Posted: Thu Jul 11, 2019 2:00 am
by pgimeno
Something very similar happened to someone very recently.

What happens is that when checking the collision with the first object, 'hero.observes' is made true, but the loop continues and when checking the collision with the second object, 'hero.observes' is overwritten and set to false.

You need to set 'hero.observes' to false before the loop, not inside.

Re: problem with player colliding with objects (for loop)

Posted: Thu Jul 11, 2019 1:49 pm
by Lokiarti
thank you for replying! I swear I did use the forum search, but I haven't found any recent thread with such question. sorry. ^^

I did what you suggested and it helped. thanks a lot.