How to make a map data array.
Root / Programming Questions / [.]
toastedCreated:
Hello this is my first post in this community, its nice to meet you all :) on petit computer I learned, with the help of a tutorial on the petit computer wiki, how to use an array and BGPUT to make a simple map w/ collision detection. Something like this
DIM MAP [32,24] RESTORE @MAP FOR I=0 TO 23 READ MAPLINE$ NEXT FOR J=0 TO 31 MAP [J,I] IF MID$[MAPLINE $,J,I]=="1" IF MAP [J,I] THEN BGPUT 0,J,I,45,0,0,0 'Now a loop divide py px/8 to get collision data with 1's to represent ground and what nothowever it doesn't seem to be working in smilebasic. I was wondering what changes I should make to the code to get it to work? Or if I have to find another way to pull map data from the array. Any help is appreciated. Thanks for reading :)
I suggest taking a gander at http://smilebasic.com/en/reference/ if you have not already, it may give you an idea as to what's going on.
Also, mind telling what the error/line given is? It may help us to help you.
Ok the error I'm given is "out of data in 0:5" line 0:5 is the code
READ MAPLINE $which I know calls back to the previous code "RETURN @MAP" which in turn points to the actual map data. I can assume this means I'm not using the DATA commands properly? Or maybe it can't find the @map tag. Thanks for the help :) I'll try reading through the smile basic reference website again as well, maybe there is something I missed.
My best guess it that you have malformed DATA.
Other problems I think I'm seeing:
The READ MAPLINE$ loop eats and discards all the data. You want it to encompass the lower part where you actually use MAPLINE$.
I'm not sure what's going on with that MAP[J,I] either. The line is an empty statement, or worse, will break thinking MAP is a command.
IF MID$[MAPLINE $,J,I]=="1" IF MAP [J,I] THEN BGPUT 0,J,I,45,0,0,0These lines look like they should be combined, or maybe just IF MID$(MAPLINE$,J,I)=="1" THEN BGPUT 0,J,I,45,0,0,0
Thank you that fixed my data problem. I apparently made it one line too long, and thats what caused the error. Ive updated my code to
ACLS:CLEAR DIM MAP [32,24] RESTORE @MAP FOR I=0 TO 23 READ MAPLINE$ FOR J=0 TO 31 MAP [J,I]=0 IF MID$(MAPLINE$,J,I)=="1" then MAP [J,I]=1 Next Next FOR I=0 TO 23 FOR J=0 TO 31 IF MAP[J,I] THEN BGPUT 1,J,I,45,0,0,0 NEXT NEXT @ loop 'player movement and col detection @MAP 'mapdatathe code seems to be working the only problem is that it gives me the error "Illegal function call in 0:13(BGPUT) I'm not sure what I'm doing wrong, the code should know that if MAP [J,I] =1 then BGPUT a tile on the coordinates with a "1" in the DATA. That's my theory anyway.
If you look at the reference page that Lumage linked earlier, the definition of BGPUT takes only 4 parameters,and yours has a couple extra. It should look like this:
BGPUT <layer>, <x coord>, <y coord>, <value>I think if you fix that it should work.
Maybe the BG layer isn't defined or visible. But it looks like you are doing an ACLS, which should set layer visibility, but you still have to define the layer size and setup the screen. You might try setting the following after ACLS:
XSCREEN 0, 128, 4 BGSCREEN 0,64,64 BGSHOW 0 ..... ' Then when you do your bgput make sure you define it like this: BGPUT 0, J, I, 1To recap what those commands do: XSCREEN sets the screen mode (0 is just the top screen), and allocates space for sprites (128 of them), and bg layers (4 of them). BGSCREEN sets up the size of your BG layers. In your case, layer 0 will contain 64x64 grid of tiles BGSHOW just ensures that the layer given as a parameter is visible (drawn). If it still doesn't work after making those adjustments, just let me know, and I'll set up an example and send you a key.
It's hard to tell from the example code, since I can't see how your map data is formatted, but if I had to guess, it looks like the string you are storing your map data in is fed a line at a time from your READ loop, and each succession in the loop just overwrites what the last iteration read. Meaning, your MAPLINE$ only contains one row of data.
Anyway, I'll add an example real quick and link it in this post.
EDIT: Here's an example key, I just tweaked your example code a little bit -> [ 7KS3EPJX ]
You're welcome; happy to help :)