Thread: Voting Npc
View Single Post
  #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