Legacy GM If statement runs twice regardless of logic

R

Rodimus

Guest
Hello!

I want to try out something, but not working:

i made a ds_list called _stations
I made a variable containing the next point id _nextstation

I wrote the next, but the code behaves on a strange way: itruns twice, and the variable _nextstation increment two times but the statement after the first run runs again. Is anybody has the same experience?

Collision with the oStop instance:

if (point_distance(x,y,other.x,other.y) < 1)
{
if (other != _stations[|_nextstation])
{
_nextstation++;
}
}
 
How do you know it runs twice? Did you use the debugger and step through it to confirm it's being ran twice? Is it possible you have two instances of the same object when you only expect one?
 
R

Rodimus

Guest
How do you know it runs twice? Did you use the debugger and step through it to confirm it's being ran twice? Is it possible you have two instances of the same object when you only expect one?
I added show_debug_message() and it shows the followings: the if case rus first, and the value increased by one. Next time, the if case somnehow can runs again, but the values nut equal, and the third time it stops, because the if statement not working.
 
R

Rodimus

Guest
Not working.
-I doublechecked everything.
- For example, if i put _currentstop = -1 inside the if statement, the statement runs once
- the ds_list has the values once
- the ds list has the correct values
- i put the values manually inside the ds_list_at the create event, but the same happening

if (_currentstop == other.id)
{
show_debug_message("reached");

if (_nextstation < ds_list_size(_stations))
{
_nextstation++;
_currentstop = _stations[|_nextstation];
}
else
{
_nextstation = 0;
}
}
 
D

drowned

Guest
I'm a little unclear about why we wouldn't expect this to run multiple times:
Code:
if (point_distance(x,y,other.x,other.y) < 1)
I don't see any indication that you're moving the player instance out of range.
 
R

Rodimus

Guest
Okay something really went strange becuse when i add the stations to the list in-game, it adds the first twice;
 
Last edited by a moderator:
R

Rodimus

Guest
I'm a little unclear about why we wouldn't expect this to run multiple times:
Code:
if (point_distance(x,y,other.x,other.y) < 1)
I don't see any indication that you're moving the player instance out of range.
the if statement runs if the distance is smaller than 1. the problem is clearly not there.
 
R

Rodimus

Guest
The cause of the double increment is not the failure of the if statement, but something went wrong when i add the stations to the list. I don't know why, but the fie second entry is alvays equals with the first, but the others are fine.
 
R

Rodimus

Guest
This piece fo code fails:

_stop = instance_place(mouse_x,mouse_y,oStop);

if (_stop != noone)
{
//_stop <- this is wrong id
ds_list_add(_parent._parent._parent._stations,_stop.id);
}

I don't know why, but i put the ds_list_add code inside a left button press event at the station object, and now the list gets the ids correctly.
If anyone knows why this code fails, please tell me!

Thanks!
 
D

drowned

Guest
the if statement runs if the distance is smaller than 1. the problem is clearly not there.
There is nothing clear about it. If the distance is smaller than 1 in two steps, the block will run twice. Nothing in your examples indicate that this would not be the case.
 
I was just reading in another thread, that if you have a collision event in an object with itself the collision event will be called twice.

For example, in the oStop object, if you have a collision event with oStop (collision with other objects of the same type), the event is triggered twice.

See :https://forum.yoyogames.com/index.p...om-an-experienced-hobbyist.44406/#post-275115

I'm not 100% sure, but could this be what is causing your issue of the if statement running twice?
 
R

Rodimus

Guest
There is nothing clear about it. If the distance is smaller than 1 in two steps, the block will run twice. Nothing in your examples indicate that this would not be the case.
But doesn't matter how close is the two object, the second if must run only once. but the problem was at different place, somehow the instance_place(mouse_x,mouse_y,oStop) part messes up, because if i clicked on the instance it gets de id correctly, but when I click on the second instance, it takes the first id again, but when I click on the third, fourth etc. it work perfectly.
 
R

Rodimus

Guest
I was just reading in another thread, that if you have a collision event in an object with itself the collision event will be called twice.

