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

Discussion in 'Programming' started by Dwighty4000, Jan 12, 2020 at 11:03 PM.

  1. Dwighty4000

    Dwighty4000 Member

    Joined:
    Sep 25, 2019
    Posts:
    56
    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
    
     

    Attached Files:

    Last edited: Jan 13, 2020 at 12:11 AM
  2. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,181
    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.
     
  3. JaimitoEs

    JaimitoEs Member

    Joined:
    Aug 9, 2016
    Posts:
    210
    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: Jan 12, 2020 at 11:51 PM
  4. Dwighty4000

    Dwighty4000 Member

    Joined:
    Sep 25, 2019
    Posts:
    56
    This is impossible because flashlightitemgesammelt is in this case always "true"
     
  5. Dwighty4000

    Dwighty4000 Member

    Joined:
    Sep 25, 2019
    Posts:
    56
    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 !?
     
  6. JaimitoEs

    JaimitoEs Member

    Joined:
    Aug 9, 2016
    Posts:
    210
    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: Jan 13, 2020 at 12:07 AM
  7. Dwighty4000

    Dwighty4000 Member

    Joined:
    Sep 25, 2019
    Posts:
    56
    why do you always go into the if function that is not accessed?
     
  8. JaimitoEs

    JaimitoEs Member

    Joined:
    Aug 9, 2016
    Posts:
    210
    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: Jan 13, 2020 at 12:39 AM
  9. Dwighty4000

    Dwighty4000 Member

    Joined:
    Sep 25, 2019
    Posts:
    56
    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;
     
  10. JaimitoEs

    JaimitoEs Member

    Joined:
    Aug 9, 2016
    Posts:
    210
    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
    
    
     
  11. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,181
    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.
     
    JaimitoEs likes this.
  12. JaimitoEs

    JaimitoEs Member

    Joined:
    Aug 9, 2016
    Posts:
    210
    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:
     
  13. Dwighty4000

    Dwighty4000 Member

    Joined:
    Sep 25, 2019
    Posts:
    56
    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.
     
  14. JaimitoEs

    JaimitoEs Member

    Joined:
    Aug 9, 2016
    Posts:
    210
    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: Jan 13, 2020 at 1:06 AM
  15. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,181
    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
    
    
     
  16. JaimitoEs

    JaimitoEs Member

    Joined:
    Aug 9, 2016
    Posts:
    210
    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
     
  17. Dwighty4000

    Dwighty4000 Member

    Joined:
    Sep 25, 2019
    Posts:
    56
    it spamms "A" if the player looks left and its spamm "B" if the player looks right: what are you expecting with this?
     
  18. JaimitoEs

    JaimitoEs Member

    Joined:
    Aug 9, 2016
    Posts:
    210
    You are right it´s don´t executing your last region, try my script and tell what you see...
     
  19. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,181
    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.
     
  20. JaimitoEs

    JaimitoEs Member

    Joined:
    Aug 9, 2016
    Posts:
    210
    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: Jan 13, 2020 at 2:08 AM
  21. Dwighty4000

    Dwighty4000 Member

    Joined:
    Sep 25, 2019
    Posts:
    56
    there is only a step event for this and nothing more on this object without a "depth = 17" in the create event...
     
  22. JaimitoEs

    JaimitoEs Member

    Joined:
    Aug 9, 2016
    Posts:
    210
    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...
     
  23. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,181
    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?
     
  24. TailBit

    TailBit Member

    Joined:
    Oct 16, 2019
    Posts:
    113
    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?
     
  25. JaimitoEs

    JaimitoEs Member

    Joined:
    Aug 9, 2016
    Posts:
    210
    By chance, have you make the flash object a child of the player?
     
  26. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,335
    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?
     
  27. Dwighty4000

    Dwighty4000 Member

    Joined:
    Sep 25, 2019
    Posts:
    56
    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...
     
  28. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,181
    Isn't that the problem right there?
     
  29. Dwighty4000

    Dwighty4000 Member

    Joined:
    Sep 25, 2019
    Posts:
    56
    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?
     
  30. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,181
    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.
     
  31. TailBit

    TailBit Member

    Joined:
    Oct 16, 2019
    Posts:
    113
    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?
     
  32. Bearman_18

    Bearman_18 Member

    Joined:
    Jan 24, 2018
    Posts:
    214
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice