timer

Started by Hadjiguru, Tue 02/03/2004 20:43:56

Previous topic - Next topic

Hadjiguru

is there a possibility to give the player-character a certain ability or an opportunity to do something for a defined space of time (e.g. for 30 seconds after drinking a magic drink)? maybe using the 'set timer'-function?

Darth Mandarb

That's exactly what you need to do.

(There've been a few topics about this already, you might want to search the forums first before asking questions!  Just FYI)

SetTimer(TIMER, AMMOUNT)
TIMER = number (1-100)
AMMOUNT = how many game cycles (40=1 sec)

So you could do:

SetTimer(1,12000) // sets timer 1 to 30 seconds

Then use code like this for whatever interaction(s) you want affected by the condition of the timer:

if (IsTimerExpired(1)!=1) { // meaning timer is NOT expired
code here;
}
else { // meaning timer HAS expired
code here;
}

Hope it helps!

~ d

Hadjiguru

hum, it doesnt work. i scripted:


if (IsTimerExpired (1)!=1 {//
my code here for the special ability
}

else {//
 code for NOT having the ability;
 }

but now the player-character has always the special ability - with AND without setting the timer before by drinking a magic drink.  

Darth Mandarb

When/Where are you setting the timer?

CB..

try this instead note the two == sign instead of the !=

if (IsTimerExpired (1)==1 {
///code here
}




///


im not sure what the diference between != and == amounts to  might handy to know if anyone can fill me in?

Gilbert

A ) is missing obviously...

if (IsTimerExpired (1)!=1) {
///code here
}


Please note, != and == are NOT the same thing, they're actually the OPPOSITE, != means not equals to, while == means equals to (when doing comparisons, not assignments).

CB..

#6
ah sorry fogot about the close bracket..

but i'm still confused about the !=


if you are using "!=" and that means "not equal to"  
then the statement reads as

if  is timer number one expired "not equal to"  "yes"

that means when the timer is not expired..  run the code?

is there a list  of all the available similar expressions?
for example whats the difference between "==" and just plain "="


//he he!! hang on i see where im loosing the plot

as a newbie i tend not to use timers with an "else" statment..

ie this sort of thing


if (IsTimerExpired(2)==1)
{if ((character[EGO].walking==0) && (character[DD].walking==0)){
MoveCharacter(DD, Random(320),Random(200));
}
SetTimer(2,200);
}    
if (character[EGO].walking!=1){
if (IsTimerExpired(3)==1)
{MoveCharacterPath(1, character[EGO].x, character[EGO].y);
SetTimer(3,200);}}


//which  might explain why it doesnt work very reliably..

i can see that a computer would prefer an either/ or option ?
is that right?

Ferret

= is assigning a value to a variable, == checks for comparisons.

So, a=1 would give a the value of one and checking for a==1 would check if a equals 1.

CB..

Many thanks Ferret , keep them coming..

Hadjiguru

i had to set three timers and now it works. but im not really satisfied with that. is there no easier way to enable a special feature for some seconds?

Scorpiorus

#10
You can type-in:

main global script
int special_ability;

function GiveAbility(int seconds) {
SetTimer(1, seconds*GetGameSpeed());
special_ability = 1;
}

function repeatedly_execute() {
if (IsTimerExpired(1)) special_ability = 0;
}

end of main global script
export special_ability;

script header
import int special_ability;
import function GiveAbility(int seconds);


From now whenever you want to enable the ability you type: GiveAbility(30); //gives ability for 30 seconds

And check the special_ability variable thorught the script, like:

function hotspot*() {
//hotspot: player interact hotspot
if (special_ability) {
   //your code if special ability is enabled
}
}

Hope it helps

~Cheers


CB..

#11
i dont know where i'd be without this forum!!
thanks Scorpious; the only thing as a newbie that i find lacking in the manual/tutorials (which are comprehensively brilliant otherwise) is the tendency to ommit telling the newbie exactly in which part of the game script to place each part of the code, i rely on the forum to accidently answer these questions for me!!..ill browse and search untill i stumble across an example then im away..thanks)


i spent hours on
experimenting with the my_counter tutorial before i sussed that i needed to put the

int my_counter = 0;

in the script header

seperate from the repeatedly execute section

//////then put in repeatedly execute//

if (my_counter == 600)
{my_counter = 0;}    
if (my_counter < 600)
{my_counter ++;}
if ((my_counter >= 0) && (my_counter < 200)) {if (character[DD].walking==0){
MoveCharacter(DD, Random(320),Random(200));}}
if (my_counter == 250) {if (character[DD].walking==0){
MoveCharacterPath(1, character[EGO].x, character[EGO].y);}}
if (my_counter == 450) {if (character[EGO].walking==1){
MoveCharacterPath(1, character[EGO].x, character[EGO].y);}}
if (my_counter == 599)
{DisplaySpeech(EGO, "1.");}


/////this was based on the in game help untitled tutorial pages..

reason i mention this is as Hadjiguru says using the timers can be fiddly and i wanted to create my own timers that i could use for multiple events and also for non blocking wait commands..



/
when yu are using the

function GiveAbility(int seconds) {
SetTimer(1, seconds*GetGameSpeed());
special_ability = 1;
}

function repeatedly_execute() {
if (IsTimerExpired(1)) special_ability = 0;
}


SetTimer
and IsTimerEpired entrys in the function
are they refering to a normal timer or does this give the special ability a timer function
is what is happening that yu have created timer(1) and renamed it to special_abilty?
meaning yu cant use Timer(1) for anything else...?

Scorpiorus

Quotethe only thing as a newbie that i find lacking in the manual/tutorials (which are comprehensively brilliant otherwise) is the tendency to ommit telling the newbie exactly in which part of the game script to place each part of the code
I'm with you here. I too think beginners scripting tutorial should probably tell more about the event-based approach, handler functions, etc. I wanted to write one which tells about that stuff but just can't get to it due to the lack of spare time.

Quotei spent hours on
experimenting with the my_counter tutorial before i sussed that i needed to put the

int my_counter = 0;

in the script header

seperate from the repeatedly execute section
yep, but better place it at the top of the main global script. Putting variable in the script header does as if you'd place the variable into every single room script (as well as to the main global script). But all these variables would be different (though have identical names).


Quoteare they refering to a normal timer or does this give the special ability a timer function
is what is happening that yu have created timer(1) and renamed it to special_abilty?
meaning yu cant use Timer(1) for anything else...?
The reason I've made a separate variable because IsTimerExpired(1) returns 0 even if there was no timer set yet. That is why you can't put:

if (IsTimerExpired (1) !=1 ) { // i.e. IsTimerExpired (1) == 0
//special ability is enabled
}

as the special ability would be enabled almost always in that case.

Timer 1 can be used for something else as along as the special ability is not enabled at the same time :)

~Cheers

CB..

many thanks Scorpiorus
wil move the int my_counter =0 into
the main global script thanks for clearing that up...it's hugely frustrating as a newbie to read these mouth watering sections of code in the manual only to be completely left without any clue as to even the need to place the diferent parts in specific parts of the code (let alone which ones and where..it really doeant even mention this.....i had actual given up on ever understanding this stuff...only reading the forums finally clued me in to the idea of what to do...)

thanks for the timer explanation aswell...that helps me to get a handle on things
cheers again!


SMF spam blocked by CleanTalk