Creating a countdown timer with Creation code?

Discussion in 'Programming' started by Zapzel-24, Sep 14, 2018 at 3:29 PM.

  1. Zapzel-24

    Zapzel-24 Member

    Joined:
    Jul 19, 2016
    Posts:
    17
    I have created a sentry enemy that fires at given intervals with the alarm variable however I feel like the alarms are too mechanical and mundane so I plan to set up an timer system as a creation code. I have a few ideas how to pull it off but the real trick is resetting the timer in the creation code to its original value to repeat the loop.

    I plan to use the step event to call out the timer's value in the creation code and ticks down with every step there will be a if statement when the timer reaches zero it performs the firing and resets the timer again to repeat the process.

    But the real question is can I really reset a value in a creation code and how can it be pulled off ?

    *Note* the sentry I created is one object that uses a creation code value (index) to determine its appearance and the direction where its firing.
     
  2. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    379
    I think you misunderstand events. You can't make a one time event (like Create Event) loop. The only way to make it do anything like a loop is a for or while statement, but that will pause EVERYTHING else from executing until it completes. I have no idea what you mean by alarms being mechanical and mundane...Any timer is basically a variable counting down. You can use alarms, or you can create your own timer (using the Step Event, not the Create Event) with custom variables, but you can make the alarm system do anything that you could do in a custom system.

    An example of a custom alarm system:

    Create Event
    Code:
    custom_alarm = 60;
    Step Event
    Code:
    if (custom_alarm > -1) {
      custom_alarm--;
    }
    if (custom_alarm == 0) {
      //Code you want executed when alarm triggers.
    }
    
     
    Phil Strahl likes this.
  3. Zapzel-24

    Zapzel-24 Member

    Joined:
    Jul 19, 2016
    Posts:
    17
    I do not mean creation EVENT i meant creation CODE were you select an object in the room and a place custom code for the step event. As mentioned previously, I have a sentry that is one object that uses this method to determine where it should be facing and how its drawn. I was wondering if its possible to to make a timer creation code that resets itself, this is what I am talking about:

    *Note* I am currently using the alarm variable as a temporary placeholder

    //Alarm Event

    Code:
    // Aming Right
    if (index == 1)
    {
    fire = instance_create(x,y,obj_bullet)
    fire.speed = 5;
    fire.direction = 0;
        
    alarm[0] = 60;
    }
    //Aming Up
    if (index == 2)
    {
    fire = instance_create(x,y,obj_bullet)
    fire.speed = 5;
    fire.direction = 90;
        
    alarm[0] = 60;
    }
    // Aming Left
    if(index == 3)
    {
    fire = instance_create(x,y,obj_bullet)
    fire.speed = 5;
    fire.direction = 180;
        
     alarm[0] = 60;
    }
    // Aming Down
    if (index == 4)
    {
    fire = instance_create(x,y,obj_bullet)
    fire.speed = 5;
    fire.direction = 270;
        
    alarm[0] = 60;
    }
    
    //Draw Event
    Code:
    //Facing Right
    if (index == 1)
    {
        draw_sprite(spr_sentry,0,x,y);
    }
    
    //Facing Up
    if (index == 2)
    {
        draw_sprite(spr_sentry_up,0,x,y);
    }
    
    //Facing Left
    if(index == 3)
    {
        draw_sprite(spr_sentry_left,0,x,y);
    }
    
    //Facing Down
    if (index == 4)
    {
        draw_sprite(spr_sentry_down,0,x,y);
    }
    And in the room I use the creation code and write the index number to determine each sentry object in the room were its facing and firing, But since this is one object they all of the sentries in the room share the same alarm system and fire at the same interval. I theorized that if its possible to create a timer system in the creation code it can create a level of unpredictability or make them firing in patterns that the player must anticipate.
     
  4. OblivionSkull21

    OblivionSkull21 Member

    Joined:
    Sep 26, 2017
    Posts:
    131
    Create Event:
    Code:
    if alarm[0] = -1{
    alarm_set(0, room_speed * 10) //this is a ten second timer. Change it if you want for longer/shorter.
    }

    alarm[0] Event:

    Code:
    if alarm[0] = -1{
    alarm_set(0, room_speed*10);
    }
    Then just put what you want to happen when the timer hits 0 inside the alarm event.

    The create event doesn't reset the alarm. You reset the alarm INSIDE the alarm itself.
     

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