LoginLogin

making and loading a map onto the screen using data arrays

Root / Programming Questions / [.]

randoCreated:
before you start screaming at me, ive looked all over sbs and google and even gamefaqs and found nothing helpful to me. i just want to understand how to use an array variable to represent a bgtile/sprite so i can make a map using data. i also want to know how to load that onto the screen, ive also been trying to figure that out. please help!

First, you create your bg texture (Can't remember how to do that, something about scsave?) and load the correct bg layer data file into a blank array (VAR A[0]). Then you BGLOAD the array. I think that's all there is to displaying a bg with BGLOAD, but their might be a little more to it. Another way to make a background with sprites is to SPSET ARR[anynumber] and the proper arguements for that function but that would take sprite "slots". If you type "bg" and press the ? button you can get some useful information on those functions. You can also look at the game manual available at the home screen of the 3ds.

I wouldn't use an array to represent the map. The BG is already an array, and BGCOORD converts all the coordinates (screen, bg pixels, bg tiles) to all the other coordinates. Instead, I would create the BG in a program like the Smile Tool, save all the layers, then load them with BGLOAD. Then when you need to access the map at a particular location, use a combination of BGCOORD and BGGET to figure out the map data at a point. If you're interested, I can come up with some code. The Smile Tool usually saves it as some weird binary format, but you can make it save as regular loadable layers using the file menu in the map editor.

Right forgot the BGGET part. What's BGCOORD for?

You can load data from an array into a BG layer using BGLOAD. BGLOAD Layer,X,Y,Width,Height,Array To get the data into the array, the easiest way is to use DATA and COPY:
VAR DATAWIDTH=4,DATAHEIGHT=3
DIM ARRAY[DATAHEIGHT,DATAWIDTH] 'create array

COPY ARRAY,@TILES 'copy DATA after @TILES into array
BGLOAD 0,2,2,DATAWIDTH,DATAHEIGHT,ARRAY 'copy array onto BG layer

@TILES
DATA 2,1,1,2
DATA 1,0,0,1
DATA 2,1,0,1
Notice that ARRAY is backwards. if your data is 10 tiles wide and 4 tiles height, you make an array with dimensions [4,10]. And to access a tile in the array, you'd do ARRAY[Y,X]. This is just how 2D arrays work in SB.

You can load data from an array into a BG layer using BGLOAD. BGLOAD Layer,X,Y,Width,Height,Array To get the data into the array, the easiest way is to use DATA and COPY:
VAR DATAWIDTH=4,DATAHEIGHT=3
DIM ARRAY[DATAHEIGHT,DATAWIDTH] 'create array

COPY ARRAY,@TILES 'copy DATA after @TILES into array
BGLOAD 0,2,2,DATAWIDTH,DATAHEIGHT,ARRAY 'copy array onto BG layer

@TILES
DATA 2,1,1,2
DATA 1,0,0,1
DATA 2,1,0,1
Notice that ARRAY is backwards. if your data is 10 tiles wide and 4 tiles height, you make an array with dimensions [4,10]. And to access a tile in the array, you'd do ARRAY[Y,X]. This is just how 2D arrays work in SB.
thanks, are the numbers in the
DATA
just defining the bg tile number, or is it the map itself? if its the definition number, then it helps ALOT, otherwise, i dont really understand how to define. but thanks!

It's the bg tile number (Which you can check using the smile tool) You can also add #BGROT90, #BGROT180, or #BGROT270 to the tile number to change the rotation of the tile, and #BGREVV or #BGREVH to mirror it.

You may want to have a look at the code for Find the Exit http://smilebasicsource.com/page?pid=820, it defines levels with data statements, which sounds about like what you are looking for. I do have code to load a binary level file somewhere if you need that instead (let me know). Unfortunately the level editor in smile tool is limited to 64x64 tile worlds only which is a shame. Anyway, you want to do things in roughly this order.
  • Clear out any old junk, and set the screen mode (ACLS, XSCREEN)
  • Load custom graphics if you have them (sprites/bg tiles)
  • Resize the background for each layer you use using (BGSCREEN)
  • Read in data (READ/DATA or LOAD) and write out bg tiles using BGPUT x, y, tile_number for each tile x, y coordinate on each layer you are using (tiles are 16x16 by default, but the tile map is measured in tiles not pixels)
  • If the layers are larger than the screen position them using BGOFS.
  • Use BGGET to figure out which tile is what on screen so you can do physics for the game and interact with the world. (Unless it is just a do nothing background)
Hope that helps!