? Map and the DATA Instruction ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password

Map and the DATA Instruction

This guide assumes you have basic programming knowledge. You have been warned. So, everyone loves the DATA instruction, right? Some may find it useless, but probably not a hinderance. Well, here's yet another use for it: making map data. Variables: DIM MAP$[height]'Visible map data DIM MAD$[height]'Map collision data DIM BGTABLE$[amount]'BG tile table DIM I,J'FOR loop DIM CH$,SCDT'Data display Ok, first we need a BG table that holds all of the different tiles you want in your map. This way, you can have only a small amount of characters you can use to make your data. In other words, you won't need to use CHR$(17) or anything weird like that. So you can set these manually like this: BGTABLE[0]=17 'etc... Or you could set them with DATA where your map data will be: @MAP_000 DATA 0,17,36,59,'etc. So these are your map tiles you work with. To read them from data, just put after RESTORE a FOR loop: FOR I=LEN(BGTABLE-1) READ BGTABLE[I] NEXT This way it reads your BG table automatically. Now we need to cover the map data. We are using the array MAP$. This has the visible map stored in it. You set this with the DATA instruction: DATA "00000 DATA "10000 DATA "11000 DATA "11100 DATA "11110 Reading this can be done with another FOR loop: FOR I=0 TO LEN(MAP$)-1 READ MAP$[I] NEXT Now we've read that. The next thing we need to read is the collision data, which we will specify the same way as we did with MAP$ data. We will use the array MAD$. We literally do this the exact same as MAP$ except that there's no COLTABLE or anything. At least, I prefer not to do that. You could do that if you had a ton of collision types, but I usually don't so I just use the same characters for the same things anyway. Like 0 is no collision, 1 is collision on all sides, etc. Not too many combos. Now for the harder part: using this data. Displaying the map is hard to figure out, but is easily done with only a few lines. You need a nested FOR loop and the string function MID$. You also need to play with character codes. So you put your nested FOR loops, something like this: FOR I=0 TO LEN(MAP$)-1 FOR J=0 TO LEN(MAP$[I])-1 NEXT NEXT So the outer loop goes through the array, and the inner loop goes through the array's current element. Now you need some magic to single out a character and use it. So, we use the definetly-for sure magic function MID$. Just set a string to a section of the MAP$ line, and turn it into a map tile. CH$=MID$(MAP$[I],J,1) This gets the map tile. Now we need to convert this into something that BGPUT can read: SCDT=BGTABLE[ASC(CH$)-ASC("0")] We basically just reduced the subscript so that it is now in range of the BGTABLE and turned that tile into our screen data. Now we just need to BGPUT that on screen: BGPUT layer,J,I,SCDT As for collision, just do the same thing in the same nested loops, just on a different layer. The only purpose of doing this extra collision layer is so that you can test for a specific function, like if a block is solid or if it's a door; not have the visible map stuff get in the way or anything. Hope this helps anyone wondering about DATA maps!
Author
rando
Updated
Rating
0 votes
Categories
Keywords
  • Map
  • Data
2 Comment(s) hakke hakke RNG Hidden Easter Eggs Third Year My account is over 3 years old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength where can i buy SBS premium membership™ i need it rando rando Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Second Year My account is over 2 years old Website Forum Leader Hidden Achievements Probably at the SBS Premium Store™. You need SBS Gold Membership™ to access the SBS Premium Store™ though.