Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My ruby discoveries
#1
This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.


I have been learning ruby and here are some of my discoveries.


rmpx uses ruby version 1.8.1, I found out by checking the RUBY_VERSION global constant.

The ruby 1.8.1 download is worthwhile if you write scripts.
It comes with better documentation, samples, and a code editor that has syntax checking [Image: alright.gif] .

I think it is a good idea to write scripts and save them as a .rb file, and then load them into rmxp like so.
Code:
require 'Scripts\EightDirectionalMovement.rb'


Then document the public interface (methods, attributes, or constants the user should know about) separate from the code.
This way, the implementation details in the .rb remains a black box.

search directories for scripts are added like this.
Code:
$: << Dir.getwd    # $: is a global array. Each string specifies a directory to be searched for Ruby scripts.
                # Dir.getwd Returns the path to the current working directory  (where the game.exe and .rxproj are)



For example, this code is in my project, in a script file just above Main. I add some directories, then load the scripts I want.
If you want to load scripts used by events you may have to load them in a rmxp script near the top of the list, I dont know, I have not yet worked with events much.
Notice the ' ' strings, they are different then the " " strings, single quotes done use all the \ codes.

Code:
$: << Dir.getwd
$: << 'C:\RPG Maker XP'

require 'Scripts\QuickNewGame.rb'
require 'Scripts\EightDirectionalMovement.rb'


I have the two .rb files loaded by the code examples as a .zip attachment to this post.

Reflection
Some new features require the modification of the original rmxp scripts, for example Postality's 8 direction script needs to replace code in the Game_Player class.
Reflection can examine aspects of the program from within the program itself.
Using reflection it is possible to get 8 direction movement without touching the original Game_Player code.
All you have to do is load a script file like the first code example, or past the new script in an empty rmxp script file and your done.

This code block is for those of you interested in seeing how my first implementation of 8directionalMovement with reflection works.

Code:
class Game_Player                               # Here we name our class Game_Player so it mixes in with the original Game_Player class.
    
    alias_method :old_update_8dir, :update                  # The Alias_method renames an old method.
    alias_method :old_move_down_8dir, :move_down       # By renameing a method we can write our own with the same name, replacing the old one.
    alias_method :old_move_up_8dir, :move_up                #Here we rename all the move_left, move_right ect, and the update method.    
    alias_method :old_move_left_8dir, :move_left
    alias_method :old_move_right_8dir, :move_right
    
    
    def acceptMovementInput?                        #this is code from the origional Game_Player update method.
!(moving?  or $game_system.map_interpreter.running? or @move_route_forcing or $game_temp.message_window_showing)  
    end
    
def update    
  @check_8dir = acceptMovementInput?        # The check_8dir is for saftey, to check 8 directional movement  when player input is taken.
  old_update_8dir                               # Call the original  Game_Player update method.
  @check_8dir = false;
end

def directionalMovement8            # Here is Postality's script. Notice it calls the old move methods, otherwise it would loop infinitely.
case Input.dir8
       when 2 then old_move_down_8dir    
       when 4 then old_move_left_8dir        
       when 6 then old_move_right_8dir        
       when 8 then old_move_up_8dir        
       when 7 then move_upper_left    
       when 9 then move_upper_right    
       when 3 then move_lower_right    
       when 1 then move_lower_left    
end    
end
    
    def move_down(*args)                             # These new move methods will be called by Game_Player's update method.
if @check_8dir then directionalMovement8             # In here we call Postality's 8 directional movement if it is safe.
else old_move_down_8dir(*args) end                  # Else we call the original  move_down.
    end
    def move_left(*args)
if @check_8dir then directionalMovement8
else old_move_left_8dir(*args) end      
    end
    def move_right(*args)
if @check_8dir then directionalMovement8
else old_move_right_8dir(*args) end      
    end
    def move_up(*args)
if @check_8dir then directionalMovement8
else old_move_up_8dir(*args) end      
    end
end



.zip   rmxp_scripts.zip (Size: 1.54 KB / Downloads: 1)
}


Possibly Related Threads…
Thread Author Replies Views Last Post
  Ruby Kickstarter Chrono Cry 0 1,982 03-31-2005, 01:00 PM
Last Post: Chrono Cry



Users browsing this thread: