Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Reply to this topicStart new topic
> [Scripting]Good Scripting Practices, Tips on good scripting practice
deadlydan
post Jan 31 2008, 05:30 PM
Post #1


Level 5
Group Icon

Group: Member
Posts: 71
Type: Event Designer
RM Skill: Masterful




Hey guys, as my first tutorial I wanted to start off with something simple, and easy to understand. This tutorial will show you a few tips of good programming/scripting practices.

Basically, the inspiration behind this is that I constantly see a lot of scripts released which are very uneasy on the eye, awkward to understand and not easy to implement extra functionality.

By improving the way you script and even the way you name your variables, you can enable a much larger amount of people to understand and extend your scripts. Sure it might take a bit more time but it's worth it in the long run.

When programming, how you use your syntax greatly affects the flow of your program, it's easy to get lost in code; which is a bit silly considering it doesn't take much effort to make it so much more visually pleasing and easy on the eyes. I'll demonstrate some techniques you can do to make your code look cleaner and more comfortable.

1. Spacing
During programming and scripting, spacing is a necessity when wanting nice clean code. A lot of people tend to have not picked up the habit of spacing when writing scripts. If anyone has read my scripts you'll see that i use spacing quite frequently; it helps to produce nice clean code.

Here's an example of code without spacing, it's hard to read and it's messy.

CODE
def TestFunction(x,y,z,string="DefaultString")
    tempnumber=10
    tempstring=string+tempnumber.to_s
    print(tempstring)
end


Here's the same code again, with spacing added, you now see how much nicer and easier on the eyes it is.

CODE
def TestFunction ( x, y, z, string = "DefaultString" )
    tempnumber = 10
    tempstring = string + tempnumber.to_s
    print ( tempstring )
end


It's significantly easier to read, just from adding spacing. It's really good practice to pick up spacing your syntax. As you can see, it's quite effective.

2. Parenthesis
As my roots in programming come from C/C++, I'm very keen on parenthesis, since this is optional in most cases in ruby not many people use it. But it's a great tool and really helps when reading through scripts and even scripting them. For example, take a look at the following code and compare it to the code after. Which do you think is easiest to read?
(Move away from the screen, look from a distance... Which is easier to read?)

CODE
def TestFunction ( x, y, z, string = "DefaultString" )
    tempnumber = 10
    result = x + y + z - tempnumber * x + y + z / 2
    tempstring = string + result.to_s
    print tempstring
end


As you can see, this code is quite crowded, and uneasy on the eyes, with spacing it's a lot more pleasing, but still not quite there yet. Here's the same code with parenthesis:

CODE
def TestFunction ( x, y, z, string = "DefaultString" )
    tempnumber = 10
    result = ( ( ( x + y + z ) - tempnumber ) * ( ( x + y + z ) / 2 ) )
    tempstring = string + result.to_s
    print ( tempstring )
end


Being easy to read isn't the only advantage of parenthesis, as you should know, if you did mathematics in school, parenthesis also is good for equations. It's also easier for the ruby interpreter to understand, since it has parenthesis it can parse your script a lot easier.

3. Indentation
You've probably already noticed that I proper indent all my code, indentation is a great programmers tool, use it properly and your code will be easier to understand and a lot cleaner. Here's an example of a commonly bad indented script where someone as obviously copied some script from somewhere and left it without fixing the indentation:

CODE
def TestFunction ( x, y, z, string = "DefaultString" )
tempnumber = 10
result = ( ( ( x + y + z ) - tempnumber ) * ( ( x + y + z ) / 2 ) )
    if ( result >= 20 )
    print ( "Test!" )
end
        tempstring = string + result.to_s
        print ( tempstring )
end


And here's the same code with nice indentation:

CODE
def TestFunction ( x, y, z, string = "DefaultString" )
    tempnumber = 10
    result = ( ( ( x + y + z ) - tempnumber ) * ( ( x + y + z ) / 2 ) )
    if ( result >= 20 )
        print ( "Test!" )
    end
    tempstring = string + result.to_s
    print ( tempstring )
end


As you can see it's a lot cleaner, this is another good practice. And now to my last section (Unless I update this smile.gif)

4. Naming
This is something a lot of people have problems with, naming variables and functions. I've seen countless times in other people's scripts variables and functions with misleading names. You should always name them in a way that it's easy to understand what it will be doing.

And an example of a variable name is this for a players step count:

CODE
@dans_thing = 2


You should never name anything like this, we have no idea what that does. A better thing would be:

CODE
@player_step_count = 2


It's now obvious what this variable is. Here's an example of a badly named function for setting a text position offset:

CODE
def TextThingy ( variable )
end


As you can see, this is also bad, we have no idea what this function does, and we have no idea what the parameter variable is for. A better function would be:

CODE
def TextSetOffset ( offset )
end


