How to have momentum carried over to new object when they switch places

Discussion in 'Programming' started by MrCleans, Oct 11, 2019.

  1. MrCleans

    MrCleans Member

    Joined:
    Oct 11, 2019
    Posts:
    2
    I'm relatively new to gamemaker but know the basics and how it all works. While making a game I ran into an issue where the player character (playergrounded) goes from being affected by gravity then by the press of space not being affected (playerfly) so the player can fly. I decided to go down the route of having the player change objects by creating the other object and destroying itself. the issue I ran into is I don't know how to have the momentum be transferred to the other object and would like a decel effect. I was wondering if having them switch objects was a good idea or should it stay in one object or how would I be able to go about this.
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,964
    The easiest solution would be to have all in same object and use a variable to track flying state to skip gravity code. When your code and number of variables expands, copying necessary data to another instance would keep becoming a bigger hassle. You'd also introduce more potential for bugs as you'd probably need to maintain parts of same code in two objects and always keep them synched up.
    Code:
    if(!isFlying){
        // Gravity code here.
    }
    
    Pressing space then flips the variable between true and false states.
     
  3. Roderick

    Roderick Member

    Joined:
    Jun 22, 2016
    Posts:
    580
    If you absolutely need to keep the two objects method, there are two obvious ways to do it:

    1- Store the relevant variables globally or in an invisible control object, and read/write to that rather than the player object.

    2- When creating the new instance, copy all the relevant variables from the old instance to the new one before destroying the old instance.

    But your best choice is probably to use a state machine and keep everything in one object.
     
    Fredrik likes this.
  4. MrCleans

    MrCleans Member

    Joined:
    Oct 11, 2019
    Posts:
    2
    thx helps a lot
     
  5. Fredrik

    Fredrik Member

    Joined:
    Jun 21, 2016
    Posts:
    176
    This might me a silly suggestion (or not), but if you use instance_change maybe use instance_create instead, and make it so when they collide they change/give information if that makes sense, and then destroy the previous one.

    Like when it goes from not flying to flying (instance is changing) do something like this (in the player grounded):

    Code:
    instance_create(x,y,player_flying)
         {
         with player_flying
              {if place_meeting(x,y,player_grounded)
                   {
                   speed = player_grounded.speed;
                   x = player_grounded.x;
                   variable = player_grounded.variable;
    
                   (or whatever other variables you wanna keep from the previous object)
                   }
              }
         {instance_destroy();}
    }
    
    Not 100% this would work, but maybe something similiar to this.
    This is essentially @Roderick's 2. suggestion, I think.
     
    Last edited: Oct 16, 2019

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