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

GMS 2 Game restarts after cooldown

Discussion in 'Programming' started by Oniiknil LPP, Mar 11, 2019.

  1. Oniiknil LPP

    Oniiknil LPP Member

    Joined:
    Mar 11, 2019
    Posts:
    8
    Hello,

    (before everything else, I apologize if my english is bad, it's not my first language)

    i'm creating a game, and I'm attemping to add a cooldown about 6 secs before the game restarts, so i first tried to create a variable that tick down every step, like that :

    Create event:
    create event1.PNG

    step event:
    step event 1.PNG

    but i didn't worked...

    then i tried to use alarms, more precisely create an objet at the right moment that start the alarm

    Create event:
    create event2.PNG

    step event:
    step event 2.PNG


    But it does not work either
    I also tried some alternatives slightly differents than what you can see on the images, without success

    Now i don't know how to do at all, so if you have an idea, please help me
     
  2. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,278
    I'm not sure which events those are as the names are not in English, but ensure that you only set the alarm once, not repeatedly. Otherwise, it will keep being reset and never count down.
     
  3. spoonsinbunnies

    spoonsinbunnies Member

    Joined:
    Aug 24, 2018
    Posts:
    38
    honestly the way both of those are written should work I personally don't like alarms, so how I would proceed to test is put the first method back in then in the draw event of the object
    draw_self()
    draw_text(x,y-32,global.hp)
    draw_text(x,y,restart_cooldown)

    while the wont solve the problem it will allow you to see if the counter is ticking down, when health hits 0. I honestly hate when something like this happens but both your codes should in theory work
     
  4. teamrocketboyz

    teamrocketboyz Member

    Joined:
    Jul 27, 2017
    Posts:
    272
    im not sure how to use alarms myself so i use a method based on the room speed. try changing the line (restart_cooldown - =1) what this is doing is taking 1 from your initial 360 so it stays on 359.

    what you need to change it to is
    Code:
    restart_cooldown -=1/room_speed*60 
    this will take away -1 every 60fps, meaning every second in your game time. set your cooldown_timer to = 10 in the create event and this should give you a to second delay.

    i would also change your < = 0 lines to <1 meaning anything below 1 (0) will set it off.

    hope this helps although im sure i will get critiqued by some of the senior members on here, but im new to all of this too and thats just how i do things.

    EDIT i know some members will bring this up so before they do. they will say that -=1/room_speed*60 is wrong because if your game is set at 60fps and you are doing 1 (divided) / by room_speed then you are doing 1 divided by 60 so there is no need to (times ) * by 60 afterwards as this will be the same as just dividing by room speed to begin with.

    for some reason the code -=1/room_speed does absolutely nothing but *60 at the end and it works. (infact if anyone can tell me why and help me out too that would be nice)
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,514
    The issue I bet is he is resetting the player's hp somewhere before the alarm has finished counting down and his code only lets it count down when the player has no hp.
     
  6. Oniiknil LPP

    Oniiknil LPP Member

    Joined:
    Mar 11, 2019
    Posts:
    8
    The alarm is not set once, but it's set on an object that is created when hp reaches 0, then the game restarts and the object is destroyed, so it should work

    I did it, and sometimes the counter tick down, and sometimes the game restarts immediately, i don't kniw why this is happening...

    I did it too, and the result is the same, I think it depends on the moment hp reaches 0

    I don't think so, i checked my program several times without finding anything like that
     
  7. teamrocketboyz

    teamrocketboyz Member

    Joined:
    Jul 27, 2017
    Posts:
    272
    i think ive figured it out. your global.hp is never hitting below 0, i will try create some sode for you and see if that helps

    EDIT ok dude try this

    Code:
    image_ speed = 0
    
    global.hp = 3
    
    restart_cooldown = 360
    Code:
    if global.hp < 1 {
        global.hp = 0
    }
    
    image_index = global.hp
    
    if global.hp = 0 && restart_cooldown > 0 {
        restart_cooldown -=1/room_speed*60
    }
    
    if restart_cooldown < 1 {
       game_restart();
    }
    
    }
    ive just created a blank project and tested this code and it works so hopefully all is good. you can play around with the restart_cooldown = 360 to get the desired delay time.

    you can always use this code within a draw_gui event to show that your restart_cooldown is counting down
    Code:
    draw_text(100,100,restart_cooldown)
    hope this helps.
     
    Last edited: Mar 12, 2019
  8. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    No need for another object for the alarm.
    Just do alarm[0] = restart_cooldown;
    And do in alarm 0 event
    game_restart();

    Dans le même object que ta variable restart_cooldown
     
  9. Oniiknil LPP

    Oniiknil LPP Member

    Joined:
    Mar 11, 2019
    Posts:
    8
    I tried, and it's always the same, sometimes the game restarts exactly when hp reaches 0, exept when it reaches 0 around 3 second after the game started, it seems like the cooldown is always ticking down, so it work only if hp = 0 at the same time as the cooldown is reset.

    I used another object because the alarm stats when the object is created, so i created an object using "instance_create_layer(...)" when hp = 0, i dont why it doesn't work.
    (unless there is a way to choose when the alarm starts after the object is created...)
     
  10. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    Of course there is.
    Code:
    if(some_condition){
    alarm[0] = alarm_countdown;
    }
    Just make sure it's not reset to alarm_countdown every step. You could use a simple bool for that, like countdown_started, like so
    Code:
    if((some_condition)&&(!countdown_started){
    alarm[0] = alarm_countdown;
    countdown_started == true;
    }
     
  11. teamrocketboyz

    teamrocketboyz Member

    Joined:
    Jul 27, 2017
    Posts:
    272
    my method works fine 100% i have created a video and uploaded it to youtube of it working and showing the code ive used.

    its just a simple box with a hp of 3 and everytime i click the box with the mouse the hp goes down by 1 and the cooldown starts at 0, take a look

     
    SnotWaffle Studios likes this.
  12. Oniiknil LPP

    Oniiknil LPP Member

    Joined:
    Mar 11, 2019
    Posts:
    8
    I think it wont work in my case, because before the condition is validated, the alarm equal 0, and the code is supposed to be executed precisely when the alarm = 0 (I'm not sure what I'm saying, so I will still try)

    EDIT: it doesn't work, but it may be because I did not understand. Did you tell me to add tour code in step event or in alarm[0] ?

    2nd EDIT: Thank you for your video, but, for an unknown reason, the cooldown still doen't work sometimes ...

    3rd EDIT: actually, it does not work at any time: sometimes the gamles restart immediately, and sometimes the countdown just doesn't tick down...
     
    Last edited: Mar 12, 2019
  13. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    Well, I guarantee you it works and that it takes less than 60 seconds to implement in your project. Alarms are one of the basics, and if you're still not comfortable with them, I suggest you do some official tutorials (like Space Rock), they will teach you how to use them. Or press F1 and look them up in the manual
     
  14. teamrocketboyz

    teamrocketboyz Member

    Joined:
    Jul 27, 2017
    Posts:
    272
    thats odd because it works for me every time perfectly, as you can see. there must be something else within your gamethats affecting this.

    can i also ask why your hp is a global? and not just a single variable like health_ = 3 or even the in built health variable?

    EDIT the code i entered is for the step event withing your character and not for any alarm
     
  15. Oniiknil LPP

    Oniiknil LPP Member

    Joined:
    Mar 11, 2019
    Posts:
    8
    Thank you for your help, i think the problem comes from another object, i'll try to find a track...
     
  16. teamrocketboyz

    teamrocketboyz Member

    Joined:
    Jul 27, 2017
    Posts:
    272
    i dont know how new you are to game maker but each object should have its own health, so your characters create health should be health_ = 5 and your enemy could be health_1 = 3. these would be seperate healths for both characters and both work individually.

    if your characters health is a global for example global.health_ and your enemy is global.health too then the global is one big number so if its set to 3 and you get hit it goes down to 2 but then if you hit an enemy it also goes down by 1. make sure each health is individual.
     
  17. Oniiknil LPP

    Oniiknil LPP Member

    Joined:
    Mar 11, 2019
    Posts:
    8
    The health is global because, in my game, the player have to move on 3 different positions, but it's more a teleportation than a real movement, so it was easier for to create 3 different object (one for each position), with their own hitbox, and an object is destroyed when another is created. I used global because otherwise health would be reset at each movement.

    I thought about my first problem, and I think it comes from the projectile object, that i use to deacrease player hp, but i can't find anythingin the code.

    Here it is, hope you can help me :

    Create event:

    [​IMG]
    create event3.PNG
    (easy_movespd and hard_movespd are for different gamemodes)

    Step event:

    step event3.PNG
    [​IMG]
     
  18. teamrocketboyz

    teamrocketboyz Member

    Joined:
    Jul 27, 2017
    Posts:
    272
    Ok i think i may have figured out your problem, you have 3 playable characters right? each time you move one dies and the other spawns? im guessing that each character has a create event? and each create event had a global.hp = 3?

    you only need to create this once as its a global so it should only ever appear in 1 create script. as you can see i have created a video to show you what happens when its creates in all 3 of the players and how to correct it. in the video each time i click on a player it gets destroyed and replaced by the next player, as you can see when each has it on his create event it stays at 3 as its the first time that character has been created.

     
  19. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    That's a good example where "easier now" means "troubles later". You should not have to destroy your player to make it spawn elsewhere, that makes no sense to me at all. And if they are 3 DIFFERENT characters, then it's not logical to have only one global health variable. Why not use an instance variable on each of them? Or better yet, parenting all of them to a par_player, just to be sure everything is consistent across the board?
     
  20. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,514
    I did not take away from his post that he has three distinct separate characters, my takeaway was that he had one character represented by three different objects with each object representing the character in a different plane. In that situation, the global health is the way to do it for the player. However, of course you should not be destroying the characters at all. If anything, it should just be deactivating the player currently in control when switching to another player. Although both cases are pretty poor ideas, when he could just as easily have that one character move back and forth between layers almost just as easily. I would have to see the concept for his game to be able to give him proper guidance on how to do it, though.
     
  21. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    Yeah, not too sure where he's going at all either...this steered quite far away from the original alarm question, tho:D
     
  22. Oniiknil LPP

    Oniiknil LPP Member

    Joined:
    Mar 11, 2019
    Posts:
    8
    It could be it but I thought about that, so it put global.hp in the hp object...
     
  23. Oniiknil LPP

    Oniiknil LPP Member

    Joined:
    Mar 11, 2019
    Posts:
    8
    I found more informations about problem :

    the game restarted instantly only when the player was placed in the position, and when on the top or bottom position the cooldown was just not ticking down, because there was there was some code in the top position player that was supposed to restart the game after 5 secs. The cooldown doesnt work but only destroy the player objects immediately. I removed it and now the game run indefinitely, even when hp = 0, and cooldown doesn't tick down.
    I don't know why, but as you seem to be more experienced, you may find the mistake

    CREATE EVENTS (same for all):


    create event6.PNG

    STEP EVENT TOP POSITION:

    step event5.PNG

    STEP EVENT MIDDLE POSITION:

    step event6.PNG

    STEP EVENT BOTTOM POSITION:

    step evnt7.PNG

    (global.cooldown is the cooldwn between each movement of the player
    edit: it works, and the line " hp = global.hp " is useless, so i removed it)
     
    Last edited: Mar 15, 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