RPG Maker Forums

Im TRYING to set up this script to use a Parallaxed background 

################################################################################
# #
# ** Parallaxed Battlebacks by Riff ** #
# #
################################################################################

################################################################################
# #
# ----------- #
# — Description — #
# ----------- #
# #
# This script allows you to use pictures to build your battle backgrounds. In #
# other words it's a bit like parallax mapping but for battle backgrounds. #
# There are 5 layers in battle : #
# -> back layer #
# -> back details layer #
# -> battler layer (that's where the sprites #
# of both actors and enemies are being #
# displayed, you can't add anything on that #
# layer through this script.) #
# -> foreground layer #
# -> light/shadow/fog/anything-you-want layer #
# #
# You can animate any layers that you want and you can set the animation pace #
# (or freeze this animation) mid-battle. You can also change layers during #
# battles through a script call. #
# You can set in the first part of the script a Hash to use parallax instead #
# of the default biomes on the overwolrd without having to note-tags every #
# overworld map. #
# You can use parallax according to region for each map. #
# You can set a default parallax for each map, this determine the parallax #
# used for all battles on this map except those that you specifically set #
# through regions. #
# You can set a switch to turn ON/OFF the use of parallax. #
# #
################################################################################

################################################################################
# #
# ------------ #
# — Terms Of Use — #
# ------------ #
# #
# . You are free to use this script in both commercial and non-commercial #
# games in whatever way that fits your needs. #
# . You are free to edit this script in whatever way fits your needs. #
# . No compatibility issue will be fixed. #
# . For bug fixes or support, post at www.nerdynest.wordpress.com or at #
# www.rpgmakervxace.net (questions explicitly answered in this header will #
# be ignored). #
# . For additionnal features requests, post at www.nerdynest.wordpress.com #
# or at www.rpgmakervxace.net. However, note that you are not guaranteed #
# to get it done(depending on the amount of work needed). #
# . Full credits must be given to Riff. #
# . You can repost this anywhere providing that credits are given #
# accordingly. #
# #
################################################################################

################################################################################
# #
# --------- #
# — Changelog — #
# --------- #
# #
# . 09/26/2013 — Initial release #
# #
################################################################################

################################################################################
# #
# ------------------------------- #
# — Concerning Compatibility issues — #
# ------------------------------- #
# #
# This script : #
# + Modifies eight classes #
# / RPG::Map #
# / Game_Map #
# / Spriteset_Battle #
# / Scene_Battle #
# / Game_Interpreter #
# + Adds one new class #
# + Overwrites no method #
# + Alias six methods #
# / setup (Game_Map) #
# / initialize (Spriteset_Battle) #
# / create_battleback1 (Spriteset_Battle) #
# / create_battleback2 (Spriteset_Battle) #
# / dispose (Spriteset_Battle) #
# / update (Spriteset_Battle) #
# + Uses too much new methods (29) #
# #
################################################################################

