Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411469 Posts in 69368 Topics- by 58422 Members - Latest Member: daffodil_dev

April 23, 2024, 05:48:52 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Random alarms to different instances
Pages: [1]
Print
Author Topic: Random alarms to different instances  (Read 638 times)
Yale
Level 0
*


View Profile
« on: October 01, 2015, 07:38:00 AM »

What I Need:
I'm making a game where I've got a team of players on the pitch and they all fall asleep at different intervals, but I'm having trouble setting up the timer for this to work (read: what I've got ain't doing what I think it should).

So I'm trying to set up a system where each instance of oPlayer has a different countdown attached to it, which then tick down and reset, e.g. instance1 starts with 12seconds, instance2 starts with 10 seconds etc. 5 different timers onto 5 instances

What I've Got:
For the oPlayer Object I've got an alarm which runs the script below 5 times, once for each player (the script is basically a state machine, ticking down from the initial value in the "awake" state, then switching to the "asleep" state and ticking down again. This keeps occuring, with the timing getting faster till it switches between being awake for 3 seconds and asleep for 3 seconds)
Code:
//SCRIPT NAME: sNarco
//argument0 = cdownasleep
//argument1 = cdownawake
//argument2 = cdslp_tracker
//argument3 = cdawk_tracker
//argument4 = alarm[n]
//argument5 = awake

//while the state machine is awake
if argument5=1
{
    //countdown to being asleep
    argument0-=1;
    //when timer finishes, falls asleep
    if argument0=0
    {
        argument5=0;
        //the time that it resets to gets bigger up to 3 (starting from 1)
        if argument3<2
        {
            argument1=ceil(random_range(argument3,argument3+1));
        }
        else
        {
            argument1=3;
        }
        argument3=argument1;
    }
}
else
{
    //countdown to being awake
    argument1-=1;
    //when timer finishes, awaken
    if argument1=0
    { 
        //the time that it resets to gets smaller down to 3 (starting from the random initial value)
        if argument2>5
        {
            argument0=floor(random_range(argument2-2,argument2));
        }
        else
        {
            argument0=3
        }
        argument2=argument0;
        argument5=1;
    }
}
//reset the alarm
argument4=room_speed

So I've set up an array for arguments 0,1,2,3,5 (cslp[0...4], cawk[0...4], cslp_t[0...4], cawk_t[0...4], awake[0...4]) and argument 4 is for each alarm, so the code for alarm[0] looks like this:

Code:
for (i=0; i<instance_number(oPlayer01); i+=1)
script_excecute(sNarco,cslp[i],cawk[i],cslp_t[i],cawk_t[i],alarm[0],awake[i]);

The arrays in the create event for the oPlayer object look like this:
Code:
//initialise arrays
for (n=instance_number(oPlayer01);n>-1;n-=1)
{
//argument0
//initial value for the timer to countdown from
cslp[n]=floor(random_range(7,12));
//argument1
//these values get reset in the alarm itself before being used, so this just initialises the array
cawk[n]=1;
//argument2
//tracker for the sleep countdown
cslp_t[n]=cslp[n];
//argument3
//tracker for the awake countdown
cawk_t[n]=1;
//argument5
//set all the machines to awake
awake[n]=1;
n-=1;
}
//set up alarm (argument4)
alarm[0]=room_speed;

Then in the draw event for the oPlayer Object ive got a loop that SHOULD draw the timer for each alarm separately on each player:
Code:
//draw alarms
for (i=0; i<instance_number(oPlayer01); i+=1)
{
        if awake[i]=1
        {
            //if it's awake then draw that time in blue (should be a number between 3-12 incl.)
            draw_set_color(c_blue)
            draw_text(instance_find(oPlayer01,i).x,instance_find(oPlayer01,i).y-45,cslp[i]);
        }
        else
        {
            //if it's asleep then draw that time in green (should be a number between
            draw_set_color(c_green);
            draw_text(instance_find(oPlayer01,i).x,instance_find(oPlayer01,i).y-45,cawk[i]);
        }
}

but instead it just seems to draw them all on each player, overlaying them ontop of each other while not ticking down at all:


Now I don't know whats wrong, if it's the way I'm calling the arguments, if its the arrays, if its the way I'm drawing the text, or do I need separate alarms? I don't think I'm calling the right variables to draw? Do I need to put a "" return <expression> "" into the script? I know the state machine part works, I had it as the code for a single alarm and it ticked down just fine.

I don't even know if this is the best way of doing this, setting up different alarms that all count down separately, if it's not then please enlighten me.

Any help would be much appreciated, I've attached the .gmz for this here (google drive link). Thanks in advance!
Logged
mokesmoe
Level 10
*****



View Profile WWW
« Reply #1 on: October 01, 2015, 11:54:42 PM »

The argument variables in a script are temporary variables that hold the values passed into the script. Changing them does not affect the variables you used when calling the script. I assume that's the only place you would be using that script, so you could just put it in the alarm[0] code directly and not use a script, or pass the i value and use the variables directly.

"return" would not help you here. What return does it selects the value that gets passed to the variable when you do "var = script(...)".

Another thing that might help is to store the ids of the 5 instances in an array so you don't have to use instance_find every time you want to access one. You could either do "var[a] = instance_create(...)" 5 times, or if you want to place them in the room editor manually "var[a] = instance_find(...,a)"

If all that code is in the player object, and the player object is the object that there are 5 of, then that would mean that all 5 objects are running the code for all 5 instances. If this is all in a separate object than this works, but if it's in the objects themselves they only need to run the code for themselves. You will need some sort of external control to set the timers to different numbers if you don't want to use randomness though. (If you are placing them in the room editor you could use the creation code.)

I think what you might not understand here is that every instance of the same object will run code and store variables separately.

Also you can just use sNarco(...) instead of script_execute(sNarco,...)
Logged
Yale
Level 0
*


View Profile
« Reply #2 on: October 02, 2015, 01:08:46 AM »

Ahh I see, thank you very much! I've put it all into a control object and it all works, thanks!

If I wanted to put it into the player object how would I get it to only run code in each instance? 

Thanks again
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic