When writing a Ruby Script, were would you place it inside of RPG Maker XP in order for your script you wrote to work?
New to Ruby Scripts: Basic Question
Learning Ruby and RGSSxOpen your Script Editor from the menu at the menu bar at the top of the screen (or by hitting the hotkey - I think it's F11).
If you've already created a script that you just want to paste in, usually the best place to paste it would be near the bottom of the script list, right above (not below!) the "Main" script. To do so, you can right-click the line above Main, choose "Insert" (I think that's what it's called), and a new, blank page will be there for you. Paste it there.
Your script will need to include the class definition (I think it's something like class GameBattler, for example, to define the GameBattler class as the class you're working in, with an extra end after the rest of your code.) so the game knows what class you're trying to modify. If your scripting doesn't include a class definition, you'll need to find the class within the existing script list and paste it into there instead (anywhere below the class definition and before the final end... and not in the middle of another method, obviously).
If you've already created a script that you just want to paste in, usually the best place to paste it would be near the bottom of the script list, right above (not below!) the "Main" script. To do so, you can right-click the line above Main, choose "Insert" (I think that's what it's called), and a new, blank page will be there for you. Paste it there.
Your script will need to include the class definition (I think it's something like class GameBattler, for example, to define the GameBattler class as the class you're working in, with an extra end after the rest of your code.) so the game knows what class you're trying to modify. If your scripting doesn't include a class definition, you'll need to find the class within the existing script list and paste it into there instead (anywhere below the class definition and before the final end... and not in the middle of another method, obviously).
It's class, not def.
def keyword is for a function, class keyword is for an object blueprint.
Thanks for the correction @TheoAllen - my XP has been broken for a while now and for whatever reason I had it in my mind that it was different in RGSS1. Corrected my last post.
RGSS Script forum is for sharing your script. I've moved this to Learning Ruby and RGSSx, as I believe it fits there best.
Usual Steps to Make a Script Work
Define classes and modules using the class and module keywords. Some people define their classes directly while others prefer to encapsulate them in modules. Either of those methods is fine.
There you get 2 versions of MyClass class. They're completely different. The first class can be called this way:
@variable = MyClass.new
While the other would required an addition...
@another_variable = MyModule::MyClass.new
As you have been told so far, you define it first and they you are able to call it.
The alias Affairs
Here is where it comes tricky. Whenever a scripter preferred to alias a method, meaning he or she symbolically renamed it to allow him or her to add more code without dissing the original one, things might get ugly.
If just a single scripter aliased any given method, let us say it is Game_Battler's skill_effect, there should be no problem with it. Just copy and paste it in the script editor before Main script.
What happens when several scripters have aliased or rewritten that skill_effect method?
The End of the In Game World!
Incompatibilities are expected to overthrow your in game kingdom and force their king or queen (the game developer?) to abdicate.
Yeah, I know I sounded fatalistic here XD but somehow you can say those things do happen. :s
If any scripter overrides a method like skill_effect and you are fine with that, then it must be placed below Scene_Debug or first in the Materials sections depending on your RM version.
Then you would need to paste any other script affecting skill_effect below that override... and hope your game will not crash harshly or look buggy.
Usually scripters recommend not to touch the default scripts directly, meaning you do not make any edits there. You would just add a new piece of code the way I mentioned a while ago.
Custom scripts that DO NOT INTERFERE with default scripts are safe, usually...
Define classes and modules using the class and module keywords. Some people define their classes directly while others prefer to encapsulate them in modules. Either of those methods is fine.
Code:
class MyClass
def my_method
end
end
module MyModule
class MyClass
def my_method
end
end
end
There you get 2 versions of MyClass class. They're completely different. The first class can be called this way:
@variable = MyClass.new
While the other would required an addition...
@another_variable = MyModule::MyClass.new
As you have been told so far, you define it first and they you are able to call it.
The alias Affairs
Here is where it comes tricky. Whenever a scripter preferred to alias a method, meaning he or she symbolically renamed it to allow him or her to add more code without dissing the original one, things might get ugly.
If just a single scripter aliased any given method, let us say it is Game_Battler's skill_effect, there should be no problem with it. Just copy and paste it in the script editor before Main script.
What happens when several scripters have aliased or rewritten that skill_effect method?
The End of the In Game World!
Incompatibilities are expected to overthrow your in game kingdom and force their king or queen (the game developer?) to abdicate.
Yeah, I know I sounded fatalistic here XD but somehow you can say those things do happen. :s
If any scripter overrides a method like skill_effect and you are fine with that, then it must be placed below Scene_Debug or first in the Materials sections depending on your RM version.
Then you would need to paste any other script affecting skill_effect below that override... and hope your game will not crash harshly or look buggy.
Usually scripters recommend not to touch the default scripts directly, meaning you do not make any edits there. You would just add a new piece of code the way I mentioned a while ago.
Custom scripts that DO NOT INTERFERE with default scripts are safe, usually...
A custom script that doesn't interfere on some level with the default scripts basically won't do anything, because there will be no entry point for it. Even the simplest of scripts is going to have to do something to insert itself into the default code at some point to be able to have its code processed.
Not really, let's say it's a custom scene like a bestiary or a journal that doesn't get the menu scene involved.
You'd just need to make the appropriate script call like $scene = Bestiary.new or SceneManager.goto(Bestiary)
Besides I've published such scripts like mini games plus many others and I didn't touch the default scripts or just recreated or combined existing scripts into a single one by naming it CustomScript or the like for convenience.
Besides I'm omitting the hidden scripts like Sprite or Window as to make it easy for the OP to get a grasp on script placement or even development.
Now that I think about it, subclassing or inheritance might be another topic to cover here but later on.
You'd just need to make the appropriate script call like $scene = Bestiary.new or SceneManager.goto(Bestiary)
Besides I've published such scripts like mini games plus many others and I didn't touch the default scripts or just recreated or combined existing scripts into a single one by naming it CustomScript or the like for convenience.
Besides I'm omitting the hidden scripts like Sprite or Window as to make it easy for the OP to get a grasp on script placement or even development.
Now that I think about it, subclassing or inheritance might be another topic to cover here but later on.
Original thread: https://forums.rpgmakerweb.com/threads/new-to-ruby-scripts-basic-question.124381/