################################################################################
# #
# ------------ #
# — Instructions — #
# ------------ #
# #
# — Parallax Set — #
# A parallax set is a set of layers (back, back_details, foreground & light)#
# that are used together. For exemple, I can have a parallaxe set named #
# 'Test' that will be composed four pictures/layers (for now) that, by #
# default, will be displayed together (you can tweak things later). #
# Concretely, a parallax set is a folder that contains four pictures. If a #
# parallax set contains less than four pictures it is said to be #
# 'incomplete' (but that doesn't necessarily mean it won't work, keep #
# reading). #
# The first thing to do is to create a new folder in your Graphics directory#
# and name it 'BattleBack_Overlays', this folder will contains all your #
# parallax set plus another picture named 'Blank' (provided with the #
# script). That 'Blank' picture is mandatory, if you don't have it or #
# don't put it in the 'BattleBack_Overlays' folder you will get a big #
# juicy error. #
# Whenever you want to create a new parallax set just create a new folder #
# and name it as the name of your parallax set. Then put in that folder #
# the layers you are willing to use. Name those pictures according to #
# the layer they are representing so : #
# -> back #
# -> back_details #
# -> foreground #
# -> light #
# If you're using an incomplete parallax set there are two scenarii : #
# -> the back layer is missing. #
# Then you will not be able to use that parallax set as a #
# default one but you will be able to use that folder with the #
# change_graphic script call. (see at the Script Calls Section)#
# -> the back layer is here, other layers are missing. #
# You can use that parallax set as a default one or through #
# the change_graphic script call (see the Script Calls Section)#
# and the missing layers will be skipped won't be displayed. #
# #
# — Customisation — #
# There are a bunch of things you can customise in the first module of the #
# script : #
# -> a game switch that will turn ON/OFF the use of parallax #
# battle backgrounds. Note that you have to set the switch to #
# ON if you want to use parallaxed backgrounds. Switching the #
# switch during a battle won't do anything. #
# -> the default pace at which your animated parallax will be #
# updated. #
# -> a hash array (defined between {}) where you can set default #
# parallaxes to use instead of the default biomes you can #
# encounter on your overwolrd maps (like snow, swamp, lava...) #
# #
# — Animation — #
# You can animate your battle background or parts of it (specific layers) #
# with this script. To do so you simply have to add X after the name of #
# the layer, X being the frame of the animation. For exemple, you can use #
# back1, back2, .... There's no limit to the number of frames you can use #
# If you want to animate a layer you have to begin with 1, if there is no #
# first frame then the script will simply skip that layer and won't #
# display it at all. #
# You can have only one layer animated if you want. (Or any other number) #
# The animation will proceed this way : 1-2-3-...-(n-1)-n-(n-1)-...-3-2-1 #
# It's basically the same way battlers sprites are animated. #
# The default pace for animation can be set in the first module. #
# #
# — Note-tags — #
# To use parallaxed battle backgrounds note-tag your maps with the #
# following : #
# <parallax battle: parallax_set region_id> #
# #
# Where parallax_set is the name of the folder you are willing to use and #
# region_id the region in which you want to use that parallax. #
# Region_id is an optional argument. If nothing is parsed here then the #
# parallaxed_set you parsed will be used as the default one for the map. #
# If you note-tag multiple default parallax set then only the latest will #
# actually be used. #
# #
# — Script Calls — #
# You can use two script calls in troop events only : (anywhere else nothing#
# will happen) #
# -> battle_reset_timer(timer, layer) will set the pace at #
# which the layer will be animated. If no layer is parsed #
# then it will affect all layers. #
# You can freeze the animation if you put a negative timer. #
# You can unfreeze the animation with a new script call. #
# If this script call is used outside of battle or during an #
# unparallaxed battle nothing will happen. #
# -> battle_change_graphic(folder, layer) will change the #
# graphic of set layer to the graphic of the same layer in #
# set folder. #
# Layer is an optional argument and if nothing is put then #
# all layers will be changed using set folder (if the #
# parallax set you're trying to use is incomplete then the #
# script will keep the old graphics for those layers. #
# If the layer or the folder are incorrect then nothing will #
# happen. Same goes if you're trying to use that script call #
# outside of battle or during an unparallaxed battle. #
# #
################################################################################

($imported ||= {})['RIFF_BATTLEPARALLAX'] = true

#===============================================================================
# ** RIFF::BATTLE_PARALLAX
#-------------------------------------------------------------------------------
# Module that stores the Regexp and variables for customizing.
#===============================================================================
module RIFF
module BATTLE_PARALLAX
#---------------------------------------------------------------------------
# Set here the ID of the switch you want to use.
#---------------------------------------------------------------------------
USE_PARALLAX = 1

#---------------------------------------------------------------------------
# Set here the default timer for your sprites.
#---------------------------------------------------------------------------
DEFAULT_TIMER = 60

#---------------------------------------------------------------------------
# Set here the parallax set you want to use for the default biomes.
# Default biomes are : Wasteland, DirtField, Desert, Lava1, Lava2,
# SnowField, Clouds, PoisonSwamp, Forest, Cliff, Boat,
# Ship, Airship & Grassland.
# Use the exemple to fill the hash and don't forget to add comas between
# each lines.
#---------------------------------------------------------------------------
DEFAULTS = {'Biome Name' => 'Parallax Set' ,
'Biome_Name2' => 'Parallax_Set2'}

