Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Voting Npc (https://forums.graalonline.com/forums/showthread.php?t=134264037)

Gunderak 07-30-2011 06:46 PM

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!");



fowlplay4 07-30-2011 07:00 PM

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

Gunderak 07-30-2011 07:17 PM

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

fowlplay4 07-30-2011 07:38 PM

Quote:

Originally Posted by Gunderak (Post 1660907)
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.

Gunderak 07-30-2011 07:46 PM

ah ok thanks for your help man.
il fix the script up xD

Gunderak 07-30-2011 08:55 PM

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!");



fowlplay4 07-30-2011 09:26 PM

Quote:

Originally Posted by Gunderak (Post 1660917)
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.

Gunderak 08-01-2011 06:11 AM

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.

Gunderak 08-01-2011 11:00 AM

I cant post the new one.
it seems the servers subscription has expired and i have no way of getting it.
-Gunderak

ff7chocoboknight 08-02-2011 01:39 AM

Why do you sign your posts? We can see that it's you saying it.


All times are GMT +2. The time now is 06:19 PM.

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