1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 [SOLVED] Making a shield object follow player upon creation

Discussion in 'Programming' started by Anti-Icarus, Jan 29, 2017.

  1. Anti-Icarus

    Anti-Icarus Member

    Joined:
    Jun 21, 2016
    Posts:
    34
    I'm working on an endless scrolling shmup and I'm having a bit of difficulty implementing the use of a shield in it. I want to have enemies sometimes drop a few power-ups upon death. On one hand, I managed to get a functioning power-up that upgrades the number and power of my player character's shots. Getting the shield power-up to work, on the other hand, has proven to be a more complicated matter. I Googled the issue, found this forum thread, and attempted to copy and implement the suggested code in the shield object's Create and Step events. But for some reason it doesn't seem to work.

    In my player character's collision event with the shield power-up object, that object gets destroyed and creates an instance of the shield object like this:

    2017-01-29_01h52_43.png

    I ended up getting the following message the moment I collided with the shield power-up as I ran my game:

    When I had it apply to the Other object (the shield being created), the shield just doesn't appear at all.

    To be more specific, I'm trying to make a shield around the player character that would briefly protect him from one hit kills for a few seconds. Based on the thread I read, I wrote the following code for the shield's Create event:

    Code:
    image_speed = 30;
    alarm[0] = 120;
    with (instance_create (x, y, obj_Shield))
    {
        follow = other.id;
    }
    and it's Step event:

    Code:
    if instance_exists(follow)
    {
        x = follow.x;
        y = follow.y;
    }
    else instance_destroy();
    And my Alarm[0] event is set to destroy the shield instance after 4 seconds.

    It's obvious that my circumstances were different from the ones experienced by the forum thread's original author and I'm convinced that the code that solved the author's problems does not work for me. What would be the most efficient way to create a shield around the player that lasts for 4 seconds upon collision with the shield power-up object?
     

    Attached Files:

  2. Perseus

    Perseus Not Ragarnak Forum Staff Moderator

    Joined:
    Dec 29, 2016
    Posts:
    953
    That's because you're creating a shield instance using the Create event of the shield object. Won't that keep on creating an infinite number of shield instances? GM is warning you about the same thing. Put that code in the player object for execution when the shield instance is to be created.
     
  3. Diveyoc

    Diveyoc Guest

    So your enemy drops a power up.
    In your obj_power_up, add a collision event with the obj_player and for the code just put:

    Collision Event:
    Code:
    instance_create(x, y, obj_shield)
    instance_destroy();  // This will just destroy the power up upon collision
    If you just have a circle shield around the player, I would just make sure the sprite is centered, and then in the obj_shield, just make the obj_player the parent of the shield.
    Then you don't need to worry about follow code or anything like that.
    Then create an End Step in the obj_shield so that it keeps redrawing it every step to move with your player.

    obj_shield End Step:
    Code:
    if instance_exists(obj_shield)
         {
         x = obj_player.x;
         y = obj_player.y;
         }
    I'm going through this a little tired, but I think it's all right.
    I'd get rid of your alarm for now. Just get your power-up/shield working properly and then add the alarm later on how you want to destroy the shield.

    Edit: Oh, and add the create event for your shield with whatever you might need, like image_speed = 30 and what not.
     
    Piyo likes this.
  4. Anti-Icarus

    Anti-Icarus Member

    Joined:
    Jun 21, 2016
    Posts:
    34
    Parenting the shield object to the player object didn't work as it caused a conflict between the two and crashed the game. The problem lies in three factors:

    1) The list of events in the player object is like this:

    2017-01-29_16h13_41.png

    2) The code in the Create event is like this:

    2017-01-29_14h59_57.png

    3) And the press S-key event has this list of actions:

    2017-01-29_14h57_13.png

    As you can probably guess, I have declared the power_up variable exclusively for my player's attack function. To be precise, I set up the attack function to change depending on the power_up variable which, unlike the shield power-up, increases whenever the player collides with the attack power up. As it stands, the behavior of the shield is like this:

    Xtreme Blaze Surfing shield bug analysis gif cropped.gif

    As I thought about the fact that the attack power-up works the way it is intended and the shield power-up does not, I'm probably guessing that the problem could be solved like this:

    1) Write a boolean variable for the shield in the player object's Create Event that would detect when the player collides with the shield power-up object and initialize it to false.
    2) Set the player object to collide with the shield power-up object.
    3) Set up the actions of the above event via Drag-N-Drop or GML in a way to true ensure that the shield itself would always surround and follow the player every time that happens; that would set the boolean variable to true and set it back to false when the shield gets destroyed in its Alarm[0] event.

    What do you think?
     
  5. Diveyoc

    Diveyoc Guest

    I'm working on a top down game, so not sure if some of my code would work the same.
    So remove the parenting then.
    I would think that just putting the code in the end that I suggested would put your shield around your player once the shield gets created.
    if instance_exists(obj_shield)
    {
    x = obj_player.x;
    y = obj_player.y;
    }

    I think with this code your shield should follow the player no matter how you create it.
    You would also want to do this wherever your shield instance create is, so that it gets created on the player from the get go.
    instance_create(obj_player.x, obj_player.y, obj_shield);
    I think all you should need is this instance create code in a collision event if your player collides with the power up.
     
  6. Anti-Icarus

    Anti-Icarus Member

    Joined:
    Jun 21, 2016
    Posts:
    34
    It worked! I had to adjust the x and y variables to center it, but it worked! Thank you!

    Now it's just a matter of adjusting the shield itself.
     
  7. Diveyoc

    Diveyoc Guest

    Ok good. Yes that code was assuming that the x,y of both player and shield were centered in your sprite. If not, then you would need to adjust the x,y which sounds like what you did.
     

Share This Page