Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > New Scripting Engine (GS2)
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-05-2011, 08:57 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Send a message via Skype™ to Gunderak
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.
Graal Script 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.
Graal Script 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..
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #2  
Old 12-05-2011, 12:33 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
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.
__________________
Reply With Quote
  #3  
Old 12-05-2011, 01:06 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Send a message via Skype™ to Gunderak
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..
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #4  
Old 12-05-2011, 01:23 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Gunderak View Post
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.
__________________
Reply With Quote
  #5  
Old 12-05-2011, 03:08 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Send a message via Skype™ to Gunderak
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.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #6  
Old 12-05-2011, 11:17 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Gunderak View Post
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.
__________________
Reply With Quote
  #7  
Old 12-05-2011, 11:24 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by Gunderak View Post
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.
__________________
Skyld
Reply With Quote
  #8  
Old 12-06-2011, 08:47 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Send a message via Skype™ to Gunderak
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.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #9  
Old 12-06-2011, 03:59 PM
Hezzy002 Hezzy002 is offline
Registered User
Join Date: Jul 2011
Posts: 247
Hezzy002 is a jewel in the roughHezzy002 is a jewel in the rough
Quote:
Originally Posted by Gunderak View Post
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.
Reply With Quote
  #10  
Old 12-06-2011, 04:16 PM
callimuc callimuc is offline
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
Quote:
Originally Posted by Gunderak View Post
Yay now I can't read the replies.
Just imagine some bad stuff.
__________________
MEEP!
Reply With Quote
  #11  
Old 12-06-2011, 04:23 PM
salesman salesman is offline
Finger lickin' good.
salesman's Avatar
Join Date: Nov 2008
Location: Colorado
Posts: 1,865
salesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud of
Quote:
Originally Posted by Hezzy002 View Post
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...?
__________________
Reply With Quote
  #12  
Old 12-06-2011, 04:47 PM
Hezzy002 Hezzy002 is offline
Registered User
Join Date: Jul 2011
Posts: 247
Hezzy002 is a jewel in the roughHezzy002 is a jewel in the rough
Quote:
Originally Posted by salesman View Post
10/10 being Jon Skeet or...?
yes, probably
Reply With Quote
  #13  
Old 12-06-2011, 06:10 PM
Emera Emera is offline
Delterian Hybrid
Emera's Avatar
Join Date: Mar 2011
Location: Newcastle Upon-Tyne
Posts: 1,704
Emera is a jewel in the roughEmera is a jewel in the rough
Quote:
Originally Posted by Gunderak View Post
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.
Reply With Quote
  #14  
Old 12-06-2011, 09:30 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by Gunderak View Post
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.
__________________
Skyld
Reply With Quote
  #15  
Old 12-07-2011, 01:38 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Send a message via Skype™ to Gunderak
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.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:55 AM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (C) 1998-2008 Linux cyberjoueurs All Rights Reserved.