- 664
- Posts
- 16
- Years
- Age 33
- England
- Seen May 16, 2024
Ok, so we've learnt about printing and declaring variables, and you may have learnt about the different types of variables. Just make sure you've read Lesson 1 first! So now onto the next lesson.
The structure of a class.
Let's jump straight in shall we? But first remember to make your Lesson 2 script section won't you.
Alright lets go with the explanation.
Line 1
The word "class" tells the game that we are writing a new class, and the class name here is Class_Badger. We must have a name for the class, otherwise we wouldn't have anything to call in the game would we? (More on calling later).
Line 2
The word "def" shows that we are writing a method, and the method name in this case is method_one. You must have a name for a method otherwise there is nothing for it to run, and it will probably cause errors.
Line 3
The word "end" tells the game that we have finished writing that method and we can move onto the next one.
Line 4
The next method
Line 5
Ends the second method
Line 6
This "end" is special because it ends the class and tells the game that there is nothing else in this class and to stop the process of running it.
So that's the basic structure of a class. But it is basic. So let's add some padding.
I've added some comment lines to explain it all as this is pretty easy and we've done it before. (The variables and printing.) Always remember to indent (RMXP does it for you) because it will be easier to understand in big scripts.
Ok, so if you put that script section into the Lesson 2, everything should be fine right? Wrong. To see the effects of this script you have just written, you need to create a new event on a map, and set the starting party position to that map. Ok, in the event you have just created, give it a graphic or sprite so you can see where it is. Double click on the character you have just made on the map, and double click the @> in the big white space to the left. Navigate yourself to the third page of the popup and choose Script... at the bottom. In the popup that...well...pops up, type in Class_Badger.new This shows the game that we want to call a script section. So for any other class that you write, to call it, it will be Class_Name.new Then run your game and go up to the character and press the spacebar. Look! The popup said badger!
Congratulations! Your first working class!
But that's not all for this lesson. Just to check if it's all sunk in, i want you to write me a class, below are the things you have to do.
Homework!
Create a class called Class_Yourname (Yourname being your name obviously).
The class needs to have two methods, the first one being the 'def initialize' one.
In the initialize method, declare 2 variables and store them in another.
In the second method, print the 3rd variable (The one with the two variables stored inside).
End the class and hand me in your homework here to this thread.
Until the next lesson!
- Crazyninjaguy ^^
The structure of a class.
Let's jump straight in shall we? But first remember to make your Lesson 2 script section won't you.
Code:
class Class_Badger # Line 1
def method_one # Line 2
end # Line 3
def method_two # Line 4
end # Line 5
end # Line 6
Alright lets go with the explanation.
Line 1
The word "class" tells the game that we are writing a new class, and the class name here is Class_Badger. We must have a name for the class, otherwise we wouldn't have anything to call in the game would we? (More on calling later).
Line 2
The word "def" shows that we are writing a method, and the method name in this case is method_one. You must have a name for a method otherwise there is nothing for it to run, and it will probably cause errors.
Line 3
The word "end" tells the game that we have finished writing that method and we can move onto the next one.
Line 4
The next method
Line 5
Ends the second method
Line 6
This "end" is special because it ends the class and tells the game that there is nothing else in this class and to stop the process of running it.
So that's the basic structure of a class. But it is basic. So let's add some padding.
Code:
class Class_Badger # Name of the class
def initialize # The initialize method. If that isnt there, nothing works
@badger = 'Badgers Rule!' # Stores a value in the variable @badger
badger2 # Calls the next method
end # Ends the method
def badger2 # The method we just called
p @badger # Print the variable we declared earlier
end # Ends this method
end # Ends Class_Badger
I've added some comment lines to explain it all as this is pretty easy and we've done it before. (The variables and printing.) Always remember to indent (RMXP does it for you) because it will be easier to understand in big scripts.
Ok, so if you put that script section into the Lesson 2, everything should be fine right? Wrong. To see the effects of this script you have just written, you need to create a new event on a map, and set the starting party position to that map. Ok, in the event you have just created, give it a graphic or sprite so you can see where it is. Double click on the character you have just made on the map, and double click the @> in the big white space to the left. Navigate yourself to the third page of the popup and choose Script... at the bottom. In the popup that...well...pops up, type in Class_Badger.new This shows the game that we want to call a script section. So for any other class that you write, to call it, it will be Class_Name.new Then run your game and go up to the character and press the spacebar. Look! The popup said badger!
Congratulations! Your first working class!
But that's not all for this lesson. Just to check if it's all sunk in, i want you to write me a class, below are the things you have to do.
Homework!
Create a class called Class_Yourname (Yourname being your name obviously).
The class needs to have two methods, the first one being the 'def initialize' one.
In the initialize method, declare 2 variables and store them in another.
In the second method, print the 3rd variable (The one with the two variables stored inside).
End the class and hand me in your homework here to this thread.
Until the next lesson!
- Crazyninjaguy ^^