SOLVED Instances acting differently

P

paulog

Guest
Hello! I'm making a game in GM1.4 but I'm facing some issues. I really hope somene can help me out.

In the game some objects have to jump to another position if the player clicks a button.

I made two variables, "global.rot" and "global.oldrot". They both have the value of 1 when the room starts, and cycle between 1 and 4.

When the button is clicked the value of "global.rot" is increased by 1 in the button object.

In the moving object, the step event tests if "global.rot" is different than "global.oldrot". If it is, the object jumps 100 pixels and "global.oldrot" get the same value as "global.rot".

The code is like this:

GML:
if global.rot = 1
{
if global.rot != global.oldrot
{
x+=50
global.oldrot = global.rot
}
}


if global.rot = 2
{
if global.rot != global.oldrot
{
x+=100
global.oldrot = global.rot
}
}


if global.rot = 3
{
if global.rot != global.oldrot
{
x+=150
global.oldrot = global.rot
}
}


if global.rot = 4
{
if global.rot != global.oldrot
{
x+=200
global.oldrot = global.rot
}
}
This works, but only for one instance of the object in the room. I don't understand how this can be. I tried to change the instance ordering but that does not seem to have any effect. The code only works for the first instance of the moving object that I place in the room. If I delete all the instances and place just one, it will work for this one, but if I place more, it will still only work for that first one.

I did draw both variables "global.rot" and "global.oldrot" on the screen and they are changing from 1 to 4, so that part seems to be working fine.

I'd like to know what I'm doing wrong and if there is a way to fix this, either by changing the code or by having a different approach.

Thanks in advance
 

Nidoking

Member
Once one instance has run this event, what will be the values of global.rot and global.oldrot when the next one runs the same event?
 

TailBit

Member
Yeah, the first one blocks the rest of them from running the code.. or use step end event to change the variable.

Maybe have a control object check the variable, and when it sees that it is different, then have it move all of them using with
Code:
if global.rot != global.oldrot{
    with(obj_moving) x += global.rot*50;
    global.oldrot = global.rot;
}
 
P

paulog

Guest
Yeah, the first one blocks the rest of them from running the code.. or use step end event to change the variable.

Maybe have a control object check the variable, and when it sees that it is different, then have it move all of them using with
Code:
if global.rot != global.oldrot{
    with(obj_moving) x += global.rot*50;
    global.oldrot = global.rot;
}
Having a control object worked! Thanks a lot!
 
Top