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.


All times are GMT +2. The time now is 01:02 PM.

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