1. Hey! Guest! The 33rd GMC Jam will take place between May 23rd, 12:00 UTC (Friday noon) and May 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

[Fixed] Variable isn't working as expected

Discussion in 'Programming' started by Zaksley, May 17, 2019 at 10:06 PM.

  1. Zaksley

    Zaksley Member

    Joined:
    Apr 19, 2019
    Posts:
    15
    Hi there.
    I'm working on a game with asteroids. There is different type of asteroid : Red, purple, orange, ...
    I created a variable hp_asteroid for all of them. Hp_asteroid = 1 for Red/Purple, 3 for Hp_asteroid.

    Every time the bullet touch an asteroid, hp_asteroid -= 1

    My problem : The variable hp_asteroid isn't working as expected : Every asteroid got his own hp_asteroid, that's ok but they all start with the hp_asteroid from the red asteroid.
    I'm pretty sure i wrote some bullshits and it should be easy to fix but i don't understand why i can't.

    Here, my code when an asteroid is create :

    Code:
    
    switch(image_index)
    {
    
        case(0): //Asteroide Rouge
            speed = random_range(1, 2);
            hp_asteroid = 1;
            break;
           
        case(1): // Asteroide Violet
            hp_asteroid = 10000;
            speed = random_range(2.5 , 4);
            break;
       
        case(2): // Asteroide Orange
            hp_asteroid = 3;
            speed = 1;
            break;
           
        case(3): // Asteroide Rose
            hp_asteroid = 3;
            speed = random_range(2.5 , 4);
            break;
           
    }
    
    
    
    
    If i touch a purple, it will instantly kill the asteroid, even if in my code hp_asteroid = 10000.
    Any idea how to fix this ?
     
  2. EvanSki

    EvanSki Member

    Joined:
    Apr 17, 2018
    Posts:
    361
    Its in the collison event with whatever and the asteroid

    Depending on how much hp from the asteroid you take when you hit it, lets say (X) amount.

    For every step or frame of your game it will remove X amount from your asteroids hp, so yes it will seem to be instantly be destroyed but really its loosing health every few seconds

    How you fix this is up to you

    -disable collison
    -have the player be bounced away to avoid the overlap of taking health away
     
  3. Zaksley

    Zaksley Member

    Joined:
    Apr 19, 2019
    Posts:
    15
    I don't really understand.
    Do you need my code for the collision "bullet - asteroid" ?
     
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,508
    You did not put globalvar in front of hp_asteroid anywhere in your project, did you?


    Collision With Asteroid event in Bullet:
    Code:
    with other {
        if --hp_asteroid==0
            instance_destroy();
    }
    instance_destroy();
    
    Then in the Asteroid's destroy event, add points, create smaller asteroids, or do whatever else you need done.

    Also, when do you set the image_index of the asteroid? If your code for creating the asteroids is like:
    Code:
    with instance_create(x,y,obj_asteroid) {
        image_index = irandom(3);
    }
    Then that won't work, because the image_index will be set AFTER the asteroid is created. In which case, you should move your switch statement to User Event 0 and then make sure you call it whenever you create an asteroid:
    Code:
    with instance_create(x,y,obj_asteroid) {
        image_index = irandom(3);
        event_user(0);
    }
     
  5. Zaksley

    Zaksley Member

    Joined:
    Apr 19, 2019
    Posts:
    15
    Hi again TheouAegis (You already help me on my projet, Thanks you again!)
    No, i'm pretty sure i didn't.

    Here is my code for Collision With Asteroid event in Bullet :

    Code:
    
    instance_destroy();
    
    audio_play_sound(snd_die, 1.5, false);
    
    with(other)
    {
        //creation debris
        repeat(30)
        {
            var debris = instance_create_layer(x, y, "Instances", obj_debris);
           
            //color
            switch(image_index)
            {
           
                case(0):
                    debris.image_index = 1;
                    break;
       
                case(1):
                    debris.image_index = 2;
                    break;
       
                case(2):
                    debris.image_index = 3;
                    break;
                   
                case(3):
                    debris.image_index = 4;
                    break;
            }
        }
       
       
        hp_asteroid -= 1;
       
        switch(sprite_index)
        {
           
            case(spr_asteroid_small):
                score += array_score[image_index] / 4
               
                if(hp_asteroid == 0)
                {
                    instance_destroy();     
                }
                break;
               
            case(spr_asteroid_medium):
                score += array_score[image_index] / 2 
               
                if(hp_asteroid == 0)
                {
                    script_create(spr_asteroid_small);
                    instance_destroy();
                }
               
                break;
           
            case(spr_asteroid_big):
                score += array_score[image_index]
               
                if(hp_asteroid == 0)
                {
                    script_create(spr_asteroid_medium);
                    instance_destroy();
                }       
                break;
    
        }
    
    }
    
    
     
  6. Zaksley

    Zaksley Member

    Joined:
    Apr 19, 2019
    Posts:
    15
    Alrightso you find the mistake :
    There is 2 way to create an asteroid : Every X seconds, an asteroid spawn or when a bullet hit a medium/big asteroid
    => In both case, i set image_index after creating the instance.

    What is the User event ? How does it works ?
     
  7. Zaksley

    Zaksley Member

    Joined:
    Apr 19, 2019
    Posts:
    15
    I did what u said and it's working perfectly !!
    But i have some questions :

    * How User Event works ?
    * How can I write Clean code ?

    Here is my code to set the User Event :

    1) At the beginning of the room_game

    Code:
        case room_game:
    
        repeat(10)
        {
            var xx = choose(
                irandom_range(0, room_width * 0.2),
                irandom_range(room_width * 0.8, room_width)
            );
    
            var yy = choose(
                irandom_range(0, room_height * 0.2),
                irandom_range(room_height * 0.8, room_height)
            );
           
            var new_asteroid = instance_create_layer(xx, yy, "Instances", obj_asteroid);
            new_asteroid.sprite_index = choose(spr_asteroid_big, spr_asteroid_medium, spr_asteroid_small);
            new_asteroid.image_index = 0;
           
    */
            with(new_asteroid)
            {
                event_user(0);        
            }
    /*
    // Why I have to put a with(...) and i can't use new_asteroid.event_user(0) ? 
       
        }   
    
        alarm[10] = room_speed;
       
    }
    
    
    2) Script after collision bullet - asteroid

    Code:
    
    var new_sprite = argument0;
    
    {
            repeat( array_spawner[image_index] )
            {
                var new_asteroid = instance_create_layer(x, y, "Instances", obj_asteroid);
                new_asteroid.sprite_index = new_sprite;
                new_asteroid.speed = speed;
                new_asteroid.image_index = image_index;
               
    */
                with(new_asteroid)
                {
                    event_user(0);   
                }
    
    /*
    
    //Same question
               
            }
    }
    
    

    3) Script spawn of asteroids every X second

    Code:
    var couleur = argument0
    
    if(choose(0, 1) == 0)
    {           
    
        // top or bottom
        var yy = choose(0, room_height);
        var xx = irandom_range(0, room_width);
       
    } 
    
    else
    {
        var yy = irandom_range(0, room_height);
        var xx = choose(0, room_width);
       
    }
    
    var new_asteroid = instance_create_layer(xx, yy, "Instances", obj_asteroid);
    new_asteroid.sprite_index = choose(spr_asteroid_big, spr_asteroid_medium, spr_asteroid_small);
    new_asteroid.image_index = couleur;
    
    */
    with(new_asteroid)
    {
        event_user(0);   
    }
    /*
    //Same question
    
    
     
  8. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,508
    User Events are just like scripts, except they are loaded with the object. They help keep your code clean, in my opinion. Unfortunately, you're limited to how many user events you can actually have per object. ...If they're allowed in Trial Versions of GM, then they essentially allow you to drastically increase the scripts limit (which are restricted to 10 scripts or so in the trial).

    Everything you put new_asteroid. in front of you can just move into the with new_asteroid{ block (minus the "new_asteroid." prefix).

    The prefix is ONLY for referring to variables, not calling scripts or events or functions. Also, you need the code inside the user event to apply to the asteroid, which means you need the with to shift the focus.
     
  9. Zaksley

    Zaksley Member

    Joined:
    Apr 19, 2019
    Posts:
    15
    Thanks for all !
     

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