I'll see what I can add to this.
One of the things that I found to be very frustrating in scripting for XP is the way that instance variables could not be changed with method aliases.
class Interpreter
def update
# do some update stuff
some_val = some_property + other_property
# Check stuff
if (stuff is running)
return
end
# Do other stuff
end
end
Here, trying to alter "some_val" after a bunch of other stuff has taken place was not possible unless the method was replaced and that seriously comprimised compatability. Enterbrain has learned from what works and what doesnt, listened to us, and has changed the way they write scripts from the idea of "do everything in one method" to "do everything with seperate methods". This has increased compatability of scripts through the roof.
Enterbrain Code:
class Game_CharacterBase
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
update_animation
return update_jump if jumping?
return update_move if moving?
return update_stop
endend
With the above code, lets say we wanted to make some changes to the way an Event updates. Lets say also we wanted to do some other Animation stuff. We dont need to touch the Update method, we would make some changes to the Update Animation method.
Your Code: (in a Script)
class Game_CharacterBase
attr_accessor :angle # Brief description
attr_accessor :angle_speed # Another brief description
alias :my_script_name_init_public_members :init_public_members unless $@
def init_public_members
# Call Original or other Alias
my_script_name_init_public_members
# Add New Properties
@angle = 0
@angle_speed = nil
end
def update_character_angle
# Update any Angles
@angle += @angle_speed if @angle_speed
end
alias :my_script_name_update_animation :update_animation unless $@
def update_animation
# Call Original or other Alias
my_script_name_update_animation
# Do stuff to update the Rotation of Sprite
@angle = update_character_angle
end
end
The above script is just an example as more stuff would need to be added for the Sprite, and probably reading Notes or Comments to initially set the Angle Speed value so it wont function as is. But as an example, look just up at Update Animation. Because the code has been modularized, the values can be altered by other scripts as well. For example you write a script that allows Sprite Rotations, the way this code is written will allow others to write scripts that further enhance your code without causing conflicts. For example, someone else adds on to the concept of Angles to create Pendulums. Or something that would relate to Sprite Rotations
NOTE: Sprite Rotations are very expensive and will drastically impact framerate.
The above is the basic framework of most scripts. Initialize your values. I set @angle_speed to nil so it only changes if the property has a non nil value. The update_character_angle method is used where property values are changed, and lastly, the update_animation method is aliased, which will allow your methods to run when update_animation is called.
Lets go a bit more advanced and throw in some Arguments into the mix.
Enterbrain Code:
class Game_CharacterBase
#--------------------------------------------------------------------------
# * Determine Coordinate Match
#--------------------------------------------------------------------------
def pos?(x, y)
@x == x && @y == y
end
end
This time, we will go for a different goal. Something like detecting collisions for big events. Im just making this up at this point so bear with me. In your code, you would create an Alias of pos? and create another new method to do checks. So we will check for a "coordinate match" and adjust the outcome by modifying a method called "pos?(x, y)" which expects two arguments. Your code will start to look something like this:
Your Code:
class Game_CharacterBase
def big_pos?(x, y)
# This returns true if conditions are met
@x == x + 1 && @y == y + 1
end
#--------------------------------------------------------------------------
# * Determine Coordinate Match
#--------------------------------------------------------------------------
alias :my_script_name_pos? 😛os unless $@
def pos?(x, y)
# Detect Collision for Big Characters
return true if (@big_char && big_pos(x, y))
# Run original code and pass unmodified variables to the code
my_script_name_pos?(x, y)
end
end
Point here is just to make sure you pass along any expected arguments, like X, Y to the aliased method name. There is an opportunity here to also change the X and Y values before passing it along to the original method for checking.
Your Code:
class Game_CharacterBase
def round_pos
(@real_x / 256.round), (@real_y / 256.round),
end
#--------------------------------------------------------------------------
# * Determine Coordinate Match
#--------------------------------------------------------------------------
alias :my_script_name_pos? 😛os unless $@
def pos?(x, y)
# If Property is neither false or nil
if @round_stuff
# Round the Real X and Y Values
x, y = round_pos
# Run original code and pass unmodified variables to the code
my_script_name_pos?(x, y)
end
end
The above code here modifies the X and Y values before it is checked. Also notable here is that I did not pass any arguments along to my new method as they were not necessary. The properties that are used in the above code are @properties so they dont need to be passed along as arguments. The ones in X, Y in the arguments will still need to be passed along.
One habit I have gotten into is to use as much documentation as I can for each method. What I do is give a brief description of what the method itself does, and explains what is expected in the Arguments.
#----------------------------------------------------------------------------------
# * on_ice? - Game_CharacterBase
# - Returns true if Character is more than half way on a tile
# and Tile matches the Ice Terrain Tag in Config
# x : Character's Real X Value
# y : Character's Real Y Value
# no_ice : Prevents Ice Movement if set, otherwise nil by default
#----------------------------------------------------------------------------------
def on_ice?(x, y, no_ice = nil)
# Some code
unless no_ice
# Adjust for Looping Maps
x = round_x(x)
y = round_y(y)
# True if Terrain Tag is an Ice Terrain Tag
$game_map.terrain_tags(x, y) == ICE_TERRAIN_TAG
end
end
The above code is also junk I just typed up so it doesnt actually do anything. I also excluded aliasing of any of the update methods and sub-methods called. But what is important here is the documentation of the code. We are usually limited to roughly one line for each line of code. But in the method header where we define what the code is, I tend to put a lot more information than is necessary. In Enterbrains code, each class has its own script entry on the left, but when we write code, that style tends to change because we may need to make modifications to several different classes at once. Thus, I find it makes things a lot easier to repeat the Class Name so I can tell the difference between Player and Event classes as both of those classes have def update and many other similar named methods that each work differently for both classes. Player looks at Input and Events do not. The second line I use for paragraphs that can fully describe this particular method. Then I list the Arguments. This is important because simply putting in *arg does not tell you what the argument is. I end up passing whole objects a lot of time, so if an expected argument is looking for an RPG::Move_Route, I can work with those properties properly.
#----------------------------------------------------------------------------------
# Turn Repeat Off - Game_CharacterBase
# - Disables Repeat on Move Routes without
# erasing the Move Route itself
# move_route : RPG::Move_Route
#----------------------------------------------------------------------------------
def turn_repeat_off(move_route)
move_route.repeat = false
end
If I were to pass anything other than an RPG Move Route as an argument, most likely the repeat property of the move_route as an argument would not exist and it would cause the script to cause a crash. The above code is exceptionally simplified so it is almost not necessary, but when you get into more complex coding where one method does a lot of different things, then it is highly useful to explain what is expected in the arguments. Again, this is also done as its own method so it can be modified by other scripts.
#----------------------------------------------------------------------------------
# Turn Repeat Off - Game_CharacterBase
# - Disables Repeat on Move Routes without
# erasing the Move Route itself
# move_route : RPG::Move_Route
#----------------------------------------------------------------------------------
alias :my_script_turn_repeat_off :turn_repeat_off unless $@
def turn_repeat_off(move_route)
# Return if Event is used for Background Animation
return if @background_event
# Call Original or any other Aliases of this Method
turn_repeat_off(move_route)
end
The above code would be an additional script that only further enhances an existing non enterbrain script. This is possible because not everything is nested into one method. If you are learning to script and have a copy of RMXP, open it up and compare Interpreter Update to RMVXA. You'll see how XP is written in such a way that Update can not be modified without either comprimising compatability or murdering your framerate. The VXA way allows just about everything to be changed and retain a very high degree of compatability because it calls sub-methods instead of being heavily nested like XP.
In conclusion, "Coding the RM Way" needs to be a bit more specific. Coding the RMXP way is kind of bad juju. It is possible, but far more difficult. We need to understand WHY Enterbrain decided to significantly change their style from heavy nesting to sub-modularizing everything and learn from what they have to teach in their code styles. The thing is, we can code the RMVXA way in RMXP scripts as well, and such a style will allow many people to enjoy quality scripts for both VXA and XP.