Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   New Scripting Engine (GS2) (https://forums.graalonline.com/forums/forumdisplay.php?f=153)
-   -   Countdown Timer (https://forums.graalonline.com/forums/showthread.php?t=134265168)

Gunderak 12-05-2011 08:57 AM

Countdown Timer
 
This is just a simple example of a countdown timer.
First off you will need to place this code into a database NPC on your server.
So go ahead and click add and name it whatever you wish and paste the code into the NPC's script.
PHP Code:

function onCreated() {
  
this.daysleft "10";
  
this.hoursleft "10";
  
this.minutesleft "10";
  
this.secondsleft "10";
  
onTimeout();
}

function 
onTimeout() {
  
this.secondsleft -= 1;
  if (
this.secondsleft == || this.secondsleft 0) {
    
this.minutesleft -= 1;
    
this.secondsleft 60;
  }
  if (
this.minutesleft == || this.minutesleft 0) {
    
this.hoursleft -= 1;
    
this.minutesleft 60;
  }
  if (
this.hoursleft == || this.hoursleft 0) {
    
this.daysleft -= 1;
    
this.hoursleft 24;
  }
  
settimer(1);
}


public function 
GetTimeLeft() {
  return 
"Days: " this.daysleft " Hours: " this.hoursleft " Minutes: " this.minutesleft " Seconds: " this.secondsleft;


To get the time simple place this code into a level NPC.
PHP Code:

function onCreated() {
  
onTimeout();
}

function 
onTimeout() {
  
this.chat NPCNAME.GetTimeLeft();
  
settimer(0.5);


But substiture NPCNAME for whatever you named the database NPC.
-Enjoy

Also for the smart ones out there, I am thinking of making a weapon where a player can set their own countdown.
The only problem so far is how would it countdown if the player is offline?
Of course you could just make a dozen or so server variables but that's a bad way.
Would making a timevar and setting it to a clientr variable work?
But even if it would that would only be good for counting down seconds..

cbk1994 12-05-2011 12:33 PM

I've told you before that you shouldn't be putting numbers in quotes if you intend to manipulate them like numbers. Also, you should really be keeping one count (seconds left) and calculating number of days, hours, minutes, and seconds from it.

Gunderak 12-05-2011 01:06 PM

Hmm, I probably could just keep one count and divide it.
And the reason I put them in quotation marks is because when assigning variables values if it's not a number you have to encase it in them, and it's just a habit now.
And it's not like it's going to affect the scripts processing time too dramaticly..

cbk1994 12-05-2011 01:23 PM

Quote:

Originally Posted by Gunderak (Post 1676459)
And the reason I put them in quotation marks is because when assigning variables values if it's not a number you have to encase it in them, and it's just a habit now.
And it's not like it's going to affect the scripts processing time too dramaticly..

It's not about processing time, it's mostly about readability. Trust me on this. It makes it harder to look at a complicated script and understand it if you do that. Any scripter is going to assume you won't be manipulating it like it's a number since you've defined it as a string. You're also adding unnecessary coercion, which itself is a good enough reason not to do it.

This isn't code gallery material with an error like that in it. It teaches others very bad practices.

Gunderak 12-05-2011 03:08 PM

Well I'm going to have to disagree with you there.
"Does not make it harder to read".
If anything it defines what you are assigning the variable to clearer.
And it does not change the way the script functions at all.

cbk1994 12-05-2011 11:17 PM

Quote:

Originally Posted by Gunderak (Post 1676465)
Well I'm going to have to disagree with you there.
"Does not make it harder to read".
If anything it defines what you are assigning the variable to clearer.
And it does not change the way the script functions at all.

You're just wrong. I don't want to sound like I'm being high-and-mighty, but if you keep scripting and eventually become good at it, you're going to look back at this and facepalm.

Quotes are for strings, not numbers. You can't keep ignoring the advice people give you and expect them to keep giving you advice.

Skyld 12-05-2011 11:24 PM

Quote:

Originally Posted by Gunderak (Post 1676459)
And the reason I put them in quotation marks is because when assigning variables values if it's not a number you have to encase it in them, and it's just a habit now.

That is not a good reason to do that. A string literal has "quotemarks", but a numeric literal does not. The script engine, whilst converting from a string to a number, does not always behave how you expect, and this sort of undefined behaviour can make scripts very confusing to debug when they go wrong. Long story short: Just don't do it.

I'm going to move this thread out of the Code Gallery for now, not just because of this, but also because I don't really feel like the code is particularly elegant, robust or educationally useful either.

Gunderak 12-06-2011 08:47 AM

Yay now I can't read the replies. thanks! -.-
And "one day if I become good at scripting".
I'm not as good as you at it but I'm not as bad as your making me sound.

Hezzy002 12-06-2011 03:59 PM

Quote:

Originally Posted by Gunderak (Post 1676627)
Yay now I can't read the replies. thanks! -.-
And "one day if I become good at scripting".
I'm not as good as you at it but I'm not as bad as your making me sound.

Ehhh, you're like a 4/10 on the ladder and you don't take well to people telling you to do something different when you're wrong.

callimuc 12-06-2011 04:16 PM

Quote:

Originally Posted by Gunderak (Post 1676627)
Yay now I can't read the replies.

Just imagine some bad stuff.

salesman 12-06-2011 04:23 PM

Quote:

Originally Posted by Hezzy002 (Post 1676628)
Ehhh, you're like a 4/10 on the ladder and you don't take well to people telling you to do something different when you're wrong.

10/10 being Jon Skeet or...?

Hezzy002 12-06-2011 04:47 PM

Quote:

Originally Posted by salesman (Post 1676630)
10/10 being Jon Skeet or...?

yes, probably

Emera 12-06-2011 06:10 PM

Quote:

Originally Posted by Gunderak (Post 1676627)
Yay now I can't read the replies. thanks! -.-
And "one day if I become good at scripting".
I'm not as good as you at it but I'm not as bad as your making me sound.

Nobody is making you sound bad :O
He's just trying to cover some factors you didn't pay attention to while coding this. He's also trying to say that, at the moment, it's not really that much use to anybody. Maybe if you go back and clean it up, add some features that could be some use to the code gallery and come back with it. That's all he's trying to say. Thanks for making the effort though.

Skyld 12-06-2011 09:30 PM

Quote:

Originally Posted by Gunderak (Post 1676627)
I'm not as good as you at it but I'm not as bad as your making me sound.

I don't mean to put you down, but I explained already good reasons why not to put quotemarks around integers, not just for optimisation, but also for clarity. You need to learn from things like this. Doing so will make you a better scripter.

The main reason that I moved the thread out of the code gallery is because I feel that is not a good solution; you are dealing with four variables for a single timestamp, which I don't think is elegant, and not seemingly handling what happens when you start going into negative days. Maybe that introduces potential race conditions later on.

You can make this much more reliable by using a single variable - the number of seconds - and printing the output format using some time formatting functions.

Gunderak 12-07-2011 01:38 AM

Fine, if it's what everyone wants il stop using quotation marks for numbers..
I will make a GUI for setting the countdown and make it a single number and then re post in code gallery.

Hezzy002 12-07-2011 01:41 AM

setTimer is frame-based, not time-based.

Gunderak 12-07-2011 02:53 AM

setTimer works in a DBNPC, are you sure it's frame based?

DustyPorViva 12-07-2011 02:58 AM

Quote:

Originally Posted by Hezzy002 (Post 1676666)
setTimer is frame-based, not time-based.

It is time-based... a timer of 0.05 triggers every 0.05 seconds. Graal is 20fps, meaning it's running at 20 frames a second, so 1 second / 20 frames = 0.05, or the lowest timeout possible.

fowlplay4 12-07-2011 03:08 AM

Quote:

Originally Posted by Gunderak (Post 1676676)
setTimer works in a DBNPC, are you sure it's frame based?

Frame-based in the sense that the timeout value decreases by (1 / Max FPS). On the server-side the Max FPS is 10, and on the client-side it's 20 (Like Dusty mentioned).

That is why it's useless to set timeouts lower than 0.1 on the server-side and 0.05 on the client-side. (Depending on the application you should be using the highest or no timeout at all if possible).

Play with this script:

PHP Code:

function onCreated() {
  
this.test timevar2;
  
this.speed 0.05;
  
setTimer(this.speed);
}

function 
onTimeout() {
  echo(
timevar2 this.test);
  
this.test timevar2;
  
setTimer(this.speed);


Both 0.05 and 0.1 will return similar values.

Gunderak 12-07-2011 06:28 AM

Ah I see.
So it's not actually controlled by when the game enters a new frame.
I was confused for a bit there lol.

Hezzy002 12-07-2011 04:48 PM

Quote:

Originally Posted by DustyPorViva (Post 1676677)
It is time-based... a timer of 0.05 triggers every 0.05 seconds. Graal is 20fps, meaning it's running at 20 frames a second, so 1 second / 20 frames = 0.05, or the lowest timeout possible.

I don't mean frame-based in the sense that you input frame values, I mean that it doesn't compare against the computer's clock; it just subtracts .05 from a value each frame and runs the function when it hits zero, and thus is less reliable for anything that's time-based instead of frame-based.

callimuc 12-07-2011 05:27 PM

Maybe itīs just me, but why not use this check?
PHP Code:

function onTimeout() {
  if (
this.secondsleft <= 0) {
    
//stuff
  
}
  
settimer(1);



scriptless 12-26-2011 08:20 PM

Question, you have 2 timeouts. One for actually counting down, and one for showing the time. Correct me if I am wrong but wouldn't it be better to just calculate the time with timevar/timevar2 in the timeout to show the variable?

Pelikano 01-24-2012 04:34 PM

Quote:

Originally Posted by scriptless (Post 1679556)
Question, you have 2 timeouts. One for actually counting down, and one for showing the time. Correct me if I am wrong but wouldn't it be better to just calculate the time with timevar/timevar2 in the timeout to show the variable?

Didn't read through the posts, but you can't "count time" inside a timeout anyway, because if you set the timeout to 0.1, it doesn't mean that it will actually be called after 0.1 seconds, it may be called after 0.5 seconds depending on the performance...

Crow 01-24-2012 04:39 PM

Quote:

Originally Posted by Pelikano (Post 1682610)
Didn't read through the posts, but you can't "count time" inside a timeout anyway, because if you set the timeout to 0.1, it doesn't mean that it will actually be called after 0.1 seconds, it may be called after 0.5 seconds depending on the performance...

If you're setting the timer to 0.1, it will be called approximately 0.1 seconds later. You can count time with the use of a timeout, but the longer you are doing this, the less accurate it will get.

Pelikano 01-24-2012 06:57 PM

Quote:

Originally Posted by Crow (Post 1682612)
If you're setting the timer to 0.1, it will be called approximately 0.1 seconds later. You can count time with the use of a timeout, but the longer you are doing this, the less accurate it will get.

it will be called @ the next frame when your timer time is over, which means that if a frame takes 0.5 second due to lag or anything your counter will be wrong

Crow 01-24-2012 08:20 PM

Quote:

Originally Posted by Pelikano (Post 1682615)
it will be called @ the next frame when your timer time is over, which means that if a frame takes 0.5 second due to lag or anything your counter will be wrong

Duh.

scriptless 01-24-2012 09:56 PM

Quote:

Originally Posted by Pelikano (Post 1682610)
Didn't read through the posts, but you can't "count time" inside a timeout anyway, because if you set the timeout to 0.1, it doesn't mean that it will actually be called after 0.1 seconds, it may be called after 0.5 seconds depending on the performance...

You can, but it's not as accurate. That is why I asked, would it not be better to just use timevar/timevar2?

Mark Sir Link 01-24-2012 11:49 PM

Quote:

Originally Posted by Gunderak (Post 1676465)
Well I'm going to have to disagree with you there.
"Does not make it harder to read".
If anything it defines what you are assigning the variable to clearer.
And it does not change the way the script functions at all.

is this real life


All times are GMT +2. The time now is 02:35 AM.

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