Help - Search - Members - Calendar
Full Version: Super Simple Vehicle Script
RPG RPG Revolution Forums > Scripting > Script Submissions > RGSS-Submissions
amaranth
I'm moving all of my scripts from rmxp.org to this site. I like it here better.

Introduction
This is a vehicle system that is similar to the Final Fantasy system. In this system, you get vehicles (a canoe, ship, and dragon), the world tileset for Aveyond 2 (free to use in freeware games), and some autotiles.

Demo
You can download the demo here: DOWNLOAD VEHICLE SYSTEM DEMO HERE

Screenshot


Script
You really should download the demo, because this system uses a mixture of script, map events, common events, variables, and switches. However, if you think you can figure it out on your own, here is the vehicle system script:
CODE
class Game_Character
  def passable?(x, y, d)
    
    # Get new coordinates
    new_x = x + (d == 6 ? 1 : d == 4 ? -1 : 0)
    new_y = y + (d == 2 ? 1 : d == 8 ? -1 : 0)
    
    # If coordinates are outside of map
    unless $game_map.valid?(new_x, new_y)
      return false
    end
    
    # If through is ON
    if @through
      return true
    end
        
    # If unable to leave first move tile in designated direction
    unless $game_map.passable?(x, y, d, self)
      return false
    end
    
    # If unable to enter move tile in designated direction
    unless $game_map.passable?(new_x, new_y, 10 - d)
      return false
    end
        
    #check terrain for vehicle
    check_terrain(new_x, new_y)    
      
    # Loop all events
    for event in $game_map.events.values
      # If event coordinates are consistent with move destination
      if event.x == new_x and event.y == new_y
        @state = true
        # If through is OFF
        unless event.through
          # If self is event
          if self != $game_player
            return false
          end
          # With self as the player and partner graphic as character
          if event.character_name != ""
            return false
          end
        end
      end
    end
    
    if @state == false
      return false
    end      
    
    # If player coordinates are consistent with move destination
    if $game_player.x == new_x and $game_player.y == new_y
      # If through is OFF
      unless $game_player.through
        # If your own graphic is the character
        if @character_name != ""
          return false
        end
      end
    end
      
    
    return true
    
  end
  
  #--------------------------------------------------------------------------
  # * Check the terrain for the vehicle
  #   @terrain (0=none, 1=river, 2=ocean, 3=air, 4=ground)
  #   @vehicle (0=foot, 1=canoe, 2=boat, 3=dragon)
  #   @state (true=passable tile, false=unpassable tile)
  #--------------------------------------------------------------------------
  def check_terrain(new_x, new_y)  
    @state = false
    @terrain = $game_map.terrain_tag(new_x, new_y)
    @vehicle = $game_variables[1]
    
    @state = true if @vehicle == 0 && @terrain == 4  #foot & ground
    @state = true if @vehicle == 1 && @terrain == 3  #canoe & river
    @state = true if @vehicle == 2 && @terrain == 2  #boat & ocean
    @state = true if @vehicle == 3                   #dragon
    @state = true if @terrain == 0
  end

end