################################################################################
################################################################################
### DO NOT EDIT ANYTHING PAST THIS POINT ###
################################################################################
################################################################################

REGEXP = /<\s*battle[-_ ]parallax\s*:\s+(\w+)\s*(\d*)\s*>/
end
end

#===============================================================================
# ** RPG::Map
#-------------------------------------------------------------------------------
# Modified to process the note-tagging.
#===============================================================================
module RPG
class Map
def load_battle_parallax_notetags
matchings = self.note.scan(RIFF::BATTLE_PARALLAX::REGEXP)
matchings.each{ |block|
if block[1] == ""
(@notes ||= {})['Default'] = block[0]
else
(@notes ||= {})[block[1].to_i] = block[0]
end }
return @notes
end
end
end

#===============================================================================
# ** Cache
#-------------------------------------------------------------------------------
# Modified to add a new bitmap category that can be loaded.
#===============================================================================
module Cache
def self.battleback_bitmap(battleback_name)
begin
return Cache.normal_bitmap('Graphics/BattleBack_Overlays/' + battleback_name)
rescue
return nil
end
end
end

#===============================================================================
# ** Game_Map
#-------------------------------------------------------------------------------
# Setup modified to include the battle_backs (and save workload farther in the
# script).
#===============================================================================
class Game_Map
attr_reader :battle_backs
alias :riff_battle_parallax_setup :setup

def setup(map_id)
riff_battle_parallax_setup(map_id)
@battle_backs = @map.load_battle_parallax_notetags
end
end

#===============================================================================
# ** Sprite_BattleBack
#-------------------------------------------------------------------------------
# New class that will handle all the sprite in the Spriteset_Battle class.
#===============================================================================
class Sprite_BattleBack < Sprite
attr_accessor :folder_name
attr_accessor :layer
attr_accessor :count
attr_accessor :index
attr_accessor :reset_timer

#-----------------------------------------------------------------------------
# Initialize method that stores various properties of the sprite.
#-----------------------------------------------------------------------------
def initialize(viewport, folder_name, layer, count)
super(viewport)
@folder_name = folder_name
@layer = layer
@count = count
@index = 1
@reset_timer = RIFF::BATTLE_PARALLAX::DEFAULT_TIMER
@timer = @reset_timer
update
end

#-----------------------------------------------------------------------------
# Update method that checks if part of the graphics infos are changed or if
# the sprite is animated.
#-----------------------------------------------------------------------------
def update
update_animation if (@count != nil && @timer == 0)
update_bitmap if graphic_changed?
@timer = [0, @timer - 1].max if @count != nil && @reset_timer > 0
center_sprite(self)
end

#-----------------------------------------------------------------------------
# Change the infos control points and call for the bitmap creation method.
#-----------------------------------------------------------------------------
def update_bitmap
@current_folder = @folder_name
@current_layer = @layer
self.bitmap = create_bitmap
end

#-----------------------------------------------------------------------------
# Method that check if graphics are changed using control points @current_layer
# and @current_folder.
#-----------------------------------------------------------------------------
def graphic_changed?
return @current_folder.nil? || @current_folder != @folder_name || @current_layer != @layer
end

#-----------------------------------------------------------------------------
# Method that create the sprite's bitmap according to current graphic infos.
#-----------------------------------------------------------------------------
def create_bitmap
if @folder_name.nil? || @layer.nil?
return Cache.empty_bitmap
else
return Cache.battleback_bitmap(@folder_name + @layer)
end
end

#-----------------------------------------------------------------------------
# Method that animate the sprite by checking if the way the sprite is
# animated and the existence of next (or previous) frame (and change the
# animation accordingly).
#-----------------------------------------------------------------------------
def update_animation
@timer = @reset_timer
if @count == :up
if next_exist?
@layer = next_frame
@index += 1
else
@layer = previous_frame
@count = :down
@index -= 1
end
elsif @count == :down
if previous_exist?
@layer = previous_frame
@index -= 1
else
@layer = next_frame
@index += 1
@count = :up
end
end
end

