GMS 2 Why do the "x" and "y" jump back?

Dwighty4000

Member
Why do the "x" and "y" jump back if the player ends to walk
I have a player object and I have a flashlight object. When the player is walking, the coordinates are taken over by the player and expanded to their "x + 100" and "y + 20".
But if the player is not walking, then it takes over the same coordinates from the player, but ignores the x + 100 and y + 20 ...
But why?
Code:
#region Gesammelt-True
if (instance_exists(obj_player_escaperun_v2))
{               
   if (obj_player_escaperun_v2.PlayerL == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {
       x = obj_player_escaperun_v2.x;
       y = obj_player_escaperun_v2.y;
       x = x - 100;
       y = y + 20;
       
       sprite_index = spr_flashlight_item_L;
   }
   if (obj_player_escaperun_v2.PlayerR == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {   
       x = obj_player_escaperun_v2.x;
       y = obj_player_escaperun_v2.y;
       x = x + 100;
       y = y + 20;
       
       sprite_index = spr_flashlight_item_R;
   }

#endregion
#region Gesammelt-FALSE

   if (global.taschenlampenaktiv == true && global.flashlightitemgesammelt == false)
   {
       x = 500;
       y = 500;
       sprite_index = spr_flashlight_item_R;
       
   }
   
}
#endregion
 

Attachments

Last edited:

samspade

Member
I can't read all the code, but on line 30 and 31 you're setting the flashlights x and y value, so if those two conditions are true, that's code is going to run and the x and y location will be set to 500 500. My guess is you just want to set it to the player's x and y instead.

Also, don't post pictures of code. It's against the forum rules and is harder to read.
 

JaimitoEs

Member
A bit example:
Code:
if (PlayerRunLeft)
{
 //do something
}
else if (PlayerRunRight)
{
 //do something
}
else
{
 ????Can the solution be here?
}

so if the player is not running that rest in the coordinates  500 and 500 relative to the world...
 
Last edited:

Dwighty4000

Member
I can't read all the code, but on line 30 and 31 you're setting the flashlights x and y value, so if those two conditions are true, that's code is going to run and the x and y location will be set to 500 500. My guess is you just want to set it to the player's x and y instead.

Also, don't post pictures of code. It's against the forum rules and is harder to read.
This is impossible because flashlightitemgesammelt is in this case always "true"
 

Dwighty4000

Member
A bit example:
Code:
if (PlayerRunLeft)
{
 //do something
}
else if (PlayerRunRight)
{
 //do something
}
else
{
 ????Can the solution be here?
}

so if the player is not running that rest in position 500 and 500 relative to the world...
As I have already said, it cannot be due to the release of the if-function as these always deliver correct values that I had displayed with a drawevent in real time.
But why does the coordinate jump from the flashlight back to the players X and Ym if there is a continuation of the code directly below it, which is only carried out when the player is running. while running or standing still, the release variables for the if-function do not change and it is inexplicable why he only half-executes the content of the if-function !?
 

JaimitoEs

Member
I recommend that you use the text panel to insert the code, so we can copy and paste it to solve it faster and write you the solution. What I read is that if the first two conditions are not met and the flashlight is off, it is positioned on the 500 500 coordinate axis in your room.
 
Last edited:

Dwighty4000

Member
I recommend that you use the text panel to insert the code, so we can copy and paste it to solve it faster and write you the solution. What I read is that if the first two conditions are not met and the flashlight is off, it is positioned on the 500 500 coordinate axis in your room.
why do you always go into the if function that is not accessed?
 

JaimitoEs

Member
I know you're using regions, but, I don't see what positions get your item in any place if the player doesn't run, I mean I tell you what I read ... Maybe in another script you are telling... x = player.x and y = player.y no?

Basically #region-#endregion is used to separate your block of code (it may be your class of function or collection of function whatever you want to specify in.)

Which helps you to identify your block of code with the Key name that explain what you have done inside that region.

It helps you to expand or collapse your code block. It simplify the way you write your code. like below:
#region "This Is My Function"
public void MyFunction()
{
//Some Code
}
#endregion

You are ending a region without closing the brakets of your first conditional...
 
Last edited:

Dwighty4000

Member
I know you're using regions, but, I don't see what positions get your item in any place if the player doesn't run, I mean I tell you what I read ... Maybe in another script you are telling... x = player.x and y = player.y no?

Basically #region-#endregion is used to separate your block of code (it may be your class of function or collection of function whatever you want to specify in.)

Which helps you to identify your block of code with the Key name that explain what you have done inside that region.

It helps you to expand or collapse your code block. It simplify the way you write your code. like below:
#region "This Is My Function"
public void MyFunction()
{
//Some Code
}
#endregion
if the player run, it will do this:
x = obj_player_escaperun_v2.x;
y = obj_player_escaperun_v2.y;
x = x - 100;
y = y + 20;
But if the player stop running, it will only do this without with the contine of the coordinates:
x = obj_player_escaperun_v2.x;
y = obj_player_escaperun_v2.y;
 

JaimitoEs

Member
Code:
#region Gesammelt-True
if (instance_exists(obj_player_escaperun_v2))
{            
   if (obj_player_escaperun_v2.PlayerL == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {
      x = obj_player_escaperun_v2.x;
      y = obj_player_escaperun_v2.y;
      x = x - 100;
      y = y + 20;
   
      sprite_index = spr_flashlight_item_L;
   }
   if (obj_player_escaperun_v2.PlayerR == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {
      x = obj_player_escaperun_v2.x;
      y = obj_player_escaperun_v2.y;
      x = x + 100;
      y = y + 20;
   
      sprite_index = spr_flashlight_item_R;
   }
}

#endregion
#region Gesammelt-FALSE

   if (global.taschenlampenaktiv == true && global.flashlightitemgesammelt == false)
   {
      x = 500;
      y = 500;
      sprite_index = spr_flashlight_item_R;
   
   }
 

#endregion

Try this...
or

Code:
#region Gesammelt-True
if (instance_exists(obj_player_escaperun_v2))
{             
   if (obj_player_escaperun_v2.PlayerL == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {
      x = obj_player_escaperun_v2.x;
      y = obj_player_escaperun_v2.y;
      x = x - 100;
      y = y + 20;
    
      sprite_index = spr_flashlight_item_L;
   }
   if (obj_player_escaperun_v2.PlayerR == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   { 
      x = obj_player_escaperun_v2.x;
      y = obj_player_escaperun_v2.y;
      x = x + 100;
      y = y + 20;
    
      sprite_index = spr_flashlight_item_R;
   }
}

#endregion
#region Gesammelt-FALSE
if (instance_exists(obj_player_escaperun_v2))
{
   if (global.taschenlampenaktiv == true && global.flashlightitemgesammelt == false)
   {
      x =   obj_player_escaperun_v2.x +500;
      y =   obj_player_escaperun_v2.y +500;
      sprite_index = spr_flashlight_item_R;
    
   }
} 

#endregion
 

samspade

Member
This is impossible because flashlightitemgesammelt is in this case always "true"
Nothing in the code you posted would require flashlightitemgesammelt to always be true. So you might be right, but if so, that code is elsewhere.

Why do you say:

But if the player stop running, it will only do this without with the contine of the coordinates:
x = obj_player_escaperun_v2.x;
y = obj_player_escaperun_v2.y;
That isn't anywhere in the code you posted.

Given that and your other comment, my guess is you have some other code that is affecting this.
 

JaimitoEs

Member
Nothing in the code you posted would require flashlightitemgesammelt to always be true. So you might be right, but if so, that code is elsewhere.

Why do you say:



That isn't anywhere in the code you posted.

Given that and your other comment, my guess is you have some other code that is affecting this.
I think is talking about an offset between the player and the lantern but i´m not sure...bad day to help, my brain is burning:D:oops:
 

Dwighty4000

Member
Code:
#region Gesammelt-True
if (instance_exists(obj_player_escaperun_v2))
{          
   if (obj_player_escaperun_v2.PlayerL == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {
      x = obj_player_escaperun_v2.x;
      y = obj_player_escaperun_v2.y;
      x = x - 100;
      y = y + 20;
 
      sprite_index = spr_flashlight_item_L;
   }
   if (obj_player_escaperun_v2.PlayerR == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {
      x = obj_player_escaperun_v2.x;
      y = obj_player_escaperun_v2.y;
      x = x + 100;
      y = y + 20;
 
      sprite_index = spr_flashlight_item_R;
   }
}

#endregion
#region Gesammelt-FALSE

   if (global.taschenlampenaktiv == true && global.flashlightitemgesammelt == false)
   {
      x = 500;
      y = 500;
      sprite_index = spr_flashlight_item_R;
 
   }
 

#endregion

Try this...
or

Code:
#region Gesammelt-True
if (instance_exists(obj_player_escaperun_v2))
{           
   if (obj_player_escaperun_v2.PlayerL == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {
      x = obj_player_escaperun_v2.x;
      y = obj_player_escaperun_v2.y;
      x = x - 100;
      y = y + 20;
  
      sprite_index = spr_flashlight_item_L;
   }
   if (obj_player_escaperun_v2.PlayerR == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {
      x = obj_player_escaperun_v2.x;
      y = obj_player_escaperun_v2.y;
      x = x + 100;
      y = y + 20;
  
      sprite_index = spr_flashlight_item_R;
   }
}

#endregion
#region Gesammelt-FALSE
if (instance_exists(obj_player_escaperun_v2))
{
   if (global.taschenlampenaktiv == true && global.flashlightitemgesammelt == false)
   {
      x =   obj_player_escaperun_v2.x +500;
      y =   obj_player_escaperun_v2.y +500;
      sprite_index = spr_flashlight_item_R;
  
   }
}

#endregion
still the same problem...
what ever you try'd in this code, the problem is not fixed and "#region Gesammelt-FALSE" is not, and was never active.
 

JaimitoEs

Member
still the same problem...
what ever you try'd in this code, the problem is not fixed and "#region Gesammelt-FALSE" is not, and was never active.
Yes but the code have not sense on sintax cause the brackets, please, read what we have posted, and then explain us why we can´t see a condition telling where it should be positioned your item if the player does not run. i´m just trying to help...
 
Last edited:

samspade

Member
Use this code. It only has a few changes. First, it turns your internal if statement into an if else. This just makes the code cleaner. Second, it moves the second if statement outside of the internal one. Third, I combined your add lines as it was unnecessary to do them in two rows. Finally, i added show debug messages. Run the code and see which debug messages show up. If it still doesn't work, your problem is elsewhere. Likely you are setting the x and y somewhere else and overwriting what is here.

Code:
#region Gesammelt-True
if (instance_exists(obj_player_escaperun_v2))
{               
   if (obj_player_escaperun_v2.PlayerL == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {
      x = obj_player_escaperun_v2.x - 100;
      y = obj_player_escaperun_v2.y + 20;
     
      sprite_index = spr_flashlight_item_L;
      show_debug_message("A"); 

   } else if (obj_player_escaperun_v2.PlayerR == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {   
      x = obj_player_escaperun_v2.x + 100;
      y = obj_player_escaperun_v2.y + 20;
     
      sprite_index = spr_flashlight_item_R;
      show_debug_message("B"); 
   }
}
#endregion

#region Gesammelt-FALSE
if (global.taschenlampenaktiv == true && global.flashlightitemgesammelt == false)
{
    show_debug_message("C"); 
    x = 500;
    y = 500;
    sprite_index = spr_flashlight_item_R;
}
#endregion
 

JaimitoEs

Member
Code:
#region Gesammelt-True
if (instance_exists(obj_player_escaperun_v2))
{      
   if (obj_player_escaperun_v2.PlayerL == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {
      x = obj_player_escaperun_v2.x - 100;
      y = obj_player_escaperun_v2.y +  20;
      sprite_index = spr_flashlight_item_L;
   }
   else  if (obj_player_escaperun_v2.PlayerR == true && global.taschenlampenaktiv == true &&global.flashlightitemgesammelt == true)
   {
      x = obj_player_escaperun_v2.x + 100;
      y = obj_player_escaperun_v2.y + 20;
     sprite_index = spr_flashlight_item_R;
   }
   else
   {
    x = obj_player_escaperun_v2.x ;
    y = obj_player_escaperun_v2.y - 200;//for example
    sprite_index = spr_flashlight_item_R;
   }
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////
#region Gesammelt-FALSE
if (global.taschenlampenaktiv == true && global.flashlightitemgesammelt == false)
 {
   x = 500;
   y = 500;
   sprite_index = spr_flashlight_item_R;
 }
#endregion
 

Dwighty4000

Member
Use this code. It only has a few changes. First, it turns your internal if statement into an if else. This just makes the code cleaner. Second, it moves the second if statement outside of the internal one. Third, I combined your add lines as it was unnecessary to do them in two rows. Finally, i added show debug messages. Run the code and see which debug messages show up. If it still doesn't work, your problem is elsewhere. Likely you are setting the x and y somewhere else and overwriting what is here.

Code:
#region Gesammelt-True
if (instance_exists(obj_player_escaperun_v2))
{              
   if (obj_player_escaperun_v2.PlayerL == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {
      x = obj_player_escaperun_v2.x - 100;
      y = obj_player_escaperun_v2.y + 20;
    
      sprite_index = spr_flashlight_item_L;
      show_debug_message("A");

   } else if (obj_player_escaperun_v2.PlayerR == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {  
      x = obj_player_escaperun_v2.x + 100;
      y = obj_player_escaperun_v2.y + 20;
    
      sprite_index = spr_flashlight_item_R;
      show_debug_message("B");
   }
}
#endregion

#region Gesammelt-FALSE
if (global.taschenlampenaktiv == true && global.flashlightitemgesammelt == false)
{
    show_debug_message("C");
    x = 500;
    y = 500;
    sprite_index = spr_flashlight_item_R;
}
#endregion
it spamms "A" if the player looks left and its spamm "B" if the player looks right: what are you expecting with this?
 

samspade

Member
it spamms "A" if the player looks left and its spamm "B" if the player looks right: what are you expecting with this?
Given what you said I thought it was the most likely. So your problem is somewhere else. You have other code affecting the x/y position. It could be inside the object, or in another object, so it is a little hard to know how to search. I'd start with the object itself, search through all events and scripts and then start looking for any objects which reference it or any parents it might have.
 

JaimitoEs

Member
As I have already said, it cannot be due to the release of the if-function as these always deliver correct values that I had displayed with a drawevent in real time.
But why does the coordinate jump from the flashlight back to the players X and Ym if there is a continuation of the code directly below it, which is only carried out when the player is running. while running or standing still, the release variables for the if-function do not change and it is inexplicable why he only half-executes the content of the if-function !?
So, are you drawing in diferent coordinates? are these calculations done in the draw event? are you drawing the light in diferent cordinates that break your calculations? we need more data, sorry!
 
Last edited:

JaimitoEs

Member
there is only a step event for this and nothing more on this object without a "depth = 17" in the create event...
You don´t tell me what you see with my last script... if you can export your project to take a look, i will do the favor to look it tomorrow, i´m going to sleep...
 

samspade

Member
there is only a step event for this and nothing more on this object without a "depth = 17" in the create event...
I feel like you're not really trying here. My version of the code, which you said you used, has only 2 pairs of x/y assignments (excluding the section that has show_debug_message("C") and is never called). Both of those two assignments combine the offset, so it would be impossible for the x/y position to the same as obj_player_escaperun_v2.

This means that there are only a few options:
  • GameMaker is magically doing something with no code responsible for it
  • There is code responsible for it and it is somewhere else
  • You haven't actually followed what we suggested or you haven't reported the results accurately
Ultimately, you're the programmer so you're going to have to figure it out. My guess is that it is the second of the three choices above. Here is my follow up question for you. What moves the light if that code doesn't? What happens if you delete all that code, will the light still move?
 

TailBit

Member
But if the player is not walking, then it takes over the same coordinates from the player, but ignores the x + 100 and y + 20 ...
does that mean it will constantly use this offset
Code:
      x = x - 100;
      y = y + 20;
if so control check the variables PlayerL and PlayerR, sounds like PlayerL turns true when you have released them both

or is it just in the middle of player, as if this code was the only thing that triggered:
Code:
       x = obj_player_escaperun_v2.x;
       y = obj_player_escaperun_v2.y;
if so then the problem isn't there, as your code is forced to get the coordinates of the player and then offset it

there isn't much to blame on that code..

but what if it is correctly offset, but it is the position of the player idle sprite that could be wrong?
 

TheouAegis

Member
Why do the "x" and "y" jump back if the player ends to walk
I have a player object and I have a flashlight object. When the player is walking, the coordinates are taken over by the player and expanded to their "x + 100" and "y + 20".
But if the player is not walking, then it takes over the same coordinates from the player, but ignores the x + 100 and y + 20 ...
But why?
Code:
#region Gesammelt-True
if (instance_exists(obj_player_escaperun_v2))
{             
   if (obj_player_escaperun_v2.PlayerL == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   {
       x = obj_player_escaperun_v2.x;
       y = obj_player_escaperun_v2.y;
       x = x - 100;
       y = y + 20;
     
       sprite_index = spr_flashlight_item_L;
   }
   if (obj_player_escaperun_v2.PlayerR == true && global.taschenlampenaktiv == true && global.flashlightitemgesammelt == true)
   { 
       x = obj_player_escaperun_v2.x;
       y = obj_player_escaperun_v2.y;
       x = x + 100;
       y = y + 20;
     
       sprite_index = spr_flashlight_item_R;
   }

#endregion
#region Gesammelt-FALSE

   if (global.taschenlampenaktiv == true && global.flashlightitemgesammelt == false)
   {
       x = 500;
       y = 500;
       sprite_index = spr_flashlight_item_R;
     
   }
 
}
#endregion
Code:
if global.taschenlampenaktiv == true {
    if global.flashlightitemgesammelt == false {
        x = 500;
        y = 500;
        sprite_index = spr_flashlight_item_R;
    }
    else {
        if instance_exists(obj_player_escaperun_v2)
        {              
            y = obj_player_escaperun_v2.y + 20;
            if obj_player_escaperun_v2.PlayerL == true
            {
                x = obj_player_escaperun_v2.x - 100;
                sprite_index = spr_flashlight_item_L;
            }
            else if obj_player_escaperun_v2.PlayerR == true
            {  
                x = obj_player_escaperun_v2.x + 100;
                sprite_index = spr_flashlight_item_R;
            }
        }
    }
}
I took out the regions because they're just encouraging repetitive code here. You can work them back in later if you want. For now, just run that code. If your issue persists, then the issue is in code elsewhere. Maybe inside the flashlight object, maybe in the player object, maybe in some other object.

Does obj_player_escaperun_v2 handle all the code for the player, including running and being idle? Or do you use instance_change() when the player stops running?
 

Dwighty4000

Member
By chance, have you make the flash object a child of the player?
I just take the coordinates of the obj_player like this, in the step-event of the obj_flashlight:
INFO: the obj_flashlight is only the moving texture of a flashlight and not the light!
Code:
      x = obj_player_escaperun_v2.x;
      y = obj_player_escaperun_v2.y;
nothing more...
 

samspade

Member
I just take the coordinates of the obj_player like this, in the step-event of the obj_flashlight:
INFO: the obj_flashlight is only the moving texture of a flashlight and not the light!
Code:
      x = obj_player_escaperun_v2.x;
      y = obj_player_escaperun_v2.y;
nothing more...
Isn't that the problem right there?
 

Dwighty4000

Member
Isn't that the problem right there?
I found out that it plays no matter how large the numbers of dinstace are in "x" or "y" the flashlight will move like delayed if he starts walking and stop. With a delay of 10 pixels and that can't be the fault of x or y because X and y are to high for making a delayed looking movment by start pressing W,A,S,D and stop press the keys...
Because no matter how big the distance between X and Y, the delay is always 10 pixels...
And I have no idea why?
But it's not that bad at all because it looks even better when the flashlight moves to the player with a little delay, but I would still like to have an answer as to why it behaves so strangely?
 

samspade

Member
I found out that it plays no matter how large the numbers of dinstace are in "x" or "y" the flashlight will move like delayed if he starts walking and stop. With a delay of 10 pixels and that can't be the fault of x or y because X and y are to high for making a delayed looking movment by start pressing W,A,S,D and stop press the keys...
Because no matter how big the distance between X and Y, the delay is always 10 pixels...
And I have no idea why?
But it's not that bad at all because it looks even better when the flashlight moves to the player with a little delay, but I would still like to have an answer as to why it behaves so strangely?
The short, and unhelpful, answer is: it is because of code you wrote but not any of the code you've currently posted. Unfortunately, since we don't know where the code is, it is understandably hard to know what to post.

The easiest way to find out that I can think of would be to systematically comment out portions of your code and until the behavior stops. At that point, you'll at least know what code is responsible and if it is not immediately clear why, could post it here.

Purely guessing it seems that you have several systems which are intertwined and affecting each other causing unexpected, but technically correct, events to occur.
 

TailBit

Member
Maybe I should ask it like this .. if you remove all the code in that step event .. will it still follow the player in any way?
 

Bearman_18

Member
Oh, so problem is that the flashlight is following the player, but delayed by ten pixels, right? Does the player move at 10 pixels per frame? If so, then I've had this issue before.

Simply, the problem is that you're updating the flashlight's position first, and then the players, meaning the flashlight will move to the right place, but then the player will move ahead of it, only allowing the flashlight to "catch up" if the player stops. Try putting the flashlight's movement code in the end step event to force it to happen last, or otherwise make it happen after the player has moved.
 
Top