Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-30-2011, 03:44 AM
xuntasi xuntasi is offline
Registered User
xuntasi's Avatar
Join Date: Jun 2011
Location: Deadaccountsville, Internet
Posts: 14
xuntasi is on a distinguished road
Question Newbie scripter needs advice.

I am very new to scripting and need help. This is the first script I've ever made and it doesn't work. What is wrong with this script? for some reason when I put the sleep() command in there it makes that player.chat variable command not work correctly.

PHP Code:
function onPlayerChats()
{
  if(
player.chat=="You are the most amazing NPC ever.")
  {
    
message That I am.;
    
sleep(3);
    
message(null);
  }
  else
  {
    
message Shut upno one cares.;
    
sleep(1);
    
player.chat="Ok...";
    
sleep(1);
    
message That's right.;
    sleep(3);
    message(null);
  }


Last edited by xuntasi; 06-30-2011 at 03:59 AM..
Reply With Quote
  #2  
Old 06-30-2011, 04:21 AM
skillmaster19 skillmaster19 is offline
Registered User
Join Date: Oct 2010
Posts: 392
skillmaster19 will become famous soon enough
message should look like
PHP Code:
message("this is a message"); 
not
PHP Code:
message this is a message done wrong
note the quotes and the parenthesis.
Functions need to be wrapped in parenthesis. Also, strings(a series of characters like "asdfghjkl") need to be in qoutes otherwise the computer recognizes them as refering to a variable.
examples:
PHP Code:
setimg("door.png"); //filenames are a string
message(this.hearts); //it's a variable so it doesn't need qoutes
//example with a mixture of both
message("I now have" SPC player.rupees SPC "gralats"); 
If you want to learn more about functions here you go:

examples of functions with variables:
PHP Code:
freezeplayer(1); //freezes the player for the float (integer that can do decimals) of 1 second.
//this can be changed to
freezeplayer(2); //freezes the player for 2 seconds. 
examples of functions with both floats and strings
PHP Code:
triggeraction(30,35,"hit"); // triggers it at x pos of 30 and y of 35, with the command hit. 
returning functions
PHP Code:
findplayerbycommunityname("skillmaster19").chat ="I'm awesome"


If you don't understand this I recommend reading twinny's tutorial, its easy and explains most of this in a much less complicated way. Then move onto fowlplay's tutorial that covers more advanced topics.

Last edited by skillmaster19; 06-30-2011 at 04:41 AM..
Reply With Quote
  #3  
Old 06-30-2011, 04:44 AM
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
Welcome to the Scripting forums.

You're mixing GS1 and GS2 together which is a bad and I believe the script isn't working as intended because you didn't make it client-side.

I personally consider message() deprecated. You can just edit the npc's text directly like you do with the player's. I.e:

this.chat = "Hello World!";

I've made a quick revision to your script with my suggested changes:

PHP Code:
//#CLIENTSIDE
function onPlayerChats() {
  if (
player.chat == "You are the most amazing NPC ever.") {
    
this.chat "That I am.";
    
sleep(3);
    
this.chat "";
  } else {
    
this.chat "Shut up, no one cares.";
    
sleep(1);
    
player.chat "Ok...";
    
sleep(1);
    
this.chat "Thats right.";
    
sleep(3);
    
this.chat "";
  }

There's a wealth of information on these forums, and on the Graal Bible / Wiki as well. You just need to search for it.

Graal Bible / Wiki:

http://wiki.graal.net/index.php/Creation/Dev/GScript

The tutorials that skillmaster mentioned can be found here:

Twinny's scripting guide
fowlplay4's scripting guide

Read through them, search the forums when you encounter problems, and feel free to look through Code Gallery scripts. We're also here to help. Good luck!
__________________
Quote:
Reply With Quote
  #4  
Old 06-30-2011, 08:58 AM
JasonJames JasonJames is offline
Registered User
Join Date: Jun 2011
Posts: 1
JasonJames is on a distinguished road
The sleep was breaking it because a sleep on serverside causes the player to fall out of scope.

You can put the player object in a temporary variable first if you'd still like to do it on serverside (although in this case there's no reason to -- Jer's example should do exactly what you want it to).

PHP Code:
temp.pl player// put the player object in a temporary variable
sleep(2);
temp.pl.chat "It still works!"
Reply With Quote
  #5  
Old 06-30-2011, 07:46 PM
xuntasi xuntasi is offline
Registered User
xuntasi's Avatar
Join Date: Jun 2011
Location: Deadaccountsville, Internet
Posts: 14
xuntasi is on a distinguished road
Thank you so much guys! The script works perfectly! Not to mention I actually understand how it works, which is good! So thanks! You guys are awesome!
__________________
This account is DEAD!
Reply With Quote
  #6  
Old 06-30-2011, 09:48 PM
xuntasi xuntasi is offline
Registered User
xuntasi's Avatar
Join Date: Jun 2011
Location: Deadaccountsville, Internet
Posts: 14
xuntasi is on a distinguished road
Question More scripting issues.

I don't mean to bother you guys with too many of my problems, but I've yet another script that I cannot figure out.
PHP Code:
//#CLIENTSIDE
function onCreated() {
  
this.name "Basic";
  }
function 
onPlayerChats() {
  if(
player.chat.start=="/setname"){
    
this.name player.chat "/setname";
  }
  if(
player.chat=="/showname") {
  
this.chat "My name is" SPC this.name @".";
  
sleep(3);
  
this.chat "";
  }

The "/showname" function works perfectly, but I can't figure out the "/setname" function. I knew as soon as I saw it that it wouldn't work, but no matter what I try I can't figure out how to fix it.
__________________
This account is DEAD!
Reply With Quote
  #7  
Old 06-30-2011, 09:53 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
PHP Code:
this.name player.chat "/setname"
You don´t use like player.chat - "/setname";
You should do something like:
PHP Code:
this.name player.chat.substring(9); 
__________________
MEEP!
Reply With Quote
  #8  
Old 06-30-2011, 10:15 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
Well name is one of those pre-defined/engine variables that you can't use.

Instead use something else like this.npc_name.

Also starts() is a string/object function. You call it like this:

I've also included how to extract the player's input using tokenize and substring.

PHP Code:
//#CLIENTSIDE
function onPlayerChats() {
  
// Checks if player chat starts with "/setname "
  // I added the trailing space so: /setname test - will work
  // and: /setnametest - will not.
  
if (player.chat.starts("/setname ")) {
    
// Substring Method
    // The starting position of the player's input is equal to the length of the command.
    
temp.new_name player.chat.substring("/setname ".length()); 
    
// Tokenize Method
    
temp.tokens player.chat.tokenize();
    
temp.new_name temp.tokens[1];
    
// Set NPC's Name
    
this.npc_name temp.new_name;
  }

The difference between substring and tokenize.

string.substring(start_position, [length]) - extracts a section of a string, if you don't specify a length it extracts everything from the starting position.

string.tokenize([token]) - splits the string into multiple tokens based on the token you passed to it. If you don't specify a token it uses a blank space.

For your scenario here it's best to just use substring because then the player won't be required to use quotations.

Quote:
Originally Posted by callimuc View Post
advice
I know you're trying to help but I think you might end up causing more harm than good.
__________________
Quote:

Last edited by fowlplay4; 06-30-2011 at 10:35 PM..
Reply With Quote
  #9  
Old 06-30-2011, 10:23 PM
xuntasi xuntasi is offline
Registered User
xuntasi's Avatar
Join Date: Jun 2011
Location: Deadaccountsville, Internet
Posts: 14
xuntasi is on a distinguished road
Thanks very much! Just to clarify, it's putting ("/setname ".length()) after player.chat.substring that causes the "/setname" part of the players chat to not effect the whole name? If you don't have time to explain, I understand, I know I'm being an endless font of questions right now, but how exactly does that work? It'd be good to know for future scripts.
__________________
This account is DEAD!
Reply With Quote
  #10  
Old 06-30-2011, 10:35 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 fowlplay4 View Post
I know you're trying to help but I think you might end up causing more harm than good.
I´ll work on that
__________________
MEEP!
Reply With Quote
  #11  
Old 06-30-2011, 10:37 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 xuntasi View Post
Thanks very much! Just to clarify, it's putting ("/setname ".length()) after player.chat.substring that causes the "/setname" part of the players chat to not effect the whole name? If you don't have time to explain, I understand, I know I'm being an endless font of questions right now, but how exactly does that work? It'd be good to know for future scripts.
I updated my code to clear it up.

Basically the length of "/setname " is the position in the string where the player's input starts. I.e:

/setname test

would return: test

You can also trim your input as well but I wouldn't worry about that right now.
__________________
Quote:
Reply With Quote
  #12  
Old 06-30-2011, 10:42 PM
xuntasi xuntasi is offline
Registered User
xuntasi's Avatar
Join Date: Jun 2011
Location: Deadaccountsville, Internet
Posts: 14
xuntasi is on a distinguished road
Ah, I get it! Thank you so much, you have been loads of help! I plan to eventually implement a script like this for a name system for pets on my server once I get better at scripting, so I'm glad I can understand it now! Thanks very much, it means a lot!
__________________
This account is DEAD!
Reply With Quote
  #13  
Old 07-02-2011, 07:08 AM
xuntasi xuntasi is offline
Registered User
xuntasi's Avatar
Join Date: Jun 2011
Location: Deadaccountsville, Internet
Posts: 14
xuntasi is on a distinguished road
Question

This is a script I made for my server so I could randomly make people say stuff if they annoyed me. Just for a bit of fun. It doesn't work, however, and I have no clue as to what is wrong with it. From everything I've learned it seems that it should work. Please help me if you have time, it would be greatly appreciated!

PHP Code:
function onWeaponFired() {
  
temp.acc.chat temp.message;
  }
function 
onPlayerChats() {
  if(
player.chat.starts=="/message " && temp.acc.account!="Graal780973") {
    
temp.message player.chat.substring("/message ".length());
    }
  if(
player.chat.starts=="/account " && temp.acc.account!="Graal780973") {
    
temp.acc findplayer(player.chat.substring("/account ".length());)
    } 
__________________
This account is DEAD!
Reply With Quote
  #14  
Old 07-02-2011, 07:58 AM
Cloven Cloven is offline
Delteria
Cloven's Avatar
Join Date: Dec 2006
Location: Florida, United States
Posts: 542
Cloven has a spectacular aura about
Send a message via AIM to Cloven
*shakes head*
Reply With Quote
  #15  
Old 07-02-2011, 05:18 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 xuntasi View Post
This is a script I made for my server so I could randomly make people say stuff if they annoyed me. Just for a bit of fun. It doesn't work, however, and I have no clue as to what is wrong with it. From everything I've learned it seems that it should work. Please help me if you have time, it would be greatly appreciated!

PHP Code:
function onWeaponFired() {
  
temp.acc.chat temp.message;
  }
function 
onPlayerChats() {
  if(
player.chat.starts=="/message " && temp.acc.account!="Graal780973") {
    
temp.message player.chat.substring("/message ".length());
    }
  if(
player.chat.starts=="/account " && temp.acc.account!="Graal780973") {
    
temp.acc findplayer(player.chat.substring("/account ".length());)
    } 
You're starting on your way to becoming a script beggar and that's not good.

Read the snippets I have linked here on Client<->Server interaction if you actually are interested in learning:

http://public.zodiacdev.com/index.ph...er_Interaction
__________________
Quote:
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 03:43 PM.


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