problem with player colliding with objects (for loop)

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.
Post Reply
User avatar
Lokiarti
Prole
Posts: 2
Joined: Mon Jul 01, 2019 11:31 am

problem with player colliding with objects (for loop)

Post 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!
Attachments
test0.love
(12.5 KiB) Downloaded 121 times
:huh:
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
User avatar
Lokiarti
Prole
Posts: 2
Joined: Mon Jul 01, 2019 11:31 am

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

Post 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.
:huh:
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 83 guests