Map-related Classes

RPG Maker (RGSS)

Map-related Classes

The basics of creating maps by code.

LOAD AN EXISTING MAP

It’s very easy to load an existing map by code in the RPG Maker script editor. We simply need to use the load_data() function. This function only needs the path+filename as a parameter. Example:

@map = load_data("Data/Map001.rvdata2")

P.S. The load_data() function can be used for all rvdata files, as we’ll see in other lessons.

Speaking of which, we can also use the sprintf() function with re-usability in mind (which borrows from the C language), but such explanation would require a longer in-depth tutorial. Anyway, here’s an example of the usage:

map_id = 1
@map = load_data( sprintf("Data/Map%03d.rvdata2", map_id) )

What’s interesting about the above snippet is that this function allows to use a special flag in the string format that acts like a dynamic variable. The %3d sequence allows to pad a digit with three leading zeros. More lines of code would be required if such feature had to be written by hand. So, it’s wise to remember the sprintf() function for this kind of situation.

As you can see, it’s very easy to load an existing map via the scripting approach.

CREATE A MAP PROCEDURALLY

Creating a map by code rather than from the editor is equally easy. Here’s a very simple code snippet.

@map = RPG::Map.new(20, 15)
@map.display_name = "Procedural Map"

We’ve created a map from scratch with the following line: @map = RPG::Map.new(20, 15). The passed init parameters are the width and height of the map in tile units. It’s very important not to mislead pixel units with tile units.

The Map class data structure is like follow, which you can find in the RPG Maker official documentation:

class RPG::Map
def initialize(width, height)
@display_name = ''
@tileset_id = 1
@width = width
@height = height
@scroll_type = 0
@specify_battleback = false
@battleback_floor_name = ''
@battleback_wall_name = ''
@autoplay_bgm = false
@bgm = RPG::BGM.new
@autoplay_bgs = false
@bgs = RPG::BGS.new('', 80)
@disable_dashing = false
@encounter_list = []
@encounter_step = 30
@parallax_name = ''
@parallax_loop_x = false
@parallax_loop_y = false
@parallax_sx = 0
@parallax_sy = 0
@parallax_show = false
@note = ''
@data = Table.new(width, height, 4)
@events = {}
end
end

You may have noticed that it’s not possible to access the real name of a map object (aka filename) via any of the properties above; only display_name is available to us. It’s because extra information related to map is shared with another class called MapInfo. So if you want to summon a map by its filename, it requires a few extra steps.

MAP INFO CLASS

Map Info is used to displays the collection of maps in the editor’s hierarchical tree view. This ‘tree view’ is stored within ‘MapInfos.rvdata2’ is an array; maps ids represents the indexes, and values are the Map info objects. Here is the data structure for MapInfo:

class RPG::MapInfo
def initialize
@name = ''
@parent_id = 0
@order = 0
@expanded = false
@scroll_x = 0
@scroll_y = 0
end
end

Let’s create a simple program to retrieve map names from the MapInfos.rvdata array.

@map_infos = load_data("Data/MapInfos.rvdata2")
map_id = 5
str1 = @map_infos[map_id].name
parent_id = @map_infos[map_id].parent_id
str2 = @map_infos[parent_id].name

@window = QWindow.new(0,Graphics.height/2,Graphics.width, 48)
@window.display_text( str1 + " is set inside " + str2 )

loop do
Graphics.update
end

This program load the MapInfos.rvdata file and store it in a variable: @map_infos = load_data(“Data/MapInfos.rvdata2”). In my project, I actually have these maps in the editor tree view.

Using map_id #5 as the index key in the MapInfos array, I store both the map name and its parent_id in variables. Thanks to the parent_id, I can also know in which other map the map is child of. Here’s the result:

And if we want to reorganize the tree view by code, we would write something like this:

@map_infos = load_data("Data/MapInfos.rvdata2")

@map_infos.each_value {|map_info|
map_info.parent_id = 0
map_info.name = map_info.name + " (edited)"
}

save_data(@map_infos, "Data/MapInfos.rvdata2")

Which would result into this the next time we re-open RPG Maker:

2 Comments

  1. Hello Erik. Actually, I was mostly considering Patreon for teaching purposes and my ongoing efforts related to Gamezopher. I guess it’s too soon at this point in time, though.

  2. Have you ever thought of developing your own class(es) on Udemy for making a good rpg? I am not sure if you use strictly the latest RPGMaker software for this or other programming software/languages like Ruby,Java, C++, Unity. Some suggestion to add would be building the story. Not the object of writing but how it’s done. Others would be mob/item/player stat and integration. Also can’t forget Blender concepts of creating a 3D like world. I know I had suggested this awhile back but at the time you were focused only on making EE:E and not what you’re doing now. Might be a good way to make some money on the side. I hope you might be more open to this now.

Leave a Reply

Your email address will not be published. Required fields are marked *