• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [SOLVED] Is there a way to make this piece of code smaller?

Zabicka

Member
Create Event:
Code:
timer = 0;
Step Event:
Code:
if (timer < 0) {
    timer = 10;
    // Some other code
} else {
    --timer;
}
I was using this piece of code for multiple things in my projects
and I was wondering if I could make it smaller or just more compact.

Is there a different way to do this?
 
R

robproctor83

Guest
I would move the step event code into a script and reference the script, especially if you are using that same code over and over. The timer = 0 is as short as it gets apart from changing it to t=0. The only other thing I could think would be to chain that variable creation with others, but that isn't really important and only really useful for organization.
 
C

Catastrophe

Guest
That's pretty compact lol.

You could use alarms, I think it's even faster that way?

Or ternary operators if you're being silly, although I'm not sure if GML supports those.
timer < 0 ? {timer = 10; stuff} : timer--
(but please don't xD)
 
R

robproctor83

Guest
That's pretty compact lol.

You could use alarms, I think it's even faster that way?

Or ternary operators if you're being silly, although I'm not sure if GML supports those.
timer < 0 ? {timer = 10; stuff} : timer--
(but please don't xD)
What is wrong with Ternary? Also, I don't know if I have seen them like that before... usually it goes (x>0) ? 1 : -1;
 
C

Catastrophe

Guest
I'm actually not sure if you can pass a block into a ternary. I don't really use them.

Mostly it's just not great to use them because nobody will be able to read your code haha.
 
R

robproctor83

Guest
I'm actually not sure if you can pass a block into a ternary. I don't really use them.

Mostly it's just not great to use them because nobody will be able to read your code haha.
Ah yea, very true... Can't remember how many times people asked me what a ternary was lol... But, it's fairly easy to grasp once you know what they are, and it's a nice shorthand for simple ifs.
 

Zabicka

Member
I would move the step event code into a script and reference the script, especially if you are using that same code over and over. The timer = 0 is as short as it gets apart from changing it to t=0. The only other thing I could think would be to chain that variable creation with others, but that isn't really important and only really useful for organization.
Thank you for your answer! But I'm not sure how to make that piece of code in to a script.

Code:
var t;
if (t < 0) {
    t = argument0;
    // code
} else {
    --t;
}
Will this work? But there is one problem I can't figure out, how to put some other code in to a script.
 

Fredrik

Member
When I make my own timers like this, I usually write it something like this:

Create event:
timer = 0;

Step event:
if timer > 0 {timer -= 1;}

and then I can just set a time based on what I need.
 
C

Catastrophe

Guest
@Zabicka
My personal thoughts are you're overthinking this. Leaving things as they are in your OP is something even advanced user do. Not everything needs an optimization.
 

TailBit

Member
@ script:
Code:
// scr_timer
if (timer<0) timer = 10 else --timer; // maybe you could use if (!--timer) timer = 10;
return timer==10; // return true if timer was reset this time
step event
Code:
if( scr_timer() ){
    // some other code
}
 

Zabicka

Member
@ script:
Code:
// scr_timer
if (timer<0) timer = 10 else --timer; // maybe you could use if (!--timer) timer = 10;
return timer==10; // return true if timer was reset this time
step event
Code:
if( scr_timer() ){
    // some other code
}
Thank you for your help, but this script still needs variable from create event, right?
 
There's a way to use just one counter for all of your timers. In this example, global.timer is the one counter.

if (global.timer > trigger_time) {
do_thing();
}

You can set the timer like:

trigger_time = global.timer + some_amount

To cancel a timer, you can set it to some value in the extreme distant future.

trigger_time = 100000000000
 

CloseRange

Member
You guys already did the whole add a script thing so I want to do my own take :oops:
Code:
if(timer++ % 10 == 0) {
     // do stuff
}
I rest my case.
 

TheouAegis

Member
if --timer == 0 {

Or if you can assure that code doesn't necessarily run every step...

if !--timer {

The first will count down always, but only do anything if you actually set timer to a positive value (==0). The second would fire after the first step (<=0)
 
Top