#-----------------------------------------------------------------------------
# Determine if next frame exists (basically if image @layer(n+1) exists).
#-----------------------------------------------------------------------------
def next_exist?
bitmap_test = Cache.battleback_bitmap(@folder_name + next_frame)
!bitmap_test.nil?
end

#-----------------------------------------------------------------------------
# Determine if previous frame exists (basically if image @layer(n-1) exists).
#-----------------------------------------------------------------------------
def previous_exist?
bitmap_test = Cache.battleback_bitmap(@folder_name + previous_frame)
!bitmap_test.nil?
end

#-----------------------------------------------------------------------------
# Returns @layer(n+1).
#-----------------------------------------------------------------------------
def next_frame
@layer.gsub(/\d+/) { |digit| digit = (@index + 1).to_s}
end

#-----------------------------------------------------------------------------
# Returns @layer(n-1).
#-----------------------------------------------------------------------------
def previous_frame
@layer.gsub(/\d+/) { |digit| digit = (@index - 1).to_s}
end

#-----------------------------------------------------------------------------
# Same method used in Spriteset_Battle.
#-----------------------------------------------------------------------------
def center_sprite(sprite)
sprite.ox = sprite.bitmap.width / 2
sprite.oy = sprite.bitmap.height / 2
sprite.x = Graphics.width / 2
sprite.y = Graphics.height / 2
end

#-----------------------------------------------------------------------------
# Method that change @folder_name and sometimes @layer variable.
# That method also proceed with the swapping between animated and static
# parallax.
#-----------------------------------------------------------------------------
def change_graphic(folder)
if folder.nil?
@folder_name = nil
@count = nil
else
layer = @layer.gsub(/\d+/) {|digit| digit = ""}
path = folder + layer
test_layer = Cache.battleback_bitmap(path)
if test_layer
@layer = layer
@folder_name = folder
@count = nil
else
path += '1'
test_layer = Cache.battleback_bitmap(path)
if test_layer
@layer = "#{layer}1"
@index = 1
@folder_name = folder
@count = :up
else
return
end
end
end
end
end

#===============================================================================
# ** Spriteset_Battle
#-------------------------------------------------------------------------------
# Modified the core functioning so that the game decide which battleback to use
# the parallaxed one or the default one.
#===============================================================================
class Spriteset_Battle
alias :riff_battle_parallax_initialize :initialize
alias :riff_battle_parallax_create_battleback1 :create_battleback1
alias :riff_battle_parallax_create_battleback2 :create_battleback2
alias :riff_battle_parallax_dispose :dispose
alias :riff_battle_parallax_update :update

#-----------------------------------------------------------------------------
# Initialize method that add the battle_back that will be used.
#-----------------------------------------------------------------------------
def initialize
@battleback_name = get_battleback_name
riff_battle_parallax_initialize
end

#-----------------------------------------------------------------------------
# Method that get the battleback used for the fight. Returns nil if the
# battleback used is not parallaxed.
#-----------------------------------------------------------------------------
def get_battleback_name
if $game_switches[RIFF::BATTLE_PARALLAX::USE_PARALLAX]
@battle_backs = $game_map.battle_backs
if $game_map.overworld?
return RIFF::BATTLE_PARALLAX::DEFAULTS[overworld_battleback_name] if RIFF::BATTLE_PARALLAX::DEFAULTS[overworld_battleback_name]
return @battle_backs[$game_player.region_id] if @battle_backs[$game_player.region_id]
return @battle_backs['Default'] if @battle_backs['Default']
else
return @battle_backs[$game_player.region_id] if @battle_backs[$game_player.region_id]
return @battle_backs['Default'] if @battle_backs['Default']
end
end
return nil
end

#-----------------------------------------------------------------------------
# Method that returns the type of terrain.
#-----------------------------------------------------------------------------
def overworld_battleback_name
if $game_player.in_boat?
'Boat'
elsif $game_player.in_ship?
'Ship'
elsif $game_player.in_airship?
'Airship'
else
terrain_battleback_name(autotile_type(1)) || terrain_battleback_name(autotile_type(0)) || default_battleback1_name
end
end