class Game_Player < Game_Character
  
  alias in_vehicle_alias update
  #--------------------------------------------------------------------------
  # * Memorize actor graphic
  #--------------------------------------------------------------------------      
  def actor_memorize
    @actor_name = $game_actors[1].character_name
    @actor_hue = $game_actors[1].character_hue
    @battler_name = $game_actors[1].battler_name
    @battler_hue = $game_actors[1].battler_hue
    return true
  end

  #--------------------------------------------------------------------------
  # * Restore actor graphic
  #--------------------------------------------------------------------------      
  def actor_restore
    actor = $game_actors[1]
    actor.set_graphic(@actor_name.to_s, @actor_hue, @battler_name.to_s, @battler_hue)
    $game_player.refresh
    return true
  end    
  
  #--------------------------------------------------------------------------
  # * Enter a vehicle
  #--------------------------------------------------------------------------        
  def enter_vehicle(type)
    
    $game_system.menu_disabled = true
    $game_system.save_disabled = true    
    
    if type != "dragon"
      @through = true
      move_forward
      @through = false    
    end
    
    actor_memorize
    actor = $game_actors[1]    
    actor.set_graphic(type, @actor_hue, @battler_name.to_s, @battler_hue)
    
    $game_switches[1] = false    #Canoe Off    
    $game_switches[2] = false    #Boat Off    
    $game_switches[3] = false    #Dragon Off        
    
    if type == "canoe"
      $game_variables[1] = 1       #set terrian
      $game_switches[1] = true    #Canoe On
      Audio.bgm_play("Audio/BGM/047-Positive05", 100, 100) #play canoe music
  
    elsif type == "boat"
      $game_variables[1] = 2       #set terrian
      $game_switches[2] = true    #Boat On      
      Audio.bgm_play("Audio/BGM/046-Positive04", 100, 100) #play ship music
      
    elsif type == "dragon"
      $game_variables[1] = 3       #set terrian
      $game_switches[3] = true    #Dragon On  
      Audio.bgm_play("Audio/BGM/045-Positive03", 100, 100) #play dragon music
      
    end
    
    $game_player.refresh
    $game_map.refresh
    
    return true
  end

  #--------------------------------------------------------------------------
  # * Enter a vehicle
  #--------------------------------------------------------------------------        
  def exit_vehicle

    $game_system.menu_disabled = false    
    $game_system.save_disabled = false
    
    if @terrain == 4
            
      if $game_switches[1] == true      #getting off canoe
        canoe = $game_map.events[1]     #get canoe event
        canoe.moveto(x, y)              #move to player's x, y coords
        $game_switches[1] = false       #Canoe Off  
        $game_variables[2] = x          #set canoe x coord
        $game_variables[3] = y          #set canoe y coord
        @through = true
        move_forward
        @through = false          
        
      elsif $game_switches[2] == true   #getting off boat
        boat = $game_map.events[2]      #get boat event
        boat.moveto(x, y)               #move to player's x, y coords        
        $game_switches[2] = false       #Boat Off    
        $game_variables[4] = x          #set boat x coord
        $game_variables[5] = y          #set boate y coord  
        @through = true
        move_forward
        @through = false                  
        
      elsif $game_switches[3] == true   #getting off dragon
        dragon = $game_map.events[3]    #get dragon event
        dragon.moveto(x, y)             #move to player's x, y coords        
        $game_switches[3] = false       #Dragon Off    
        $game_variables[6] = x          #set dragon x coord
        $game_variables[7] = y          #set dragon y coord        
      end
      
      # get off vehicle
      actor_restore
    
      # reset terrain
      $game_variables[1] = 0          
    
      # play walking music
      Audio.bgm_play("Audio/BGM/044-Positive02", 100, 100) #play music
      
      $game_player.refresh  
      $game_map.refresh    
    
    end
  
  end
  
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    in_vehicle_alias
    # if not on ground terrain, check for boat exit
    if $game_variables[1] != 0
      if Input.trigger?(Input::C)
        exit_vehicle
      end  
    end
  end

end


Add An Additional Vehicle
1. Create a switch for your new vehicle. Call it "using vehicle" (replace vehicle with the name of your vehicle).

2. Create two variables for your vehicle. Call them "vehicle x" and "vehicle y" (replace vehicle with the name of your vehicle).

3. Add an event on your map for your new vehicle. Set it up to look like the other vehicle events. Modify the switches on the event so that they are for your vehicle, not the dragon, canoe, or boat vehicle. Modify the script call in the event so that it is for your vehicle. For example:
CODE
$game_player.enter_vehicle("hovercraft")


4. Put your vehicle sprite in your project's "characters" folder. Make sure that the name of the vehicle is exactly what you specified in step 3. For example, hovercraft.png

5. Get a peice of paper and write down the slot numbers for your switch, your variables, and the event you created for your vehicle. You will need to reference them later in your code.

6. Open the script editor and go to the Vehicle Script section.

7. In def check_terrain, add a new if statement for your vehicle. For example:
CODE
    @state = true if @vehicle == 4 && @terrain == 5  #hovercraft & swamp


8. In def enter_vehicle, add the switch you created for your vehicle to the list. For example:
CODE
    $game_switches[283] = false    #turn off the "using hovercraft" switch


9. In def enter_vehicle, add a new if statement for your vehicle. For example:
CODE
    elsif type == "hovercraft"
      $game_variables[1] = 5       #set terrian
      $game_switches[4] = true    #Hovercraft On  
      Audio.bgm_play("Audio/BGM/045-Positive03", 100, 100)


10. In def exit_vehicle, add a new if statement for your vehicle. For example:
CODE
      elsif $game_switches[4] == true   #getting off hovercraft
        dragon = $game_map.events[4]  #get hovercraft event
        dragon.moveto(x, y)                 #move to player's x, y coords        
        $game_switches[4] = false        #turn "using hovercraft" off    
        $game_variables[8] = x             #set hovercraft x coord
        $game_variables[9] = y             #set hovercraft y coord


