confused by this

W

Wild_West

Guest
this is just a simple script I'm trying to use to make a variable of any kind count down as I've had success using this method in place of alarms because those can get confusing.
The problem is from what I see the script should be working but the timers I set aren't counting down.
I've made many other scripts up until now but those all used values from other objects except for this one I made for attacking, which has no variables specific to my player inserted as those would of course come upon calling the script.
So yeah just confused as to why the attacking script will work but this timer one won't.
Thanks for any advice. My version of Gm:studio is standard 1.4

///set_loop_timer( typeOfTimer, lowestTime, resetTime, decayRate);

if(argument0 <= argument1){ argument0 = argument2; }

if(argument0 >= argument2){ argument0 = argument2; }

argument0 -= argument3;
 
A

Aura

Guest
You can't do that. Because the arguments hold the value of the variables passed to the script, not a pointer to the variable itself. You'll have to return a value from the script and catch it in the timer variable.
 

johnwo

Member
Arguments are essentialy copies of the variable you supply, unless it's a reference.

Cheers!

EDIT:
Got ninja'd...
 
J

Jaqueta

Guest
I'm confused by your question xD
What are you exactly trying to do?
Well, as they said above, arguments are a simple container, you can use the "return" function to well... RETURN the value you want.

Example: return argument0;

And then you use your script like this: YourVar=set_loop_timer....
 

hippyman

Member
One way around this issue would be to use arrays for this and then you can pass the array in and change that directly in the script.

In scripts you can reference arrays rather than copy them with the array accessor (@)

Add one more argument to your script and then make a timer array similar to how alarms work.
Code:
///set_loop_timer( timerArray, timerIndex, lowestTime, resetTime, decayRate);

if(argument0[@argument1] <= argument2){ argument0[@argument1] = argument3; }

if(argument0[@argument1] >= argument3){ argument0[@argument1] = argument3; }

argument0[@argument1] -= argument4;
Now you can call your script like this

Code:
timer[10] = 0;//create ten timers
set_loop_timer(timer,0,0,100,5);//updates timer[0]
set_loop_timer(timer,5,0,100,5);//updates timer[5]
...etc
 

Yaazarai

Member
I'm gonna add onto what the previous two said. You can't pass a variable to a script as an argument, only the value the variable holds. UNLESS you're using arrays or dynamic memory. Arrays can be passed by reference and dynamic memory (ds-lists, ds-grids, buffers, etc.) can be passed by ID. A good example of this actually is timers.

Arrays are the only thing in GML that can be passed by reference.
 
W

Wild_West

Guest
One way around this issue would be to use arrays for this and then you can pass the array in and change that directly in the script.

In scripts you can reference arrays rather than copy them with the array accessor (@)

Add one more argument to your script and then make a timer array similar to how alarms work.
Code:
///set_loop_timer( timerArray, timerIndex, lowestTime, resetTime, decayRate);

if(argument0[@argument1] <= argument2){ argument0[@argument1] = argument3; }

if(argument0[@argument1] >= argument3){ argument0[@argument1] = argument3; }

argument0[@argument1] -= argument4;
Now you can call your script like this

Code:
timer[10] = 0;//create ten timers
set_loop_timer(timer,0,0,100,5);//updates timer[0]
set_loop_timer(timer,5,0,100,5);//updates timer[5]
...etc
That's a good idea I'll use it, thanks ^
 
W

Wild_West

Guest
I'm gonna add onto what the previous two said. You can't pass a variable to a script as an argument, only the value the variable holds. UNLESS you're using arrays or dynamic memory. Arrays can be passed by reference and dynamic memory (ds-lists, ds-grids, buffers, etc.) can be passed by ID. A good example of this actually is timers.

Arrays are the only thing in GML that can be passed by reference.
I haven't gotten as far as using the dynamic lists yet so that makes sense but like I said in the post my other script for enabling damage to an enemy and changing the player sprite, creating a damage object to hit them with works fine and nothing except the object's hp, guard and attack are in that.
passing in the arguments for sprite index, an instance to create, and all that works fine.