#-----------------------------------------------------------------------------
# Method that returns the type of terrain depending on tile flag.
#-----------------------------------------------------------------------------
def terrain_battleback_name(type)
case type
when 24,25 # Wasteland
"Wasteland"
when 26,27 # Dirt field
"DirtField"
when 32,33 # Desert
"Desert"
when 34 # Rocks
"Lava1"
when 35 # Rocks (lava)
"Lava2"
when 40,41 # Snowfield
"SnowField"
when 42 # Clouds
"Clouds"
when 4,5 # Poisonous swamp
"PoisonSwamp"
when 20,21 # Forest
"Forest"
when 22,30,38 # Low hill
"Cliff"
end
end

#-----------------------------------------------------------------------------
# Modified to use either parallaxed or default battleback. Create the back
# sprite.
#-----------------------------------------------------------------------------
def create_battleback1
parallax_datas = folder_exist?(@battleback_name, '/back') if @battleback_name
if $game_switches[RIFF::BATTLE_PARALLAX::USE_PARALLAX] && parallax_datas
@back1_sprite = Sprite_BattleBack.new(@viewport1, @battleback_name, parallax_datas[0], parallax_datas[1])
@back1_sprite.z = 0
@parallaxed = true
else
riff_battle_parallax_create_battleback1
end
end

#-----------------------------------------------------------------------------
# Modified to use parallaxed battleback if back sprite exist, default one if
# not. Create the back details sprite, the foreground sprite and the light.
#-----------------------------------------------------------------------------
def create_battleback2
parallax_datas = folder_exist?(@battleback_name, '/back_details') if @battleback_name
if $game_switches[RIFF::BATTLE_PARALLAX::USE_PARALLAX] && @parallaxed
if parallax_datas
@back2_sprite = Sprite_BattleBack.new(@viewport1, @battleback_name, parallax_datas[0], parallax_datas[1])
@back2_sprite.z = 1
else
@back2_sprite = Sprite_BattleBack.new(@viewport1, nil, nil, nil)
end
create_foreground_battleback
create_light_battleback
else
riff_battle_parallax_create_battleback2
end
end

#-----------------------------------------------------------------------------
# Create the foreground sprite. Set a blank sprite if not parallaxed.
#-----------------------------------------------------------------------------
def create_foreground_battleback
parallax_datas = folder_exist?(@battleback_name, '/foreground') if @battleback_name
if parallax_datas
@foreground_sprite = Sprite_BattleBack.new(@viewport1, @battleback_name, parallax_datas[0], parallax_datas[1])
@foreground_sprite.z = 800
else
@foreground_sprite = Sprite_BattleBack.new(@viewport1, nil, nil, nil)
end
end

#-----------------------------------------------------------------------------
# Create the light sprite; Set a blank sprite if not parallaxed.
#-----------------------------------------------------------------------------
def create_light_battleback
parallax_datas = folder_exist?(@battleback_name, '/light') if @battleback_name
if parallax_datas
@light_sprite = Sprite_BattleBack.new(@viewport1, @battleback_name, parallax_datas[0], parallax_datas[1])
@light_sprite.z = 900
else
@light_sprite = Sprite_BattleBack.new(@viewport1, nil, nil, nil)
end
@light_sprite.opacity = 64
end

#-----------------------------------------------------------------------------
# Method that check wether the set layer exists or not.
#-----------------------------------------------------------------------------
def folder_exist?(folder_name, layer_prefix)
path = folder_name + layer_prefix
bitmap_test = Cache.battleback_bitmap(folder_name + layer_prefix)
if bitmap_test.nil?
bitmap_test = Cache.battleback_bitmap(folder_name + layer_prefix + '1')
if bitmap_test
layer = "/#{layer_prefix}1"
count = :up
end
else
layer = "/#{layer_prefix}"
count = nil
end
return [layer, count] if bitmap_test
return nil
end

#-----------------------------------------------------------------------------
# Modify the dispose method the take care of the foreground and light sprites.
#-----------------------------------------------------------------------------
def dispose
riff_battle_parallax_dispose
dispose_foreground_battleback
dispose_light_battleback
end

#-----------------------------------------------------------------------------
# Method that dispose of the foreground sprite.
#-----------------------------------------------------------------------------
def dispose_foreground_battleback
@foreground_sprite.bitmap.dispose if @foreground_sprite
@foreground_sprite.dispose if @foreground_sprite
end