Credits
SephirothSpawn: I made this script after studying SephirothSpawn's (mr. talented!) vehicle script.
DerVVulfman: Enhancing the system!

Bugs
If you find a bug, let me know! I'm crazy busy right now, but I will try to fix any problems. Or, if an advanced scripter wants to take over this project, feel free to do so (again, i'm crazy busy with work). There are some gotchas with this system:
1. Don't use terrain flags on any map but your world map. (if you do, put an if statement in the code so that the vehicle system only works with your world map.
2. If you use a tileset background for an event on your world map, your vehicle will go over it. Use an empty character sprite if you run into problems.
Blizzard
That looks nice. It seems to me that it's better organized than the other vehicle script I once saw. I think it was Seph's, I'm not sure, though.
Akumasenshi
Hmm, now if I only I had a good Futuristic Motorcycle sprite xD, very nice!
inRedGames
I added a new vehicle but when I get in it and move, I still see it in its original spot!
Then when I get out, it goes back to its starting position.

Also, how do I make it so the vehicle can travel over land AND forests?


Oh yeah this is unrelated but I have a jumping script and I was wondering if it was possible to disable jumping on the world map?

Sorry for being such a nooberton!

whoooopdeeeedo
I am having the same situation. When I have the character enter the canoe, the canoe will move but the graphic doubles and one stays in the place it was originally.
Another problem I am having is, I have set the terrain tags and the canoe floats on the water AND on the land. And when I have the character enter the canoe, the canoe moves where I have the arrows move it, but I cannot exit the canoe.
I have the demo. I have seen the events and common events and scripts but cannot figure out what I am doing wrong.
Hadeki
Um... I really like this script.
But where did you get the tileset for your demo? I really like it and I want to use it in my game.
Neyon
Can I post this script in other blog ?
Neyon
WOW
This script is awesome, I was looking for it
CJC
This script works well, except it changes the BGM whenever you exit a vehicle. How do you make it so it changes to whatever music was playing before you entered a vehicle rather than a certain song?
Night5h4d3
I cant say this will work (but I hope it will), because RMXP has been bugging out for me, so I had to whip this up using the help files.

CODE
#-------------------------------------
# RGSS2 import
#-------------------------------------
module RPG
  class BGM < AudioFile
    @@last = BGM.new
    def play
      if @name.empty?
        Audio.bgm_stop
        @@last = BGM.new
      else
        Audio.bgm_play("Audio/BGM/" + @name, @volume, @pitch)
        @@last = self
      end
    end
    def self.stop
      Audio.bgm_stop
      @@last = BGM.new
    end
    def self.fade(time)
      Audio.bgm_fade(time)
      @@last = BGM.new
    end
    def self.last
      @@last
    end
  end
end
#-------------------------------------
# end RGSS2 import
#-------------------------------------

class Game_Character
  def passable?(x, y, d)
    
    # Get new coordinates
    new_x = x + (d == 6 ? 1 : d == 4 ? -1 : 0)
    new_y = y + (d == 2 ? 1 : d == 8 ? -1 : 0)
    
    # If coordinates are outside of map
    unless $game_map.valid?(new_x, new_y)
      return false
    end
    
    # If through is ON
    if @through
      return true
    end
        
    # If unable to leave first move tile in designated direction
    unless $game_map.passable?(x, y, d, self)
      return false
    end
    
    # If unable to enter move tile in designated direction
    unless $game_map.passable?(new_x, new_y, 10 - d)
      return false
    end
        
    #check terrain for vehicle
    check_terrain(new_x, new_y)    
      
    # Loop all events
    for event in $game_map.events.values
      # If event coordinates are consistent with move destination
      if event.x == new_x and event.y == new_y
        @state = true
        # If through is OFF
        unless event.through
          # If self is event
          if self != $game_player
            return false
          end
          # With self as the player and partner graphic as character
          if event.character_name != ""
            return false
          end
        end
      end
    end
    
    if @state == false
      return false
    end      
    
    # If player coordinates are consistent with move destination
    if $game_player.x == new_x and $game_player.y == new_y
      # If through is OFF
      unless $game_player.through
        # If your own graphic is the character
        if @character_name != ""
          return false
        end
      end
    end
      
    
    return true
    
  end
  
  #--------------------------------------------------------------------------
  # * Check the terrain for the vehicle
  #   @terrain (0=none, 1=river, 2=ocean, 3=air, 4=ground)
  #   @vehicle (0=foot, 1=canoe, 2=boat, 3=dragon)
  #   @state (true=passable tile, false=unpassable tile)
  #--------------------------------------------------------------------------
  def check_terrain(new_x, new_y)  
    @state = false
    @terrain = $game_map.terrain_tag(new_x, new_y)
    @vehicle = $game_variables[1]
    
    @state = true if @vehicle == 0 && @terrain == 4  #foot & ground
    @state = true if @vehicle == 1 && @terrain == 3  #canoe & river
    @state = true if @vehicle == 2 && @terrain == 2  #boat & ocean
    @state = true if @vehicle == 3                   #dragon
    @state = true if @terrain == 0
  end