///enable_dmg_enemy( key, sprite, animateSpd, object, x, y, target, attackImage);

//change the sprite based on the given key and create the instance given
if(keyboard_check_pressed(argument0))
{
sprite_index = argument1;
image_speed = argument2
instance_create(x,y,argument3);
}

if(place_meeting(argument4,argument5,argument6) and sprite_index = argument1 and image_index = argument7)
{
argument6.hp -= atk -argument6.grd;
}

so is the difference between this working and my countdown script not working that the values in here are dynamic memory?
 
W

Wild_West

Guest
I'm confused by your question xD
What are you exactly trying to do?
Well, as they said above, arguments are a simple container, you can use the "return" function to well... RETURN the value you want.

Example: return argument0;

And then you use your script like this: YourVar=set_loop_timer....
yeah that's really all I'm trying to do is make something count to 0 so something can happen and then reset to 100 or whatever and repeat.
It's just the way to use return always confused me when I tried to use it because nothing happened.

I'm sorry for being so dumb about this but I'm new to making my own scripts so, ya know trial and error
 
W

Wild_West

Guest
Arguments are essentialy copies of the variable you supply, unless it's a reference.

Cheers!

EDIT:
Got ninja'd...
so the copy would of course be in the calling object's create ,and the reference is defined as what? The variables you set local in the actual script?
 
W

Wild_West

Guest
You can't do that. Because the arguments hold the value of the variables passed to the script, not a pointer to the variable itself. You'll have to return a value from the script and catch it in the timer variable.
Okay I see. after reading this I tried doing that and it sort of worked.

///Script----------------------------------------------------------------------
timerLoop = argument0;

if(timerLoop <= argument1){ timerLoop = argument2; }

if(timerLoop >= argument2){ timerLoop = argument2; }

timerLoop -= argument3;

return(timerLoop);
-------------------------------------------------------------------------------
the timer was set where I put it

///In enemy step event-----------------------------------------------------------------

musicTimer = set_loop_timer( 100, 0, 100)
--------------------------------------------------------------------------------

But it only counted down by one then just stops at 99
and I got an error when I tried to draw out another timer using that same method.

so still lost here.
 

johnwo

Member
Do something like this for your timers:
This is all pseudo-code, but should function with little to no modification.

Create an object.

In the create event you put:
Code:
alarmCount = 32;

for (var i=0;i<alarmCount;i++) {
c_alarm[i] = -1;
c_alarm_trig[i] = false;
}
In the step event you put:
Code:
for (var i=0;i<alarmCount;i++) {
c_alarm[i] = max(c_alarm[i]-1,-1);
if c_alarm[i] == 0 then c_alarm_trig[i] = true;
}
To set an alarm you'd simply do:
Code:
c_alarm[0..alarmCount] = someVar;
To check if an alarm was triggered you'd simply do:
Code:
if c_alarm_trig[0..alarmCount] == 0 then {
// Do something
c_alarm_trig[0] = false;
}
To make this even more effective, you could make a script like this, call it something like scr_alarms_triggered:
Code:
for (var i=0;i<alarmCount;i++) {
if c_alarm_trig[i] then return i;
}
return -1;
Then, in the object's step event:
Code:
var alarm_trig = scr_alarms_triggered();
while alarm_trig != -1 {
switch (alarm_trig) {
case 0:
// Alarm 0 went off...
break;
case 1:
// Alarm 1 went off...
break;
case 2:
// Alarm 2 went off...
break;
// ...
case 31:
// Alarm 31 went off...
break;
default:
break;
}
c_alarm_trig[alarm_trig] = false;
alarm_trig = scr_alarms_triggered();
}
Alarms might seem confusing, but they, like most things, really aren't.
 
W

Wild_West

Guest
Do something like this for your timers:

Alarms might seem confusing, but they, like most things, really aren't.
I still think I'd rather stick with a basic variable countdown, but I appreciate the help.
I know I'm probably limiting myself but if I spend anymore time trying to redo what already works for me I'll never make any progress on this game.

It already took me a year and a half to build the world the game is based off of lol
 
Top