RGSS For Newbies (Ongoing Series) (2024)

RGSS: MAP, TILESETS and TILEMAP

HOW TO LOAD A MAP?

It's very easy to load a map 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:

Code:

load_data("Data/Map001.rvdata2")

We can 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:

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 a map via scripting. P.S. The load_data() function can be used for all rvdata files, as demonstrated in the next section.

The Map data structure comes with so many properties such as width, height, tilemap data, linked tilesets, etc. In a future tutorial, we'll learn how to create a procedural map from scratch by code.

TILESETS

RGSS has a data structure called Tileset. I find the term a bit misleading because in truth the structure holds more than only one tileset. There are in fact 9 slots referring to a tileset type as follow: A1, A2, A3, A4, A5, B, C, D, E. The RPG Maker team should have called this class something like Tilesets or TilesetPack to strengthen the idea it's a group of tilesets, not just one.

When you organize tilesets within the RPG Maker's tileset editor, each 'pack' of tilesets are stored in an array, and this array is then saved as "Tilesets.Rvdata2" within the Data folder.

What's more, each map in RPG Maker is associated to one of those packs of tilesets stored in "Tilesets.Rvdata2". A map has in fact a property called tileset_id which refer to an index of that array.

TILESET DIAPORAMA DEMO

Here's a quick demo that allows to display each image from a pack of tilesets associated to a map on the screen. You can cycle throughout the list of tilesets associated with a map using the Left and Right arrows.

Code:

map_id = 1@map = load_data(sprintf("Data/Map%03d.rvdata2", map_id))@tilesets_rvdata = load_data("Data/Tilesets.rvdata2")@ts_pack = @tilesets_rvdata[@map.tileset_id]@ts_images = []@ts_pack.tileset_names.each do |str| @ts_images.push( Bitmap.new("Graphics/Tilesets/" + str) ) if str != ""end@show_tileset = Sprite.newi = 0loop do Graphics.update Input.update if Input.trigger?(:LEFT) i -= 1 if i > 0 elsif Input.trigger?(:RIGHT) i += 1 if i < @ts_images.length-1 end @show_tileset.bitmap = @ts_images[i] end

RGSS For Newbies (Ongoing Series) (1)

First, we load our packs of tilesets in a variable that serves as an array: @tilesets_rvdata = load_data("Data/Tilesets.rvdata2"). Then, we find the tileset pack we need with our map's tileset id property: @tileset = @tilesets_rvdata[@map.tileset_id]. The Tileset data structure doesn't hold an array of images per se, it holds only the filenames in an array. So we create an array of bitmaps in which we use those filenames as the main parameter: @ts_pack.tileset_names.each do |str| @ts_images.push( Bitmap.new("Graphics/Tilesets/" + str) ) if str != "" end.

In summary, remember that a map hold a tileset_id that refers to the index of the 'tilesets.rvdata2' array. Each pack of tilesets has an array of 'filenames' that refer to image files in the folder. You use those filename to create bitmap objects in the code. There's more related to the Tileset data structure, such as tile collision, but it will be for another future lesson.

TILEMAP

RPG Maker has a built-in feature called Tilemap. This feature magically takes care of the rendering process of tiles assigned to it. Coding your own rendering engine would take a significant amount of efforts. It's something I plan to teach, if there's enough requests in the future. For the time being, the Tilemap class provided by the RPG Maker team does the trick with very little intervention from the coder—even if it's somewhat limited for complex mapping layouts. Here's the simplest way to init the Tilemap class:

Code:

@map = load_data("Data/Map001.rvdata2")@tilemap = Tilemap.new@tilemap.map_data = @map.data

In the following demo, the map's tileset image will switch at x interval of time, while preserving the tilemap data.

Code:

### SETTINGSscreen_width = 640screen_height = 480Graphics.resize_screen(screen_width, screen_height)### LOAD EXISTING MAPmap_id = 1@map = load_data(sprintf("Data/Map%03d.rvdata2", map_id))### CREATE TILEMAP@viewport = Viewport.new(0,0,screen_width, screen_height)@tilemap = Tilemap.new(@viewport)@tilemap.map_data = @map.data### LOAD EXISTING TILESETSpath = "Graphics/Tilesets/"tileset_arr = []tileset_arr[0] = "Outside_A1"tileset_arr[1] = "Inside_A1"tileset_arr[2] = "World_A1"### MAIN LOOPi = 0loop do @tilemap.bitmaps[0] = Bitmap.new(path + tileset_arr[i]) i += 1 i = 0 if i > 2 Graphics.update Graphics.wait(30)end

RGSS For Newbies (Ongoing Series) (2)

So, that's the basic of it. Of course, there's more to learn, but that will be for another lesson. Stay tuned.

SOURCE: http://www.gamezopher.com/2018/07/03/this-is-a-test-post/

RGSS For Newbies (Ongoing Series) (2024)
Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5661

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.