end

class Game_Player < Game_Character
  
  alias in_vehicle_alias update
  #--------------------------------------------------------------------------
  # * Memorize actor graphic
  #--------------------------------------------------------------------------      
  def actor_memorize
    @actor_name = $game_actors[1].character_name
    @actor_hue = $game_actors[1].character_hue
    @battler_name = $game_actors[1].battler_name
    @battler_hue = $game_actors[1].battler_hue
    return true
  end

  #--------------------------------------------------------------------------
  # * Restore actor graphic
  #--------------------------------------------------------------------------      
  def actor_restore
    actor = $game_actors[1]
    actor.set_graphic(@actor_name.to_s, @actor_hue, @battler_name.to_s, @battler_hue)
    $game_player.refresh
    return true
  end    
  
  #--------------------------------------------------------------------------
  # * Enter a vehicle
  #--------------------------------------------------------------------------        
  def enter_vehicle(type)
    
    $game_system.menu_disabled = true
    $game_system.save_disabled = true    
    
    if type != "dragon"
      @through = true
      move_forward
      @through = false    
    end
    
    actor_memorize
    $original_bgm = RPG::BGM.last ## Night5h4d3 edit ##
    actor = $game_actors[1]    
    actor.set_graphic(type, @actor_hue, @battler_name.to_s, @battler_hue)
    
    $game_switches[1] = false    #Canoe Off    
    $game_switches[2] = false    #Boat Off    
    $game_switches[3] = false    #Dragon Off        
    
    if type == "canoe"
      $game_variables[1] = 1       #set terrian
      $game_switches[1] = true    #Canoe On
      Audio.bgm_play("Audio/BGM/047-Positive05", 100, 100) #play canoe music
  
    elsif type == "boat"
      $game_variables[1] = 2       #set terrian
      $game_switches[2] = true    #Boat On      
      Audio.bgm_play("Audio/BGM/046-Positive04", 100, 100) #play ship music
      
    elsif type == "dragon"
      $game_variables[1] = 3       #set terrian
      $game_switches[3] = true    #Dragon On  
      Audio.bgm_play("Audio/BGM/045-Positive03", 100, 100) #play dragon music
      
    end
    
    $game_player.refresh
    $game_map.refresh
    
    return true
  end

  #--------------------------------------------------------------------------
  # * Enter a vehicle  
  #--------------------------------------------------------------------------        
  def exit_vehicle

    $game_system.menu_disabled = false    
    $game_system.save_disabled = false
    
    if @terrain == 4
            
      if $game_switches[1] == true      #getting off canoe
        canoe = $game_map.events[1]     #get canoe event
        canoe.moveto(x, y)              #move to player's x, y coords
        $game_switches[1] = false       #Canoe Off  
        $game_variables[2] = x          #set canoe x coord
        $game_variables[3] = y          #set canoe y coord
        @through = true
        move_forward
        @through = false          
        
      elsif $game_switches[2] == true   #getting off boat
        boat = $game_map.events[2]      #get boat event
        boat.moveto(x, y)               #move to player's x, y coords        
        $game_switches[2] = false       #Boat Off    
        $game_variables[4] = x          #set boat x coord
        $game_variables[5] = y          #set boate y coord  
        @through = true
        move_forward
        @through = false                  
        
      elsif $game_switches[3] == true   #getting off dragon
        dragon = $game_map.events[3]    #get dragon event
        dragon.moveto(x, y)             #move to player's x, y coords        
        $game_switches[3] = false       #Dragon Off    
        $game_variables[6] = x          #set dragon x coord
        $game_variables[7] = y          #set dragon y coord        
      end
      
      # get off vehicle
      actor_restore
    
      # reset terrain
      $game_variables[1] = 0          
    
      # play walking music  ## Night5h4d3 edit ##
      if $original_bgm != nil
        $original_bgm.play
      else
        Audio.bgm_play("Audio/BGM/044-Positive02", 100, 100) #play music
      end

      $game_player.refresh  
      $game_map.refresh    
    
    end
  
  end
  
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    in_vehicle_alias
    # if not on ground terrain, check for boat exit
    if $game_variables[1] != 0
      if Input.trigger?(Input::C)
        exit_vehicle
      end  
    end
  end

