Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Newbie scripter needs advice. (https://forums.graalonline.com/forums/showthread.php?t=134263702)

xuntasi 06-30-2011 03:44 AM

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);
  }



skillmaster19 06-30-2011 04:21 AM

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.

fowlplay4 06-30-2011 04:44 AM

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!

JasonJames 06-30-2011 08:58 AM

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


xuntasi 06-30-2011 07:46 PM

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! :D

xuntasi 06-30-2011 09:48 PM

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.

callimuc 06-30-2011 09:53 PM

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


fowlplay4 06-30-2011 10:15 PM

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 (Post 1656831)
advice

I know you're trying to help but I think you might end up causing more harm than good.

xuntasi 06-30-2011 10:23 PM

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.

callimuc 06-30-2011 10:35 PM

Quote:

Originally Posted by fowlplay4 (Post 1656834)
I know you're trying to help but I think you might end up causing more harm than good.

I´ll work on that :)

fowlplay4 06-30-2011 10:37 PM

Quote:

Originally Posted by xuntasi (Post 1656835)
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.

xuntasi 06-30-2011 10:42 PM

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! :)

xuntasi 07-02-2011 07:08 AM

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());)
    } 


Cloven 07-02-2011 07:58 AM

*shakes head*

fowlplay4 07-02-2011 05:18 PM

Quote:

Originally Posted by xuntasi (Post 1657020)
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

fowlplay4 07-02-2011 05:32 PM

Quote:

Originally Posted by xuntasi (Post 1657045)
Omg that's awesome!! :D I've never found this big of a GS2 tutorial, this is perfect!! Thank you so much! :D

It was in my first post in this thread... Read more carefully next time.

xuntasi 07-02-2011 05:33 PM

I'll stop asking for so much help, sorry if it was bugging you, I didn't mean to do that! Thank you so much for that tutorial, it's the biggest I've ever found!! After I read that I shouldn't even have to ask for help anymore! :D


All times are GMT +2. The time now is 04:23 AM.

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