Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 07-30-2011, 06:46 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Voting Npc

Basically iv scripted an npc which you can vote with.
its pretty simple but i thought id share it.
although it might not be such a great npc to you, to me its a milestone xD

So far all it has is /vote and /unvote functionality.
id like to make it have Votes: whatever in this.chat,
but i cant figure out how to do that without skrewing it up.
anyway enjoy the script, use it however you wish =D

PHP Code:
function onCreated(){
this.chat "0";
}
function 
onPlayerchats(){
if(
player.chat == "/vote"){
if(
clientr.voted=1){
return;
}
player.chat "Voted!";
this.chat += 1;
clientr.voted=1;
}
if(
player.chat == "/unvote"){
if(
clientr.voted=0){
return;
}
if(
clientr.voted=1){
clientr.voted=0;
player.chat "Unvoted!";
this.chat -= 1;
}
}
}
function 
onPlayertouchsme(){
say2("If You Like The New Menu Gui#bSay /vote To Vote Yes!#bYou Can Write /unvote To Cancel!");

__________________

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 07-30-2011, 07:00 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Here's some problems with your script:
  • It's not styled properly.
  • You're using an assignment instead of a comparison in your if statements.
  • You're using chat to store the votes.
  • Your script can break easily, and votes will reset if the NPC goes to sleep and onCreated gets called, and anyone who voted will have to unvote which will cause your survey values to go into the negatives.
  • It's not documented at all.

Solutions
  • Indent your code. See Code Gallery example. If you aren't sure you're styling it right use http://jsbeautifier.org
  • if (variable == value) not if (variable = value).
  • Use a server or a DB variable to store the votes, and update your NPC to display it in chat. I.e: server.survey_votes
  • Track a Survey ID, and only allow them to vote it their clientr.voted isn't the current Survey's ID. I.e: server.survey_id
  • Document and place comments in your code. See Code Gallery example.

Quote:
Originally Posted by fowlplay4
How to style your script properly with jsbeautifer.org

Copy-paste your code into the code text area.

Settings:
- Indent with 2 spaces
- Braces with control statement
- Un-check all Checkboxes

Click 'Beautify'
Here's an example of basic Code Gallery post: http://forums.graalonline.com/forums....php?p=1660882
__________________
Quote:
Reply With Quote
  #3  
Old 07-30-2011, 07:17 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
wow didnt realise there were so many problems with it :$
so id have to store it server side your saying?
thatd be handy then if the npc server goes, it keeps its value.
then i could do somthing like..
this.chat = "Votes: "@server.survey_votes;
how do i store things to the server instead of to the npc?
thanks for your feedback i really appriciate it.
thats the only way im gonna learn, by people telling me what im doing wrong xD
__________________

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 07-30-2011, 07:38 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Quote:
Originally Posted by Gunderak View Post
wow didnt realise there were so many problems with it :$
so id have to store it server side your saying?
thatd be handy then if the npc server goes, it keeps its value.
then i could do somthing like..
this.chat = "Votes: "@server.survey_votes;
how do i store things to the server instead of to the npc?
thanks for your feedback i really appriciate it.
thats the only way im gonna learn, by people telling me what im doing wrong xD
Just like any other variable.

server.survey_votes++;

You can view, and edit server variables on the server-side.
__________________
Quote:
Reply With Quote
  #5  
Old 07-30-2011, 07:46 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
ah ok thanks for your help man.
il fix the script up xD
__________________

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 07-30-2011, 08:55 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Update.
Here is the latest new and improved version.
Thanks fowlplay for your help.

PHP Code:
function onCreated() {
  
this.chat "Votes: "@server.votes;
  
timeout 0.1;
}

function 
onTimeout() {
  
onCreated();
}

function 
onPlayerchats() {
  if (
player.chat == "/vote") {
    if (
clientr.voted 1) {
      return;
    }
    
player.chat "Voted!";
    
server.votes += 1;
    
clientr.voted 1;
  }
  if (
player.chat == "/unvote") {
    if (
clientr.voted 0) {
      return;
    }
    if (
clientr.voted 1) {
      
clientr.voted 0;
      
player.chat "Unvoted!";
      
server.votes -= 1;
    }
  }
}

function 
onPlayertouchsme() {
  
say2("If You Like The New Menu Gui#bSay /vote To Vote Yes!#bYou Can Write /unvote To Cancel!");

__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #7  
Old 07-30-2011, 09:26 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Quote:
Originally Posted by Gunderak View Post
Update.
Here is the latest new and improved version.
Thanks fowlplay for your help.

code...
You're still using an assignment instead of a comparison in your if statements. I.e:

if (clientr.voted = 0) {

should be

if (clientr.voted == 0) {

You also don't need to use a timeout at all. You only need to update the chat when the npc is created, and when a player votes. You can write a function called updateChat() to take care of that for you.

PHP Code:
function updateChat() {
  
this.chat "Votes: " server.votes;

You should also avoid using returns to stop the script from executing further, it can sometimes make your script difficult to follow.

Re-factored:

PHP Code:
function onCreated() { 
  
updateChat();


function 
onPlayerchats() { 
  if (
player.chat == "/vote") { 
    if (
clientr.voted == 0) {
      
player.chat "Voted!"
      
server.votes += 1
      
clientr.voted 1;
      
updateChat();
    } else {
      
player.chat "You've already voted!";
    }
  } 
  else if (
player.chat == "/unvote") { 
    if (
clientr.voted == 1) { 
      
clientr.voted 0
      
player.chat "Unvoted!"
      
server.votes -= 1;
      
updateChat();
    } else {
      
player.chat "You haven't voted yet!";
    }
  } 
}

function 
updateChat() {
  
this.chat "Votes: " server.votes;

I still don't think this 'voting' npc is all that helpful since you can only agree with it, and you don't get any kind of statistic from those who disagree. I.e: Voting Yes or No.

Your code still needs to be updated to support multiple/changing the survey, and to be documented.
__________________
Quote:
Reply With Quote
  #8  
Old 08-01-2011, 06:11 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
ok, so return; is bad to use. gotcha.
iv updated it to use else functions.
iv also added a server.yes.votes and server.no.votes so the player can write /yes or /no
when i get home il post the script here and then can you see if its ok?
anyway thanks for all your help man.
__________________

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 08-01-2011, 11:00 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
I cant post the new one.
it seems the servers subscription has expired and i have no way of getting it.
-Gunderak
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #10  
Old 08-02-2011, 01:39 AM
ff7chocoboknight ff7chocoboknight is offline
Skyzer Zolderon
ff7chocoboknight's Avatar
Join Date: Dec 2006
Location: New Hampshire, United States
Posts: 725
ff7chocoboknight is a name known to allff7chocoboknight is a name known to allff7chocoboknight is a name known to allff7chocoboknight is a name known to all
Send a message via AIM to ff7chocoboknight Send a message via MSN to ff7chocoboknight
Why do you sign your posts? We can see that it's you saying it.
__________________
Reply With Quote
Reply


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 06:45 PM.


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