Results 1 to 10 of 16

Thread: Gs2 for the Noobs #5

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Street Boss John's Avatar
    Join Date
    Jun 2013
    Location
    Lebanon
    Posts
    825

    Gs2 for the Noobs #5

    Hey guys, this tutorial will show the difference of classes, Databases, and weapons in Graal, as-well as showing some very basic examples of what you can do with them. And this will also be covering the use of an 'object'/NPC inside a level, and will most-likely introduce you to player client and clientr variables.

    weapons:

    A weapon is not what it sounds like, it's not a knife, or a sword in here, the weapon is a script that is attached to the player, it's usually a movement system, or a GUI, or some hack protection... Some servers (including iEra), has some of its' items scripts in their weapons.

    In the past 4 tutorials, we've been working with this type of scripts... You know when someone updates a weapon every time, it will echo() something in RC along the lines of:

    Weapon/GUI-script WeaponName added/updated by Graal#

    Most if not all functions are present in weapons, but remember that a 'weapon' CANNOT be used as an object, it cannot be present in a level, nor walk around a player, nor speak to anyone.

    You can triggerserver(), triggerclient() and do several other stuff inside a weapon...

    NPCs:

    Some of you might know what an NPC is, or at-least that it stands for Non-Player Character.

    First, you need to know how to place an NPC, open GraalEditor (also known as Level Editor), and select 'Baddies, NPCs, Chests' on the right... (Don't worry, nobody uses baddies and chests, the best thing to use is an NPC because you can script a baddy or a chest using an NPC)



    And those are the two types of NPCs available:


    We're going to use the one that is on the left, because the one on the right uses Gs1, and this is a Gs2 lesson so...

    The following script is self explanatory, which shows a standing still NPC.

    PHP Code:
    function onCreated() {
      
    this.showcharacter();

      
    this.body "body1.png";
      
    this.head "head1.png";
      
    this.shield "shield1.png":
      
    this.attr[1] = "hat1.png";
      
    this.nick "Jean-Paul Marie Jacques-Wilbert";
      
    this.colors[0] = "black";
      
    this.colors[1] = "black";
      
    this.colors[2] = "black";
      
    this.colors[3] = "white";
      
    this.colors[4] = "black";
      
    this.ani "idle";
      
    this.ap 100;

    Place it inside your NPC script and just upload the level to your server, once done, you'll see the NPC standing in that server.

    But what can you exactly do with it?
    Here it comes... A widely used built-in function WITH A GRAMMAR ERROR, is onPlayerTouchsMe()!

    What this does, it just checks when the player has touched the NPC, and when he does, it will trigger that function...

    Add to the script:
    PHP Code:
    function onPlayerTouchsMe() {
      
    player.chat "Ahh!! Sorry I keep touching you.... (awkward!!)";

    This function is used on iEra's doors! I'll give an example on it later...
    You can also warp a player to another level too.

    Example:
    PHP Code:
    function onPlayerTouchsMe() {
      
    player.setlevel2("levelname.nw"3020); // setlevel2(levelname, x, y);

    BTW, the this. prefix specifies to the object, so if you want to change the coordinates, the chat or anything on the NPC, simply use:
    PHP Code:
    this.10;
    this.30;
    this.2// Z axis is not 'really' used in 2D games, but can be used as a dis-actual offset of the y axis

    this.chat "My name is Brog and I kill pandas, they eat my bamboo!"
    An NPC DOES NOT contain triggerserver() and triggerclient() ... (actually, it does, but somehow it broke in time so they just use triggeraction in the meantime as a triggerserver() - for triggerclient, there's nothing, but you can always switch to the attr array which is available in both sections)

    Having an object in CLIENTSIDE, will make you ONLY YOU see that.
    Example:
    PHP Code:
    //#CLIENTSIDE
    function onPlayerTouchsMe() {
      
    this.chat "I'm noobish"//because this function is under CLIENTSIDE, you will see the NPC say the chat, but other players won't unless they touch him too!

    So this is what an NPC actually is, but not all NPCs are supposed to be visible, some NPCs have this.image = ""; and does a lot of stuff.

    Let's take for example a race. You know when you touch that very last part you will get warped, right?

    Then the script located in that last block is:
    PHP Code:
    function onPlayerTouchsMe() {
      
    player.setlevel2(this.level1020); // 10 and 20 are the coordinates of the winning spot

    But there's a problem here, now everyone that touches this NPC will be able to get warped to the winning spot, right? We only want one of them to get warped and the who that is the first.
    So I'm going to make a variable called 'winner', and what this will do is store the graalid of the first person inside this variable, then check IF the guy's ID is 'winner', warp him there.

    PHP Code:
    function onCreated() {
      
    this.winner "nobody";
    }

    function 
    onPlayerTouchsMe() {
      if (
    this.winner != player.account && this.winner != "nobody") {
        return; 
    // if the winner variable is not equals to the player account AND not equal "nobody", then go back, don't read the next code.
      
    } else {
        
    this.winner player.account// Why did I use this.? Because I want the variable to stay here when another player touches it!
        
    player.setlevel2(this.level1321.5); // warp the player to the winning arena
      
    }

    classes:
    This is a really important notion to learn.

    A class is not what you go to school and watch, it's not where you can stick bubblegum on the side of your table
    What 'class' means here, is a NPC script, that can be given to several others.

    Do you know Crate Race in iEra?
    Do you know how every crate is scripted? Here's how:

    PHP Code:
    //#CLIENTSIDE
    function onCreated() {
      
    setTimer(1); // Call onTimeOut() after 1 second
    }

    function 
    onTimeOut() {
      
    temp.randomnumber random(0100); // A random number between 0 and 100
      
      
    if (temp.randomnumber 50) { // if the random number is bigger than 50, hide
        
    this.hide();
      } else { 
    // if not, then show
        
    this.show();
      } 

      
    setTimer(1); // Call onTimeOut() again after 1 second.

    So we copy this script on every crate NPC and make a nice road with it.
    But here comes snk and tells us that, "Hey John, 1 second is way too much to change the visibility of the crates, how about you make it 0.5 seconds?"... And here's our problem, how are we gonna change from 1 second to 0.5 seconds in ALL the NPCs?! Oh my, I'm going to have to change it in EVERY NPC I've put the script in!

    Have no fear, Classes are here!

    What you can do with a class, is use 1 scripts, and connect them to any NPC/Weapon/Database you want!

    Let's create a new Class and call it whatever you want, I will call mine, "johnisawesome" (I'm too modest).

    How? Just press this icon: on Client-RC, then press the cross on the left in -> ->

    Okay, now that we have created our class, all we have to do is put our Crate script inside and press Apply.

    But we haven't linked the class to the NPC yet! How to do that?
    There's a default function called join(classname).

    So what we will put in the Crate NPC is:
    PHP Code:
    this.join("johnisawesome"); 

    Now, update the level in game and you will see that crates are working fine! And if snk wants you to change something in the Crate script all you have to do is just change it in the class, and it will auto change to all the crates in the level!

    This is one use of the classes in Graal.

    One thing you need to know in classes, triggerserver() and triggerclient() do not always work.

    databases:
    DBs are a little bit more advanced, they are the combination of weapons and classes...

    Here's a small information box about each of them:

    Weapons
    • Can be added to players
    • Cannot have a location on a map (as in X, Y and levelname)
    • Does not save variables
    • Cannot be attached to objects/players using join()
    • Allows triggerserver() and triggerclient()
    • Is static


    Classes
    • Can be linked to players
    • Can have a location on a map (as in X, Y and levelname)
    • Does not save variables
    • Can be attached to objects/players using join()
    • Does not allow triggerserver() and triggerclient()
    • Is not static


    Databases
    • Cannot be added to players
    • Can have a location on a map (as in X, Y and levelname)
    • Does save variables
    • Cannot be attached to objects/players using join()
    • Allows triggerserver() and triggerclient()
    • Is static
    So the thing is, what does 'static' mean and what does it mean by, it saves variables?

    But first, to create a database, do the same thing we did with Classes but with Database NPCs.

    Once created, you'd see (that's an example of Gang Wars database).

    Flags are the variables used using the 'this.'.
    And the Script is, well, the script.

    Press on Edit Script, type in:
    PHP Code:
    function onCreated() {
      
    this.something "Hello";

    Then press apply, and go to Edit Flags and you can see that there's written something=Hello

    Now if you change the script to:
    PHP Code:
    function onCreated() {
      echo(
    this.something);

    It would echo Hello because the this.something variable was saved in the database.

    So this.something is a saved variable, this is where server store their items, images, hats, and stuff like that.
    Last edited by John; 02-14-2014 at 11:09 AM.
    -Johnaudi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •