The RPG Maker Resource Kit

RMRK RPG Maker Creation => General Tutorials and Eventing => RPG Maker General => Tutorials Database => Topic started by: Tsunokiette on December 09, 2006, 12:21:06 AM

Title: Tsuno Sense Scripting Tutorial # 1 : "Hello World!"
Post by: Tsunokiette on December 09, 2006, 12:21:06 AM
Hello lads and lassies, welcome to my second tutorial from a series of tutorials on anything from mapping to scripting.

Today we shall learn the mysteries of the epitome of all things that are programming.

"Hello World !"

Some of you may not have heard this phrase before, and I don't blame you. It used to be one of the most popular ways to teach apprentices of the computer how to program. I don't see it as much anymore, but it works all the same. It teaches you the basics.

RUBY is one of the most useful OO (object oriented) programming languages to have emerged.

Enough of the intro, let's get down to business shall we?

_____________________________________________

The very first funtion of RUBY we will learn is the print command. It is a pre-defined method that doesn't activate the printer (as it's name would imply), rather, it opens a prompt window with the text you supply.

There are four different ways to use this method.

Code: [Select]
print "Text Here"
print("Text Here")
print 'Text Here'
print('Text Here')

You may be saying, "What's the difference?", and you're right, there may not seem to be a difference. However, there is, but let's save that for a later lesson shall we?

Open a new event (give it a graphic) and add the script command.

In the text box, type -

Code: [Select]
print "Hello World !"

Now save, and test the game.

Walk up to and talk to the character, if you did everything correctly, a box should pop up with the words "Hello World!" printed inside it. Click okay (or cancel/X/whatever it is) and close the game window.

That was neat eh? But it doesn't look very professional, after all, there's some annoying quotation marks around the words.

Now I want to introduce you to a varient of the print command.

It's name? Simply p. The difference between print and p is simply that while print shows the quotations, p does not.

So go back into the event, and edit the script command to read the following -

Code: [Select]
p "Hello World !"

Test the game as before and

Hello World !

should appear without quotations in that window. (We will refer to window as a prompt)
Please correct me if I used the wrong term.

That's nice, but typing

Code: [Select]
p "Hello World !"

every time I want the message to show up seems lengthly doesn't it?

We will now create our very first method.

Open the script editor and create a new script at the top of the script list (easier to find). I recommend you name it (without quotes) "Hello World! ***" to keep track of it.

In the text field, type the following -

Code: [Select]
def hi
     p "Hello World !"
end

Let's go over what we just typed.

def tells the interpreter (what is running the scripts) that we are creating a new method. (def is short for define)

hi is the name of the method. Method names always start with a lower case letter and contain no spaces.

p "Hello World !". We should know what this is by now, please remember that the quotations identify the "Hello World !" as text.

end is kind of like the [/blah] tags in HTML and BBL. It tells the interpreter that we're done defining the method.

Now click okay and go back into the event.

Replace the script command text with the following.

Code: [Select]
hi

Test as usual, the same thing should happen as before.

Fun!

_____________________________________________

By now you're probably bored with "Hello World !".

So, let's personalize it shall we?

Firstly, replace the method we created earlier with the following.

Code: [Select]
def hi (name = "World")
     p "Hello #{name} !"
end

Let me explain the differences you see before you.

(name = "World") is what we refer to as an arguement. When we have name = "World", it's saying that if you supply nothing else when you run the method, name should be "World". Arguements are seperated by commas, and can simply be a variable.
(EX: (name = "World", age)

p "Hello #{name} !" is the same as before with one difference. Instead of pure text, it has #{name}. The #{name} is replaced with the value of the variable name in text form. Therefore, whatever name is, it will be included in the prompt.

NOTE: If you use #{variable} in text, you must always use double quotes, if you use single quotes it will print exactly as you typed it! I will explain this in a later lesson.

If you test the game as it is now, (please remember to save each time you make a change), it will once again say "Hello World !", so let's play with it a bit.

Go back into the event and edit the script command text to say the following.

Code: [Select]
hi("NAME HERE")

For me, I would do -

Code: [Select]
hi("Tsunokiette")

which will put out -

"Hello Tsunokiette !".

Neat eh?

While you may not think you've learned much, you really have. Let's go over what you just learned shall we?

_____________________________________________

You've learned the basics of how to use strings.
You've learned how to define a method.
You've learned how to integrate RGSS and Events.
You've learned how to use arguements.
AND
You've learned how to use the #{variable} funtion in strings.

This may not seem like a lot, but these are the most widely used tools in all of programming, it is what we use to test for errors throughout a script.

Until Next Time -
Tsuno Out
Title: Re: Tsuno Sense Scripting Tutorial # 1 : "Hello World!"
Post by: Djangonator on December 09, 2006, 12:25:39 AM
This was really informative and I appreciated it a lot. Like, seriously. u r t3h pwn
Title: Re: Tsuno Sense Scripting Tutorial # 1 : "Hello World!"
Post by: Falcon on December 09, 2006, 12:29:33 AM
Nice, this should help a lot of new scripters :)
Title: Re: Tsuno Sense Scripting Tutorial # 1 : "Hello World!"
Post by: Tsunokiette on December 09, 2006, 05:01:17 PM
Thanks peeps. (for lack of a better term xD)

(EX: "Thanks guy and woman" sounds kind of forced and strange)

Any other comments? I know I only spent like 20 minutes on this, but surely it helped.
Title: Re: Tsuno Sense Scripting Tutorial # 1 : "Hello World!"
Post by: Blizzard on December 11, 2006, 02:03:41 PM
The p command is awesome during in-depth debugging. :D It saved my butt many times. Although I prefer making a debug window with constant information update sometimes.