For example, in the oStop object, if you have a collision event with oStop (collision with other objects of the same type), the event is triggered twice.

See :https://forum.yoyogames.com/index.p...om-an-experienced-hobbyist.44406/#post-275115

I'm not 100% sure, but could this be what is causing your issue of the if statement running twice?
No, the stop events not collise with each other. The problematic part was with instance_place(mouse_x,mouse_y,oStop); I now placed the click part into the oStop object, and works perfectly.
 
D

drowned

Guest
Nah, collision happens every step. If it works as such, then something else is correcting the mistake in your example.
 
R

Rodimus

Guest
Nah, collision happens every step. If it works as such, then something else is correcting the mistake in your example.
INSIDE the point_distance if statement is this part:
if (_nextstation < ds_list_size(_stations))
{
_nextstation++;
_currentstop = _stations[|_nextstation];
}
else
{
_nextstation = 0;
}

this is what runs twice, regardless the _nextstation value. The reason is the above mentioned instance_place(mouse_x,mouse_y,oStop); part, because when i click and run that part, the game add the first value at the second time to the _stations list. So it is not corrected by something else, the problem was elsewhere. Doesn't matter, how mani times the point distance part runs, but the if statement inside runs once.
 
R

Rydon_Star

Guest
I had run into a similar problem with my code, where upon going through the debugging it was running an If statement twice. I fixed it with the below change:

Here is the old code:
with(cameraObj)
{
if(point_distance(cameraObj.x, cameraObj.y, marker4Obj.x, marker4Obj.y) > 2)
move_towards_point(marker4Obj.x, marker4Obj.y, 2);
else
{
playerObj.cutScene += 1;
x = marker4Obj.x;
y = marker4Obj.y;
speed = 0;
}
}

Here is the new code or Fix:
if(point_distance(cameraObj.x, cameraObj.y, marker4Obj.x, marker4Obj.y) > 2)
{
with(cameraObj)
move_towards_point(marker4Obj.x, marker4Obj.y, 2);
}
else
{
playerObj.cutScene += 1;
cameraObj.x = marker4Obj.x;
cameraObj.y = marker4Obj.y;
cameraObj.speed = 0;
}

Basically I had the If statement wrapped within the Camera Object and it was running through twice on every step, which was really odd. Once I moved the Camera Object into the If statement then it ran through once. I only had 1 instance of the Camera and 1 instance of this object with the code in it (CutScene Object). I may be coding this stuff horribly, I am not really sure, still pretty new to Gamemaker, but I wanted to put this here in case it helped anyone else.

Thanks!
 
R

Rodimus

Guest
I had run into a similar problem with my code, where upon going through the debugging it was running an If statement twice. I fixed it with the below change:

Here is the old code:
with(cameraObj)
{
if(point_distance(cameraObj.x, cameraObj.y, marker4Obj.x, marker4Obj.y) > 2)
move_towards_point(marker4Obj.x, marker4Obj.y, 2);
else
{
playerObj.cutScene += 1;
x = marker4Obj.x;
y = marker4Obj.y;
speed = 0;
}
}

Here is the new code or Fix:
if(point_distance(cameraObj.x, cameraObj.y, marker4Obj.x, marker4Obj.y) > 2)
{
with(cameraObj)
move_towards_point(marker4Obj.x, marker4Obj.y, 2);
}
else
{
playerObj.cutScene += 1;
cameraObj.x = marker4Obj.x;
cameraObj.y = marker4Obj.y;
cameraObj.speed = 0;
}

Basically I had the If statement wrapped within the Camera Object and it was running through twice on every step, which was really odd. Once I moved the Camera Object into the If statement then it ran through once. I only had 1 instance of the Camera and 1 instance of this object with the code in it (CutScene Object). I may be coding this stuff horribly, I am not really sure, still pretty new to Gamemaker, but I wanted to put this here in case it helped anyone else.

Thanks!
Thanks but as I wrote the problem was something else. Since that I moved to libGDX, but so may things I learned during the use of GMS comes handy :)
 
Top