Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Script Formatting Guidelines (https://forums.graalonline.com/forums/showthread.php?t=61805)

Skyld 10-18-2005 05:37 PM

Script Formatting Guidelines
 
It has reached my attention lately that a number of people have been neglecting to keep their scripts in a sensible state. As a result of this, I have written a set of guidelines for scripting. They seem to have adopted the name "SSI-GS2".

What are the advantages of following these guidelines, you say? It's quite simple. People may be more willing to help you with your code. See section B.1.

A. Definitions
  • A.1. Function: A predefined block of code, which can be executed when needed and can be given parameters since gscript2
  • A.2. Variable: An object that's value can be changed
  • A.3. Variant: A variable which automatically assumes the type of data depending upon the data it is given
  • A.4. Event: Triggers which the scripting engine uses to execute predefined code (usually an Event Block)
  • A.5. Event block: A predefined block of code, written to be executed when a certain event occurs


B. General Formatting Guidelines

B.1. Importance of Readability
The readability of your code is very important, for more than one reason:
  • It makes it much easier for others to read and understand your code
  • It makes it much easier to spot syntactical errors, and often simple logic problems
  • It is generally much nicer to work with

B.2. Scripting Style
Most people usually have their own style of scripting. However, sometimes these differences make it harder to understand code. Therefore, it is a good idea to stick to these guidelines when formatting your code.
  • B.2.1. Indentation

    Indentation should always be consistent throughout your code. Two whitespaces per open block are effective.
    This example is good:
    PHP Code:

    //#CLIENTSIDE
    function onCreated()
    {
      
    showimg(200"testimage.png"100100);

      if (
    player.30)
      {
        
    changeimgcolors(2001001);
      }


    However, this example is bad:
    PHP Code:

    function onCreated()
    {
    showimg(200"testimage.png"100100);

    if (
    player.30)
    {
        
    changeimgcolors(2001001);
    }


  • B.2.2. Spacing

    Spaces between parameters, operators, etc, should always be consistent. One whitespace is usually best.
    Please note that when using commands in old gscript, this is not always applicable.
    This example is good:
    PHP Code:

    function onCreated()
    {
      if (
    player.account == "Skyld" || player.account == "GrowlZ1010")
      {
        
    player.chat "Woah.";

        
    triggeraction(00"serverside""Weapon""action");
      }


    And the following example is bad:
    PHP Code:

    function onCreated()
    {
      if (
    player.chat=="Skyld"||player.chat=="GrowlZ")
      {
        
    player.chat="Woah.";
        
    triggeraction(0,0,"serverside","Weapon","action");
      }


  • B.2.3. Braces Edited

    Braces are used to fit more than one operation into an event or function. It is usually good practise to use them even if you only have one line of code in your function.

    However, to promote readability, you should remain consistent on whether your braces are on a new line or directly follow the function definition/statement.

    Some argue towards having braces on their own lines (imagine that you have a long script with a lot of braces being used. Reading over it with braces on their own lines not only makes it easier to spot where blocks are opened and closed, but it makes the script more spaced out.) however, this still remains as preference to the scripter.

    The following example is good:
    PHP Code:

    function onCreated()
    {
      
    this.chat "Hello";
    }

    function 
    onPlayerChats()
    {
      
    this.chat "WHAT DO YOU MEAN";


    The following example is bad:
    PHP Code:

    function onCreated() {
      
    this.chat "Hello";
    }
    function 
    onPlayerChats()
    {
      
    this.chat "WHAT DO YOU MEAN";


  • B.2.4. Topic Separation

    Topic separation has two parts.

    B.2.4.1. Keeping code dedicated to one purpose
    If you are writing code that performs several tasks, i.e. drawing an image and then writing to some player variables, these should be seperated by a blank line.

    The following example is good:
    PHP Code:

    //#CLIENTSIDE
    function onCreated()
    {
      
    showimg(200"testimage.png"100100);
      
    changeimgvis(2004);

      if (
    player.30)
      {
      }


    And the following example is bad:
    PHP Code:

    //#CLIENTSIDE
    function onCreated()
    {
      
    showimg(200"testimage.png"100100);
      
    changeimgvis(2004);
      if (
    player.30)
      {
      }


    B.2.4.2. Keeping conditional checks to a topic
    When using if () to check things, it is best that you keep all conditional checks to a set topic.

    The following example is good:
    PHP Code:

    function onCreated()
    {
      if (
    player.account == "Skyld")
      {
        if (
    player.30 && player.30)
        {
        }
      }


    And the following example is bad:
    PHP Code:

    function onCreated()
    {
      if (
    player.account == "Skyld" && player.30 && player.30)
      {
      }


  • B.2.5. Comments
    Keep the style of the comment suited to the length of the comment.

    For more than two lines of comments, you should use this:
    PHP Code:

    /*
      Comments.
      More comments.
      Even more comments.
      Rah rah rah.
     */ 

    For up to two lines of comments, you should use this:
    PHP Code:

    // Comments.
    // Further commenting. 


C. Code Efficiency Rules

These guidelines are simply designed to assist in making your code more efficient.
  • C.1. Do not use an event block more than once

    You should only use an event block once per script.

    This applies especially to old gscript.

    The following example is good:
    PHP Code:

    function onPlayerChats()
    {
      if (
    player.chat == "Hello")
      {
        
    this.chat "Good day to you, sir";
      }
        else
      if (
    player.chat == "Goodbye")
      {
        
    this.chat "Take care of yourself, sir";
      }


    The following example is bad:
    PHP Code:

    function onPlayerChats()
    {
      if (
    player.chat == "Hello")
      {
        
    chat "Good day to you, sir";
      }
    }

    // More code here

    function onPlayerChats()
    {
      if (
    player.chat == "Goodbye")
      {
        
    this.chat "Take care of yourself, sir";
      }


  • C.2. Use arrays for values of one topic

    It is bad organisation and general bad practise to use more than one variable to store more than one value that are all related.

    The following example is good:
    PHP Code:

    function onCreated()
    {
      
    this.myaccount player.account;
      
    this.myposition = {player.xplayer.yplayer.level.name};


    And the following example is bad:
    PHP Code:

    function onCreated()
    {
      
    this.myaccount player.account;
      
    this.myx player.x;
      
    this.myy player.y;
      
    this.mylevel player.level.name;


  • C.3. Don't put //#CLIENTSIDE in unusual places such as inside code blocks

    This isn't really an efficiency rule as such - it's a fundamental requirement - that //#CLIENTSIDE should not be put inside a function or such.

    This means, that //#CLIENTSIDE should always be outside of a code block.

    The following example is good:
    PHP Code:

    function onPlayerChats()
    {
      
    this.chat player.chat;
    }

    //#CLIENTSIDE

    function onCreated()
    {
      
    showimg(200"testimage.png"100100);
      
    changeimgvis(2004);


    And the following example is bad:
    PHP Code:

    function onPlayerChats()
    {
      
    this.chat player.chat;
    }

    function 
    onCreated()
    {
      
    //#CLIENTSIDE
      
    showimg(200"testimage.png"100100);
      
    changeimgvis(2004);


  • C.4. Don't put code outside of an event block

    Code should always be inside an event block, so that code execution is, well, organised. Putting code outside of an event block is a Generally Bad Idea™. The only exception to this is a use of this.join();.

    The following example is good:
    PHP Code:

    function onCreated()
    {
      
    this.chat "Welcome";


    And the following example is bad:
    PHP Code:

    this.chat "Welcome"


End. Suggestions and constructive criticism welcome.

ApothiX 10-18-2005 05:42 PM

So you've made your own "SSI-GS2"?

Quote:

Originally Posted by Skyld
The following example is bad:
NPC Code:

function onCreated() {
chat = "Hello";
}


What is wrong with that style? :|

Skyld 10-18-2005 05:46 PM

Quote:

Originally Posted by ApothiX
So you've made your own "SSI-GS2"?

If you want to call it that.
Quote:

Originally Posted by ApothiX
What is wrong with that style? :|

Quote:

Originally Posted by Skyld
Imagine that you have a long script with a lot of braces being used. Reading over it with braces on their own lines not only makes it easier to spot where blocks are opened and closed, but it makes the script more spaced out.

I find it much easier to read over code that has braces on their own lines for those reasons. I am sure that others would agree with me.

ApothiX 10-18-2005 05:56 PM

Ack, sorry, just quickly skimmed through it and didn't notice the explanation.

Quote:

Originally Posted by Skyld
I find it much easier to read over code that has braces on their own lines for those reasons. I am sure that others would agree with me.

I guess it's just a matter of getting used to it. Personally, I don't like seeing so many irrelevant lines, and things are easier to follow with they are in the form:

a() {
}

It's probably because I use that style in both gscript and c, and have been using it for quite some time :x

Polo 10-18-2005 06:47 PM

I agree with ApothiX on this one, in the sense that I prefer packing the opening brace at the END of the leading line. HOWEVER, I agree that both are perfectly valid, and you will find most scripters are 50/50 split as to which is better. As a result I think both should be allowed as valid in the guidelines above. :)

Edit:
I spoke with Skyld and this issue is now changed. See above (Section 2.3) for the new proposal. As it currently stands, I am agreeing with everything proposed, so Skyld currently has my endorsement :).

excaliber7388 10-18-2005 09:23 PM

WHY IS EVERY ONE LOOKING AT ME?
XD I'll do my best from now on, thanks!

ApothiX 10-18-2005 09:57 PM

Ah, with that edit, I'll have to agree aswell that Skyld's guidelines are suitable :)

napo_p2p 10-18-2005 10:47 PM

Quote:

Originally Posted by ApothiX
Ah, with that edit, I'll have to agree aswell that Skyld's guidelines are suitable :)

Ditto ;).

Silent 10-18-2005 11:40 PM

Very well explained and laid out, good job

Riot 10-19-2005 03:32 AM

It is certainly something I can agree to, however, you should mention the use of "else" and "else if" to remove unneeded conditional checks.

ApothiX 10-19-2005 04:18 AM

Quote:

Originally Posted by Riot
It is certainly something I can agree to, however, you should mention the use of "else" and "else if" to remove unneeded conditional checks.

But that will lead to bashing of my favourite style :(

NPC Code:
if(condition) {
statements;
} else {
statements;
}


prozac424242 10-19-2005 04:43 AM

(hides from the obsessivly neat people) ... cleanliness is good, and scripting/programming should be visually appealing as an art form ... but there is a point where becoming insanely obsessive about whether or not there is one extra blank line separating code blocks deeming your code to be good or bad ... that sends certain folks running the other way ... folks like me who care about making it work first, then commenting it and making it look nice later.

I feel that comments are more important than how the code looks, becasue in the end few people are going to see your code - far more are going to see what actions your code performs. And if there are comments, then the next npc person who picks up your code can most likely figure out your code and will either re-format what you wrote to their liking, or simply redo what you made from scratch to perform the same function.

napo_p2p 10-19-2005 04:48 AM

Quote:

Originally Posted by prozac424242
I feel that comments are more important than how the code looks, becasue in the end few people are going to see your code - far more are going to see what actions your code performs. And if there are comments, then the next npc person who picks up your code can most likely figure out your code and will either re-format what you wrote to their liking, or simply redo what you made from scratch to perform the same function.

For me, style > commenting.

I'd rather work on a nicely styled script that isn't commented, then work on a commented script that is all on one line.

ApothiX 10-19-2005 02:48 PM

Quote:

Originally Posted by prozac424242
folks like me who care about making it work first, then commenting it and making it look nice later.

Believe it or not, when code looks nice, and is easy to read, it makes debugging go a lot smoother. If I had an error in a code that was very poorly formatted, it would take me a long time to find where the error was, but if everything was formatted correctly, it is much easier to spot errors and correct them.

prozac424242 10-19-2005 06:21 PM

I do agree that having your own format
of code that works for you is something every programmer will develop.
I have more than 20 years of programming under my belt,
and when I write any program there is an order to it,
each command and bracket gets its own line, I indent nested blocks of code,
but I do it all as naturally as breathing.
I don't need to think about it, I just do it.
But I don't need to write a lengthly document explaining how I do it,
since everyone who sticks with programming of any kind will pick it up anyways to save their own sanity when they go back to edit their old code.

napo_p2p 10-19-2005 07:48 PM

Quote:

Originally Posted by prozac424242
But I don't need to write a lengthly document explaining how I do it

It's not that lengthy. Plus, the document is not meant to explain how he styles, but rather meant to help others who do not "have 20 years of programming under their belts" to develop their own style.

ApothiX 10-20-2005 01:08 AM

Quote:

Originally Posted by prozac424242
But I don't need to write a lengthly document explaining how I do it, since everyone who sticks with programming of any kind will pick it up anyways to save their own sanity when they go back to edit their old code.

That's the problem, a lot of people don't format their code, and never learn to. *looks at his computer science class*

Nitro2030ce 10-20-2005 01:35 AM

Quote:

Originally Posted by Skyld
I find it much easier to read over code that has braces on their own lines for those reasons. I am sure that others would agree with me.

I don't, and you make it seem like it's illegal for people to script in the style/format they want.

ForgottenLegacy 10-20-2005 01:44 AM

Some people read code based on the braces and their positions, while others read code based on the ammount of whitespace that comes before the first character in the line. I, personally, don't like having braces on their own line, as it throws off my ability to read and understand the code. That's what whitespace is for, isn't it? To aide the readability of the code? Personally, I don't put braces on their own line as I think it's overkill on style and it throws off my ability to read the code.

Also, when I first learned how to script, I learned the syntax of placing the braces after the conditional it comes from. -Shrugs- Just the way I was taught, and the way I think. Anyone agree/disagree?

excaliber7388 10-20-2005 01:49 AM

Heh, trust me, just use the format....they're watching you ;) No really, some parts of it are kinda annoying (like not having weaponfired && playermp>2 in the same line, or when you have to do something like that multiple times) but it will help other people read it, as it is easy for every one.

ApothiX 10-20-2005 02:31 PM

Quote:

Originally Posted by Nitro2030ce
I don't, and you make it seem like it's illegal for people to script in the style/format they want.

He's not saying it's illegal, he's just pointing out general guidelines for people who don't format their script, or want to make it more readable and efficient.

Quote:

Originally Posted by excaliber7388
Heh, trust me, just use the format....they're watching you ;) No really, some parts of it are kinda annoying (like not having weaponfired && playermp>2 in the same line, or when you have to do something like that multiple times) but it will help other people read it, as it is easy for every one.

The only thing that should be in the global scope should be event checks. "&& playermp>5" defys that rule.

Fry 10-20-2005 07:58 PM

Quote:

Originally Posted by prozac424242
(hides from the obsessivly neat people) ... cleanliness is good, and scripting/programming should be visually appealing as an art form ... but there is a point where becoming insanely obsessive about whether or not there is one extra blank line separating code blocks deeming your code to be good or bad ... that sends certain folks running the other way ... folks like me who care about making it work first, then commenting it and making it look nice later.

Yes, style is something every person sees different, what's important is that you chose one and stick with it for the whole script.
Beside that there are only readability rules that should be totally obvious, as in not placing everything in one line/etc.
Stuff like new lines for braces, or spaces around operators is something that's different for everyone.

Rick 10-20-2005 08:56 PM

Skyld, use [html] instead of [code], it gets rid of those space nasties in the code examples.

ApothiX 10-20-2005 09:26 PM

Quote:

Originally Posted by Rick
Skyld, use [html] instead of [code], it gets rid of those space nasties in the code examples.

:o I didn't know about this infamous [html], I usually use: [php] for longer code samples

napo_p2p 10-21-2005 02:32 AM

Quote:

Originally Posted by ApothiX
:o I didn't know about this infamous html, I usually use: php for longer code samples

Same here.

HTML Code:

function onCreated() {
  //Just looking
}

Ooooo.... shiny

prozac424242 10-21-2005 06:34 AM

u know, today i was debugging an npc,
and i indented the blocks of code
according to where the brackets were
with one bracket on each line and whaddayaknow! I found where the missing bracket went.

Thanks mr. style! :happy:

napo_p2p 10-21-2005 06:46 AM

Quote:

Originally Posted by prozac424242
u know, today i was debugging an npc,
and i indented the blocks of code
according to where the brackets were
with one bracket on each line and whaddayaknow! I found where the missing bracket went.

Thanks mr. style! :happy:

Skyld made a believer out of this one. :cool:

ApothiX 10-21-2005 02:49 PM

prozac424242 has been converted to stylism.

Codein 11-03-2005 09:39 PM

There should be like "Stylistianity" or something, lol. Those who don't style their code, and those who do. Then people can say "I'm a proud follower of Stylistianity"

Lance 11-04-2005 02:29 AM

Quote:

Originally Posted by Codein
There should be like "Stylistianity" or something, lol. Those who don't style their code, and those who do. Then people can say "I'm a proud follower of Stylistianity"

No.

prozac424242 11-05-2005 05:46 PM

actually I rememberd what I learned i college intro to computer science class ten years ago (oops dont want to give away my age)

The curly brackets { and } should be as the upper left and lower left corners of a box, a box in which you should have intented so that you could draw a literal box around a printout of each block of code.

HTML Code:

function onCreated()
  {
    //code goes here
    each indent I do for two spaces

      if (something==1)
        {
          //more code here
        }

  }

function onWhatever()
  {
    //slap Unixmad just becasue
  }

the white space above and below each code block or nested code block
can help you visualize the box in which the block of code belongs in.
that's how I was taught.

Rick 11-05-2005 06:20 PM

Ewwww :P

ApothiX 11-10-2005 06:17 PM

Quote:

Originally Posted by prozac424242
actually I rememberd what I learned i college intro to computer science class ten years ago (oops dont want to give away my age)

The curly brackets { and } should be as the upper left and lower left corners of a box, a box in which you should have intented so that you could draw a literal box around a printout of each block of code.

HTML Code:

function onCreated()
  {
    //code goes here
    each indent I do for two spaces

      if (something==1)
        {
          //more code here
        }

  }

function onWhatever()
  {
    //slap Unixmad just becasue
  }

the white space above and below each code block or nested code block
can help you visualize the box in which the block of code belongs in.
that's how I was taught.

Wow, that is some ugly formatting right there..


All times are GMT +2. The time now is 07:36 PM.

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