Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Trying to make a Timer (https://forums.graalonline.com/forums/showthread.php?t=134266704)

Stowen 06-22-2012 11:54 PM

Trying to make a Timer
 
Hello. I'm trying to make a countdown timer, but it doesn't seem to be working. I'm also not sure of the best way to go about it.

Here is what I have so far:

PHP Code:

//#CLIENTSIDE
function onCreated() {
  
this.timer == 15;
  
this.chat "Countdown";
}

function 
onPlayerChats() {
  if (
player.chat == "/count") {
    
this.timer == 15;
    
onTimeOut();
  }
}

function 
onTimeOut() {
  
this.timer 1;
  
this.chat this.timer;
  
setTimer(1);



ffcmike 06-23-2012 12:02 AM

You have a double equals sign (which are for comparing values) in your timer assignments.

Reducing the timer should also either be this.timer --; or this.timer -= 1;

Crow 06-23-2012 12:05 AM

In your onCreated() function, you are using the equality operator (==) to assign a value. This will not work. When assigning values to variables, use a single equality sign. This also applies to your onPlayerChats() function. Also, since you're setting this.timer (you might want to change the name as well, I bet that's reserved) in onPlayerChats(), it seems a bit redundant to also do that in onCreated().

Next, you'll want to decrement a variable by using either of these two methods:
PHP Code:

this.var--;
this.var -= 1

What you're doing in your timeout will not work. Finally, you should probably check inside your timeout if the countdown has reached zero and, if it has, do not continue the timeout.

DustyPorViva 06-23-2012 12:12 AM

PHP Code:

//#CLIENTSIDE
function onCreated() {
  
// I'd be wary of using a variable named this.timer. This.timer might be reserved for actual timeouts
  
  // Initiate a new variable that logs the current time
  // We will later use this to keep track of the countdown
  // via offsetting it. If this.countdowntime > timevar2, then assume it is counting down
  
this.countdowntime timevar2;
}

function 
onPlayerChats() {
  
// Call the function to start the countdown, let it know you want it to be 15 seconds
  
if (player.chat == "/count"Countdown(15);
}

function 
Countdown(t) {
  
// Take timevar2 as a reference point and add the time(t) provided to it. This will let
  // the script know there is now time + t amount of time for the countdown
  
this.countdowntime timevar2 t;

  
// As long as timevar2 is less than this.countdowntime, then there is a countdown occuring
  // Use while to create a loop(for updating). This is similar to a timeout, as it will
  // continue to execute the script as long as the statement is true
  
while (timevar2 this.countdowntime) {
    
// Do some basic math to find the difference in time(the countdown)
    // Also round it with int(x + .5) or else you'll get large floating values like 12.23463452342345356463
    
this.chat int(this.countdowntime timevar2 .5);

    
sleep(.5); // No need to update the loop more frequently than this since we're providing decrements in seconds
  
}
 
  
this.chat "Countdown complete!";



Stowen 06-23-2012 02:49 AM

Thank you guys! :)


All times are GMT +2. The time now is 08:26 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.