That brings me to my next point, i often see in scripts when people add variables to the RGSS $game_temp or $game_system for example, they name they're variables with generic names, when adding things in RGSS it's always best to add a prefix before your function and variable names, so that people understand what your script adds, and to prevent conflicting with other scripts. A bad example of not adding a prefix before a variable in $game_temp for a message system called DMS (Dan's Message System) for a text storage variable is:

CODE
class Game_Temp
    attr_accessor :text
    
    alias my_initialize initialize
    def initialize
        my_initialize
        @text = ""
    end
end


A good example with prefixes is:

CODE
class Game_Temp
    attr_accessor :dms_string
    
    alias dms_initialize initialize
    def initialize
        dms_initialize
        @dms_string = ""
    end
end


You can can clearly see that the added variables are for dms. Notice the alias named dms_initialize, always put a different prefix before your aliases to prevent conflicting with other scripts aliased functions.

Anyway, that concludes my tutorial for today, I hope you learned some things by this, and I'm glad to help. If you have any questions you can message me more post in reply to this. Remember, it's good practice to get into these habits, you'll find yourself speeding along when your code is clean and easy to understand.

Thanks,
DeadlyDan
smile.gif


__________________________
Go to the top of the page
 
+Quote Post
   
neclords
post Feb 1 2008, 02:15 AM
Post #2


Love me! I wanna you to love me!
Group Icon

Group: Revolutionary
Posts: 196
Type: Event Designer
RM Skill: Beginner




Thanks..

Very much !!


__________________________

~Time To sWallow tHe Pain and VaniSH OuR HuRt~
Go to the top of the page
 
+Quote Post
   
remody356
post Feb 19 2008, 01:35 PM
Post #3



Group Icon

Group: Member
Posts: 4
Type: Event Designer
RM Skill: Advanced




yeah it is nice, i have been wanting to learn ruby


__________________________

Coming Soon....
Go to the top of the page
 
+Quote Post
   
andani
post Mar 4 2008, 05:17 PM
Post #4


Level 4
Group Icon

Group: Member
Posts: 47
Type: Event Designer
RM Skill: Skilled




Yes, this is quite helpful indeed. Thanks dan. This is a very good start for me.


__________________________





My name's Andani. I am a very blunt person who says what they feel. I usually do not reveal my thoughts except when I feel obliged.

-Andani Yunatta Kappenluthe
Go to the top of the page
 
+Quote Post
   
Dragifon
post Mar 19 2008, 07:00 PM
Post #5





Group:
Posts: 4
Type: Event Designer
RM Skill: Skilled




this is cool! i would like to see a list of commands that can be used in this scripting language though cause i dont know at all how to use it sweat.gif but i would like to learn.

i also have a few of questions.

1. What is RTP and what does it stand for?

2. What is DBS and what does it stand for?

if you can answer these id be exceedingly grateful biggrin.gif
Go to the top of the page
 
+Quote Post
   
YanXie
post Mar 19 2008, 07:08 PM
Post #6


Because Tomorrow Will Surely Come...
Group Icon

Group: Revolutionary
Posts: 1,137
Type: None
RM Skill: Skilled




.....

RTP stand for Run Time Package.

It is used for default graphic for our VX project.

DBS stand for Default Battle System

It is the system which by default available to use for battling purpose in VX.


__________________________
how make teleport to graveyard then your character die?

AWAY FOR VACATION.
NOT HERE UNTIL JAN/FEB 2010 -w-/
Go to the top of the page
 
+Quote Post
   
Zeriab
post Mar 20 2008, 01:49 AM
Post #7


Level 12
Group Icon

Group: Revolutionary
Posts: 196
Type: Event Designer
RM Skill: Skilled




It's a nice tutorial deadlydan happy.gif
The conventions are well explained.
I do not like your spacing convention too much because it is inconsistent with the spacing convention in the default scripts
I would suggest using the same style as in the default scripts. It's easier with just one convention than with two ^^
For example:
CODE
def TestFunction ( x, y, z, string = "DefaultString" )
    tempnumber = 10
    tempstring = string + tempnumber.to_s
    print ( tempstring )
end
would be changed into
CODE
  def TestFunction(x, y, z, string = "DefaultString")
    tempnumber = 10
    tempstring = string + tempnumber.to_s
    print(tempstring)
  end


*hugs*
- Zeriab


__________________________
Go to the top of the page
 
+Quote Post
   
Sheena-Tiger
post Mar 20 2008, 03:31 AM
Post #8


Level 1
Group Icon

Group: Member
Posts: 13
Type: None
RM Skill: Beginner




Konnichiwa!

nicely done smile.gif
i learned programming myself (well, mostly the theory and PHP only) and your examples are very good stuff.
but i need to signs Zeriabs opinion, your type of spacing is a bit much of spacing in my opinion too, Zeriabs example is much more easy to read for me


__________________________

Go to the top of the page
 
+Quote Post
   
Dragifon
post Mar 22 2008, 11:30 AM
Post #9





Group:
Posts: 4
Type: Event Designer
RM Skill: Skilled




QUOTE (puppeto4 @ Mar 19 2008, 09:15 PM) *
.....

RTP stand for Run Time Package.

It is used for default graphic for our VX project.

DBS stand for Default Battle System

It is the system which by default available to use for battling purpose in VX.

Thank you. happy.gif
Go to the top of the page
 
+Quote Post
   
blooDhazE43
post Apr 5 2008, 11:13 AM
Post #10



Group Icon

Group: Member
Posts: 2
Type: Developer
RM Skill: Beginner




good tutorial, but i don't get everything sweat.gif

but i have a favor to ask: can someone tell how i can change the equipmentpossibilitiy?
(like final fantasy tactics advance: you can equip an shield, but you can equip an acessory instead?)
sorry for band english(i am from austria) and thanks in advance
Go to the top of the page
 
+Quote Post
   
barkot
post Apr 22 2008, 01:10 AM
Post #11


Level 3
Group Icon

Group: Member
Posts: 33
Type: Musician
RM Skill: Beginner




omg!
That was helpful! Too bad I dunno how to program things using RGSS2 xD

yours,
Bark =]
Go to the top of the page
 
+Quote Post
   
pewpewpew
post Apr 22 2008, 02:11 AM
Post #12


Level 3
Group Icon

Group: Member
Posts: 36
Type: None
RM Skill: Undisclosed




Hehe, I learnt good form the hard way. I nearly failed my programming class because I often lumped all my code into one little block; named my variables things like "a1" or "b3", and I never added any comments.

Took me a while to shake myself of that habit. I was able to read my own code just fine, but can only imagine my TAs were tearing their hairs out.


__________________________
Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 25th May 2013 - 09:04 PM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker