Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   New Scripting Engine (GS2) (https://forums.graalonline.com/forums/forumdisplay.php?f=153)
-   -   Scripting questions (https://forums.graalonline.com/forums/showthread.php?t=134268910)

iDigzy 11-20-2013 09:02 PM

Scripting questions
 
Hi, I recently started scripting GS2 and I have a few questions.
1. What are classes for, like difference between weapons?
2. What is the point of npc's option when you can add npc's in level editor? Is it the same?
3. For setting things for a player, for example the hat or head would the script be something like player.attr1 = "filename"?
4. Are there any "and" or "or" words that can be used in an if statement? For example, a script like
function onPlayerchats() {
if (player.chat = "hello") *could you put an "and" or an "or" comparison here?* {
player.chat = "hi"
}

cyan3 11-20-2013 09:24 PM

Quote:

Originally Posted by iDigzy (Post 1723926)
Hi, I recently started scripting GS2 and I have a few questions.
1. What are classes for, like difference between weapons?
2. What is the point of npc's option when you can add npc's in level editor? Is it the same?
3. For setting things for a player, for example the hat or head would the script be something like player.attr1 = "filename"?
4. Are there any "and" or "or" words that can be used in an if statement? For example, a script like
function onPlayerchats() {
if (player.chat = "hello") *could you put an "and" or an "or" comparison here?* {
player.chat = "hi"
}

Classes are used to minimize code reuse and helps improve debugging/maintenance of your code as you'd only have to edit the single class to apply the changes to every script that uses that class. For example, you could have a class that handles damage which could then be called from any weapon script when you need to cause damage to a player instead of rewriting that same code in every weapon script.

It would be player.attr[1] = "filename";

and is && and or is ||

For example:

PHP Code:

function onPlayerChats() {

    if (
player.chat == "hello" || player.chat == "bye") {

        
player.chat "hi";
    }


Also if you are checking a value is equal to something please use the == operator instead of =, Although both work in GS2 it is good practice to use == for equality and = for assignment.

Jakov_the_Jakovasaur 11-20-2013 09:43 PM

in addition to what cyan3 said, classes are not an actual object such as an npc or weapon, but scripts which are essentially copied and pasted (dynamically) into other scripts. you can join multiple classes into one script, you can join classes within classes, and classes can be joined to multiple different types of objects which for example includes players

npcs within rc (often called database npcs) are permanent objects which are not binded to a particular level, so for example if you updated a level all npcs within it would reset to their original state whereas a database npc would retain variables which were stored on it. database npcs can also be warped into different levels and can have their scripts processed without players being in the same level

callimuc 11-20-2013 09:54 PM

1. Like cyan said, with classes you don't need to copy and paste the same function all the time for every script, meaning if you got a bug and you fix it inside a class, that will do the change to every script being joined to that class -> you don't need to find all the weapons/npcs and update them manually.

2. DB NPCs are used to store information. Lets say you got something like:
PHP Code:

function onPlayerTouchsMe() {
  if (!(
player.account in this.playersThatTouchedMe)) {
    
this.playersThatTouchedMe.add(player.account);
  }


Inside a normal NPCs, the this.playersThatTouchedMe array would reset everytime you update/edit the level. with a DB NPC this would still be saved as a flag.

3. To change a players hat, you need to change the players attr[1] (player.attr[1] = "hat image file";) and for the players head, you need to set the player.head = "head image file"; (for trial users its limited to head0.png to head15.png, maybe even just head10.png).

4. Using && will get you the option to see if the 2nd statement is also true, while using || will check if either one of the statements is true

The or check:
PHP Code:

function onPlayerTouchsMe() {
    
//check if 'player.account' is either 'Stefan' or 'Unixmad'
  
if (player.account == "Stefan" || player.account == "Unixmad") {
    
player.chat "Hey I'm Stefan or Unixmad";
  }


Basically the same as
PHP Code:

function onPlayerTouchsMe() {
  if (
player.account == "Stefan") {
    
player.chat "Hey I'm Stefan or Unixmad";
  }
  else if (
player.account == "Unixmad") {
    
player.chat "Hey I'm Stefan or Unixmad";
  }




The and check:
PHP Code:

function onPlayerTouchsMe() {
    
//check if 'player.account' is 'Stefan' and if he is wearing the 'head0.png' head
  
if (player.account == "Stefan" && player.head == "head0.png") {
    
player.chat "Hey I'm Stefan wearing the head0.png!!";
  }


Basically the same as:
PHP Code:

function onPlayerTouchsMe() {
    
//check if 'player.account' is 'Stefan' and if he is wearing the 'head0.png' head
  
if (player.account == "Stefan") {
    if (
player.head == "head0.png") {
      
player.chat "Hey I'm Stefan wearing the head0.png!!";
    }
  }



iDigzy 11-20-2013 10:09 PM

So a class is kind of like a variable, where it has code that you can just type the class file instead of re-entering all the code?
Thanks for all the answers btw :D

callimuc 11-20-2013 10:54 PM

Let's say more of a non-changeable flag ('const flag' if you're already familiar with that)

Torankusu 11-21-2013 12:12 PM

http://public.zodiacdev.com/Fowlplay4

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

These two links have been helping me get back into gs2.

iDigzy 11-21-2013 06:56 PM

Quote:

Originally Posted by Torankusu (Post 1723935)

Thanks, I knew about fowlplay's but I'll check out the gscript dev one

I have another question, (I think it would be better just to post any questions I have under this thread instead of spamming forums, not sure). I am trying to make a staff block/spawn and npc. I somewhat know how to make it, but I'm not sure how you would do it. This is my current script so far.
NPC Code:
//#CLIENTSIDE
function onPlayerChats() {
if (player.chat = ":blockspawnon") {
enabled = true;
if (player.chat = ":blockspawnoff") {
enabled = false;
}
if (enabled = true) {
player.chat = "Block Spawned!";
putnpc("block.png","test",30,30);
this.chat = "block";
drawunderplayer();
block();
}
}
}


scriptless 11-21-2013 08:57 PM

Quote:

Originally Posted by iDigzy (Post 1723941)
Thanks, I knew about fowlplay's but I'll check out the gscript dev one

I have another question, (I think it would be better just to post any questions I have under this thread instead of spamming forums, not sure). I am trying to make a staff block/spawn and npc. I somewhat know how to make it, but I'm not sure how you would do it. This is my current script so far.
NPC Code:
//#CLIENTSIDE
function onPlayerChats() {
if (player.chat = ":blockspawnon") {
enabled = true;
if (player.chat = ":blockspawnoff") {
enabled = false;
}
if (enabled = true) {
player.chat = "Block Spawned!";
putnpc("block.png","test",30,30);
this.chat = "block";
drawunderplayer();
block();
}
}
}


putnpc needs to be serverside

PHP Code:

function onActionServerside() {
  
// ...
  // putnpc2()
}
//#CLIENTSIDE
... other stuff 

You will need to trigger this so look up triggerserver.

iDigzy 11-21-2013 09:22 PM

Quote:

Originally Posted by scriptless (Post 1723943)
putnpc needs to be serverside

PHP Code:

function onActionServerside() {
  
// ...
  // putnpc2()
}
//#CLIENTSIDE
... other stuff 

You will need to trigger this so look up triggerserver.

thanks :)

Jakov_the_Jakovasaur 11-21-2013 09:39 PM

Quote:

Originally Posted by iDigzy (Post 1723941)
Thanks, I knew about fowlplay's but I'll check out the gscript dev one

I have another question, (I think it would be better just to post any questions I have under this thread instead of spamming forums, not sure). I am trying to make a staff block/spawn and npc. I somewhat know how to make it, but I'm not sure how you would do it. This is my current script so far.
NPC Code:
//#CLIENTSIDE
function onPlayerChats() {
if (player.chat = ":blockspawnon") {
enabled = true;
if (player.chat = ":blockspawnoff") {
enabled = false;
}
if (enabled = true) {
player.chat = "Block Spawned!";
putnpc("block.png","test",30,30);
this.chat = "block";
drawunderplayer();
block();
}
}
}


when comparing values you need to use '==' rather than '=', as '=' is for assigning values and when assigning values it always returns true

for this example you should be assigning the variable using the 'this.' prefix, otherwise it would apply globally which would cause conflict if there were multiple copies of this npc

its also good to avoid checking conditions you already know to be false, for example you can do -

PHP Code:

if (player.chat == ":blockspawnon") {
  
this.enabled true;
}
else if(
player.chat == ":blockspawnoff") {
 
this.enabled false;


or -

PHP Code:

  this.enabled = (player.chat == ":blockspawnon") ? true :
    (
player.chat == ":blockspawnoff") ? false this.enabled
  



iDigzy 11-21-2013 11:58 PM

Well I have absolutely no clue how to trigger the server, after reading a little on it, this is what I have made (not sure if it makes sense though). Hopefully someone can help me fix/understand it D:?
Quote:

function onActionServerSide(a) {
if (cmd == "testing") {
triggerClient("test", this.name, "testing");
}
}

//#CLIENTSIDE
function onPlayerChats() {
if (player.chat = "test") {
triggerServer("test", this.name, "testing");
}
}

function onActionClientSide(a) {
if (cmd == "testing") {

player.chat = "elephants";
triggerServer("test", this.name, "testing");
}
}


Torankusu 11-22-2013 12:37 AM

I am brushing up on these features as well, so I will link you directly to the resource(s) I am using...

http://forums.graalonline.com/forums...17&postcount=4
http://forums.graalonline.com/forums...84&postcount=6

Both, courtesy of cbk. :)


I guess to expound based on your usage:

I'd recommend creating a CLASS for your staff block.
Using putnpc2 (serverside only), it will determine the position to place that block when the server is triggered.
You will then join that NPC placed to the class of your staff block (ex: npc.join("class name"); )

Also, you should get into the habit of using the "echo" feature to ensure parts of your code are working correctly.

Example:

PHP Code:

function onActionServerSide(cmd){
  if (
cmd == "test"){
    echo(
"test successful");
  }
}


//#CLIENTSIDE

function onPlayerChats(){
  if (
player.chat == "test"){
    
triggerServer("gui"this.name"test");
  }



That is an example of a working trigger to the server, that will echo "test successful" in your RC if the player with the weapon says "test", and hopefully help you understand the basic format for it better. :)

Hopefully someone else can chime in as far as setting parameters to transfer to the server, as I am still messing around with that. :P

iDigzy 11-22-2013 01:01 AM

Quote:

Originally Posted by Torankusu (Post 1723956)
I am brushing up on these features as well, so I will link you directly to the resource(s) I am using...

http://forums.graalonline.com/forums...17&postcount=4
http://forums.graalonline.com/forums...84&postcount=6

Both, courtesy of cbk. :)

Thanks to you and cbk, I'll take a look at it :)

iDigzy 11-27-2013 05:30 PM

Well I haven't posted a question on here in a bit, but I ran into another problem. I want to make a script that sets the players tag. I'm not sure if this script is correct in any way/or if I over thought it. Can someone tell me whats wrong :o?
PHP Code:

function onActionSeverside(tags){
if (
tokens[0] == "/tag"){
player.guild "Elepahnt";
}
}

//#CLIENTSIDE
function onPlayerChats(){
if (
player.chat == "/tag"){
temp.tokens player.chat.tokenize();
triggerServer("gui"this,nametags);
}




All times are GMT +2. The time now is 01:56 PM.

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