end
CJC
After glancing at that I figure it will probably work, but there's another problem I forgot to mention. (sorry!)

I think whoever originally made this script assumes that a vehicle will always be on the same map (like a world map thing you'd find in Final Fantasy). I say this because of the music thing, but also because each vehicle corresponds to a certain Event ID. But if you want to use the same vehicle on different maps it won't work unless you make sure it's whatever ID you chose in the script. Another way around it is making a separate vehicle in the script for each map you want to use a vehicle on. Both of these are pretty inconvenient. So what I'm asking is if there's a way to make it so... the script memorizes what event the vehicle is supposed to be, I guess? I dunno. If that's not possible, someone could link me to a script where you can use a vehicle on more than one map. But yeah, any help would be appreciated.
Legacy
There is a script that allows you to move events from a different map to another, i cannot remeber where i've seen this. But i will do some searching for you and post it here if i find it.
CJC
Thanks, man. It's much appreciated.
Gatos
I was wondering if its possible to use this script in such a way that i can call it through as an item or with a move. Essentially, I'm try to mimic the use like in pokemon, but i don't wanna use the Pokemon Essential project . Is there any way anyone can help?
jomarcenter
i could use your world map so i can create game easily because after i release the beta version of future helper xd all of the beta tester wanted the world map
as of thank you became one of the beta tester i will e-mail you soon with some installer with full walkthrough
note:don't submit to my game as your
karldaylo
i have this error.. help please
Night_Runner
So you pasted the script in the right place?

In your game editor, select Tools >> Script Editor. Along the left, scroll all the way down to the bottom, right click on Main, and select Insert.
Paste the script in the blank window on the right.
And if you want you can name the script in the bottom left of the window.

Sorry, if it's in the right place it really should work.... Let me know how it goes either way!
karldaylo
QUOTE (Night_Runner @ May 6 2011, 11:35 PM) *
So you pasted the script in the right place?

In your game editor, select Tools >> Script Editor. Along the left, scroll all the way down to the bottom, right click on Main, and select Insert.
Paste the script in the blank window on the right.
And if you want you can name the script in the bottom left of the window.

Sorry, if it's in the right place it really should work.... Let me know how it goes either way!


sir, i know how to input new script, (that explains the error, since after all i imported this succesfuly on my game)
i placed it after scene_debug and befor main... thank you

EDITED: darn. jsut as i thought it wasnt compatible with my other script... anyways.. thanks for responding smile.gif
Haku
Can someone put the demo please?
I can not run the addon to change the map and storing the vehicle when you return รงรง
Thanks wink.gif
Haku
UP please ^^
brewmeister
@Haku, what are you asking? The Demo link in the first post works.

Are you asking for another demo with an additional vehicle added?
Haku
My problem is that every time you change the map and then we return, the vehicle is set at the starting point and not where it was left.
I thought I had understood that this thing had been settled, but I can not follow the instructions ็_็
So I asked an additional demo ^^
brewmeister
Ok, I think I understand. She left out a step, and I added a Note on step 3...

3. Do not change "Switch 0003: using dragon is ON" on page 2 of your event. This is to prevent entering the vehicle directly from the dragon.

11. Edit the database common events to add your vehicle.
Initialize: add your vehicles starting X & Y
World Map End: Set event location
Haku
QUOTE (brewmeister @ May 17 2011, 10:43 AM) *
Ok, I think I understand. She left out a step, and I added a Note on step 3...

3. Do not change "Switch 0003: using dragon is ON" on page 2 of your event. This is to prevent entering the vehicle directly from the dragon.

11. Edit the database common events to add your vehicle.
Initialize: add your vehicles starting X & Y
World Map End: Set event location

I do not think you understand very well the steps :S
I did as I have suggested, but to no avail :S

You can put a demo please?
brewmeister
QUOTE
I do not think you understand very well the steps :S


I must understand it pretty well. I got it to work!!! smile.gif

Here is a demo with a horse added, that can walk over mountains & forests (Terrain 1)

http://hbgames.org/user_files/Lambchop_Vehicle_Sys2.exe


To test:
Get in canoe, travel under bridge to lake
Get out of canoe
Get on Ship, travel to SW island near horse
Get on horse, cross mountains
Get off horse, go into the cave
Come out of the cave, everything is right where you left it
Haku
Thanks brewmeister!

wink.gif
Princessbinas
i thinkboth are broken because the boat goes ove land tress and mountains! if possible can someone fix this.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.