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] Want to create a timer, having issues

Discussion in 'Programming' started by KitKatarine, Oct 28, 2016.

  1. KitKatarine

    KitKatarine Guest

    Hello all, I am less-than-pleased-as-punch to say that I am having more difficulty.

    I want there to be a delay between clicking a cut icon to the tree actually being, well, cut.

    I have tried alarms, and besides not understanding how they work, I have a tendency to lose track of them if they aren't there. (My code is dotted with global variables D: )

    I have also tried variables, but for whatever reason, I can't get the variables to do the things I want to do! SO.

    This is what my code looks like as of right now:
    Code:
    if mouse_check_button(mb_left) with instance_place(mouse_x, mouse_y, obj_cut) // if I clicked the cut icon
    {  
      
        if global.energy > 0   // if player energy is above 0, do this thing
        {
            with instance_place(obj_cut.x, obj_cut.y + 96, obj_tree) // do this thing with the tree below the cut icon
            {
              
                {
                  
                  // I want to add a timer here for 2 seconds //
    
                instance_change(obj_stump, false);    // change to a stump
                global.energy = global.energy - 10       // subtract energy
                global.wood = global.wood +3             // add 3 wood.
              
                }
            }
        instance_deactivate_object(obj_cut);         // then kill the cut icon.
        }
    }
    
    I saw in another thread from August that I could create variables to help me set a delay. So I tried something along the lines of:


    Code:
    /// Create Event ///
    
    global.countdown = room_speed*2; // This is put in the create event with all the other global variables 
    
    
    Code:
    /// STEP EVENT///
    
    if mouse_check_button(mb_left) with instance_place(mouse_x, mouse_y, obj_cut) // if I clicked the cut icon
    {  
      
        if global.energy > 0   // if player energy is above 0, do this thing
        {
            with instance_place(obj_cut.x, obj_cut.y + 96, obj_tree) // do this thing with the tree below the cut icon
            {
              
                global.countdown = global.countdown - 1   // Which should reduce the amount by 1
                 if (global.countdown = 0)    // This is essentially what was in the other thread.
                {
                instance_change(obj_stump, false);    // change to a stump
                global.energy = global.energy - 10       // subtract energy
                global.wood = global.wood +3             // add 3 wood.
               }
            }
        instance_deactivate_object(obj_cut);         // then kill the cut icon.
        }
    }

    Now it just gets rid of obj_cut.

    I am so confused as to what I'm doing wrong. There isn't enough coffee in the world for this! D:

    Thank you all so much for any help you might be able to give me~!
     
  2. DanMunchie

    DanMunchie Member

    Joined:
    Sep 19, 2016
    Posts:
    121
    If you're going to have a countdown timer, it will need to be in obj_tree since you are deactivating obj_cut the instant you click it (i.e. on the next steps, it won't calculate any more).

    Instead - add an alarm to obj_tree (or a countdown if you prefer) - and have only this alarm/countdown be started when you click obj_cut. Then in the alarm event of obj_tree (or when countdown == 0) have obj_tree do the things it needs to do:
    Code:
                instance_change(obj_stump, false);    // change to a stump
                global.energy = global.energy - 10       // subtract energy
                global.wood = global.wood +3             // add 3 wood.
     
    KitKatarine likes this.
  3. KitKatarine

    KitKatarine Guest

    Alright! I'll see if this works. Thank you~
     
  4. DanMunchie

    DanMunchie Member

    Joined:
    Sep 19, 2016
    Posts:
    121
    No worries - I agree alarms are confusing at first.

    Basically:
    When they're at rest, alarms are equal to -1. They will sit and wait at -1 until you set their value to a positive number.
    When their value is 0 or above, they will automatically count down every step until they get to -1 and go back to resting state
    When their value is exactly 0, they trigger the Alarm event

    E.g.

    obj_tree has an Alarm 0 event to add the wood etc.
    When you click obj_cut, set that obj_tree instance's .alarm[0] = room_speed*2;
    2 seconds later, the Alarm 0 event in that obj_wood instance will trigger
     
    KitKatarine likes this.
  5. Roderick

    Roderick Member

    Joined:
    Jun 22, 2016
    Posts:
    580
    One caveat: If you don't have a matching Alarm Event, the Alarm will never count down.

    I once tried to use an Alarm, but checked its status with an if (alarm[0] == x) check in the step code so that I could trigger different things as it counted down. Nothing happened. Test code showed that Alarm[0] stayed at whatever value I set it at, until I added a blank Alarm[0] Event.
     
    KitKatarine and DanMunchie like this.
  6. Diveyoc

    Diveyoc Guest

    Could he just use a variable like cut_counter = 0; Same concept as using a fire counter or rate of fire counter.
    Then whatever action cuts the tree, first command would be cut_counter += 1.
    If cut_counter >= 100
    // Code to cut tree //
    Then reset cut_counter to 0 again afterwards.
     
  7. KitKatarine

    KitKatarine Guest

    Okay maybe I'm not getting it.

    In obj_player's step, I now have:
    Code:
    if mouse_check_button(mb_left) with instance_place(mouse_x, mouse_y, obj_cut)
    {   
       
        if global.energy > 0
        {
            with instance_place(obj_cut.x, obj_cut.y + 96, obj_tree)
            {
                global.countdown = global.countdown - 1
    
            }
        instance_deactivate_object(obj_cut);
        }
    }
    
    Which - as I understand it - means that if I click the obj_cut icon, it will start to count down, and then delete the cut icon in the process.

    So now in obj_tree's step, I have this:
    Code:
                if (global.countdown = 0)
                {
               
                instance_change(obj_stump, false);
                global.energy = global.energy - 10
                global.wood = global.wood +3
               
                }
    
    Which, because I have countdown going down in one step, it should register as 0 eventually right? But, alas, to no avail. I sat and waited. All it did was sit there.

    Am I missing something?
     
  8. TheBroman90

    TheBroman90 Member

    Joined:
    Jun 20, 2016
    Posts:
    371
    Code:
    if mouse_check_button_pressed(mb_left) with instance_place(mouse_x, mouse_y, obj_cut)
    {  
        if global.energy > 0
        {
            with instance_place(obj_cut.x, obj_cut.y + 96, obj_tree)
            {
                alarm[0] = room_speed*2;
            }
        instance_deactivate_object(obj_cut);
        }
    }
    And this is for obj_tree.
    Code:
    /// Alarm0 Event
    instance_change(obj_stump, false);
    global.energy = global.energy - 10;
    global.wood = global.wood +3;
     
    KitKatarine likes this.
  9. KitKatarine

    KitKatarine Guest

    AHAAAAAAAA!

    That works! That works exactly!

    Okay, okay perfect. I am still unsure about alarms, but at least now I understand them. Thank you so so much! Saved me again~

    (I can almost guarantee I'll be back. Wait for it~)
     

Share This Page