def dispose_light_battleback
@light_sprite.bitmap.dispose if @light_sprite
@light_sprite.dispose if @light_sprite
end

#-----------------------------------------------------------------------------
# Modified so that all layers are updated.
#-----------------------------------------------------------------------------
def update
riff_battle_parallax_update
update_remaining_layers
end

#-----------------------------------------------------------------------------
# Method that updates foreground and light sprite.
#-----------------------------------------------------------------------------
def update_remaining_layers
[@foreground_sprite, @light_sprite].each{ |sprite|
sprite.update if sprite}
end

#-----------------------------------------------------------------------------
# Result of a script call.
# This method set the timer of a layer to a certain value.
#-----------------------------------------------------------------------------
def reset_timer(timer, layer)
if @parallaxed
if layer.nil?
[@back1_sprite, @back2_sprite, @foreground_sprite, @light_sprite].each{ |sprite|
sprite.reset_timer = timer if sprite}
else
case layer
when 'back'
@back1_sprite.reset_timer = timer if @back1_sprite
when 'back_details'
@back2_sprite.reset_timer = timer if @back2_sprite
when 'foreground'
@foreground_sprite.reset_timer = timer if @foreground_sprite
when 'light'
@light_sprite.reset_timer = timer if @light_sprite
end
end
end
end

#-----------------------------------------------------------------------------
# Result of a script call.
# This method proceed with the change of graphic (all layers or a specific
# one if layer argument is not nil).
#-----------------------------------------------------------------------------
def change_graphic(folder_name, layer)
if @parallaxed
if layer.nil?
[@back1_sprite, @back2_sprite, @foreground_sprite, @light_sprite].each{ |sprite|
sprite.change_graphic(folder_name) if sprite}
else
case layer
when 'back'
@back1_sprite.change_graphic(folder_name) if @back1_sprite
when 'back_details'
@back2_sprite.change_graphic(folder_name) if @back2_sprite
when 'foreground'
@foreground_sprite.change_graphic(folder_name) if @foreground_sprite
when 'light'
@light_sprite.change_graphic(folder_name) if @light_sprite
end
end
end
end
end

#===============================================================================
# ** Game_Interpreter
#-------------------------------------------------------------------------------
# Adding the various methods needed for script calls.
#===============================================================================
class Game_Interpreter
def battle_reset_timer(reset_timer, layer = nil)
SceneManager.scene.reset_timer(layer, reset_timer) if SceneManager.scene_is?(Scene_Battle)
end

def battle_change_graphic(folder_name, layer = nil)
SceneManager.scene.change_graphic(folder_name, layer) if SceneManager.scene_is?(Scene_Battle)
end
end

#===============================================================================
# ** Scene_Battle
#-------------------------------------------------------------------------------
# Methods called in Game_Interpreter (because you can't grab the spriteset
# anywhere else than here).
#===============================================================================
class Scene_Battle < Scene_Base
def reset_timer(timer, layer)
@spriteset.reset_timer(timer, layer)
end

def change_graphic(folder_name, layer)
@spriteset.change_graphic(folder_name, layer)
end
end
I keep getting the following error when it starts the battle 

error 487 return nil.. 

in the script description it says the following above line 487

"Method that get the battleback used for the fight. Returns nil if the
# battleback used is not parallaxed" .

im not sure what it means that the file is not parallaxed.... please help

Latest Threads

Latest Posts

Latest Profile Posts

Don't forget, aspiring writers: Personality isn't what your characters do, it is WHY they do it.
Hello! I would like to know if there are any pluggings or any way to customize how battles look?
I was thinking that when you start the battle for it to appear the eyes of your characters and opponents sorta like Ace Attorney.
Sadly I don't know how that would be possible so I would be needing help! If you can help me in any way I would really apreciate it!
The biggest debate we need to complete on which is better, Waffles or Pancakes?
rux
How is it going? :D
Day 9 of giveaways! 8 prizes today :D

Forum statistics

Threads
106,047
Messages
1,018,540
Members
137,834
Latest member
EverNoir
Top