Coding in RM is not the same as just coding in Ruby.
We define the RM way to be the set of best practices that RM scripters should adhere to.
The goal of writing the RM way is to maximize compatibility and most importantly user-friendliness.
What are some things people should do or avoid when coding in RM?
Data Localization
Data should be localized, not spread across various places.
Note-tags are great for accomplishing this.
Any attributes that are intended to be associated with instances of a class should be stored with that instance.
If you have a script that provides new weapon attributes, users should not set those attributes up in a script configuration. If you decide to remove a weapon, you don't want to have to go through all your scripts and data files looking for any leftover data associated with that weapon.
There may be good reasons for storing everything together in a script configuration, but generally if a user is looking at their weapon setup, you don't want them to keep in mind that there are 20 other places to look. This becomes especially important if there are multiple developers on the same project.
Method Aliasing
Generally, it is better to alias methods, rather than overwriting them. Exceptions do exist, but usually if it can be done with aliasing, there is no reason to overwrite the original methods. Overwriting methods because "my way is just better" should not be done.
Also, take note on what your script overwrite or add, and inform users about possible conflicts and general methods to resolve them. For example, If you are making a custom script that changes how damages are calculated, you may want to inform the users that the script might cause problems with some battle systems, and tell them the script placement that will most likely work.
Best practices, eh?
Here's some of my favorites - might save you a lot of headaches:
NEVER change the value of a variable directly. Use an accessor method (I'm not talking about the i in index loops...).
That let's you put in bounds checking and any other debug code / failsafe methods in place.
You should have bounds checking and failsafe methods in place: don't assume a variable will have a valid value.
The no Global Variable rule is true in almost cases (that's not just a Ruby thing).
Aliasing vs. Overwriting: If you to be able to 'drop in' other scripts that expect certain behaviors from the core classes and have them 'just work', you should alias. If you want to be _absolutely_ sure that the game always uses your method and not the original, you should overwrite. [Note - in either case you should leave the original script alone and put your changes down at the bottom].
Format your code properly. The 2 space indent is pretty standard in Ruby. If you're used to three, fine - but be consistent about it. You should always be able to 'see' where you are without having to count begins and ends.
Don't try to get fancy with the ternary operator. If it's not immediately clear what's going on, use an if/else.
Use parentheses rather than relying on order of operations for compound conditionals.
Comment your code liberally. Imagine you just found this script somewhere: is there enough information for you to know what it does?
Catching Errors
Use rescues sparingly.
If something's wrong, the dev has likely done something wrong.
Especially if you're working with formulas, you REALLY don't want to return a default value and not tell the dev that something happened.
The default Damage evaluation code, for example, returns 0 if the formula is wrong and doesn't make it apparent that no damage was dealt cause the formula was wrong.
Exceptions have their place, but don't use it to protect devs from making mistakes.
"User" in this case refers to anyone that's using the script. Could be people on the team, or anyone using RM to develop the project.
The event commands are built into RM so there isn't much to deliver on the script author's end.
Already has been mentioned, but I'm going to reiterate and underline it: Comment your code!
There's nothing worse than downloading a fairly complex script, only to find out that the only comments are in the file header and the author has an apparent hatred of whitespace. This makes the code extremely hard to read for everyone except the person who wrote it.
I like making changes to scripts to suit my needs. If I find multiple scripts that perform a given task reasonably well, I'll always opt for the more readable version, even if it's missing a couple minor features.
I pinned this for you guys 'w')b
Good thread.
Meaningful Variable/Property Names
Even abbreviated names can sometimes be difficult to understand, for someone else trying to decipher your code.
This includes not giving methods and variables the same name. Knowing when to use @pos (the class/module variable), self.pos (the method) or pos (the local method variable) is just confusing, and benefits nobody.
Asynchronous Requests
If you want to run something, don't just immediately execute it. Instead, you should request the engine that you want to do something and let it determine when it will process it.
The engine is constantly updating all sorts of things: graphics, input, scenes, other sprites, a bunch of threads, numerous events, etc; who knows what kind of problems can occur if you decide to cut in and suddenly tell it to do something else.
At best, your game would freeze while it processes your request. Worse, you might throw things into an inconsistent state and then your game crashes randomly with no real way to reproduce it.
This pattern can be observed in various places, such as when you want to call a common event, or you want to call the menu from the map scene, or you want to transfer to another map.
Meaning explicitly tell the engine how your computations should proceed
for example if you want to multiply the HP by 10, then divide the results by 2, write it as
(object.hp*10)/2
instead of
object.hp*10/2
sure it might work the same, but as your computations go more complex, using parentheses will become more useful and needed...
or for conditions
group your compound conditions correctly
like if you want to set a condition where it should be If A and BorC
if a and (b or c)
Unique module, class, and alias method name
In order to raise compatibility, if you're going to make a module or alias, make sure the name is not too general.
You can insert you name or your trademark in your script.
In my case, if I want to make an alias in a certain method, I would like to use this format
alias theo_scriptname_orimethod_name orimethod_name
That also apply for class name. For example
Sprite_Icon is general name. It's better if we define it in different way, such as Sprite_IconScriptName
Optimize the algorithm of methods... and your way of coding.
Okay. You've made your scripts work. But, instead of copy-pasting the working codes, make a method for calling the codes so when you need that code to be used, simply call the method. It makes your codes clean. This way of coding is also called Encapsulation, AFAIK.
Also, take a look back to the iterations and usage of variables. Handle them with care.
I've found commenting a script to be very helpful to the original developer, later on. I'd say it's important to say not just what is a parameter's name in the comment, but most importantly, if it needs to be a particular type.
Especially in each documented user-callable function, set default parameters, where reasonable and, within the function be sure to check for nil and other unexpected data types. It's good to make use of the object functions respond_to? and is_a?. For example: parameter.respond_to?🙂to_s) can confirm something can have the "to_s" method called on it. And parameter.is_a?(Array) checks to see if it is an array type.
Also, if you have a critical problem, it's good to raise an exception, with clear, easy-to-understand text, such as "Unable to start battle - notetag from Actor[1] (Paula) contains a timeout less than 0" It doesn't need to be player-understandable, but it does need to make sense to the game developer. This saves them a lot of time and frustration.
And, if the global $DEBUG is set to true, it's a good idea to print out key information to the console (use puts or print). This can help the game developer immensely during playtesting.
These are just good programming practices, and in my testing, I've found them indispensable. Ruby's loose data typing can cause very hard-to-detect problems, otherwise.
Make Instructions As Friendly As Posibble
Well, I just want to add something that actually is quite important. While the script work really nice and the code is pretty neat, but if the instructions oo on how to use the script is not made properly, the user might scream and curse the script maker. Escpecially if the user is totally blind about scripting. There's some scripts I ever found, has pretty bad instruction on how to use it. It kinda like the script maker is lazy to provide the best instruction he/she can do. This quite important I believe.