I need help with DefData and Arrays
BlitzMax Forums/BlitzMax Beginners Area/I need help with DefData and Arrays
| ||
Hi :) I started programming a small platformer with BlitzMax and now I'm totally confused about a bug in my program. I'd like to store the map data with DefData commands (just for now, because of simplicity) and load the map into an array. Should be no problem... should ;) I cut a few lines out of my code to show you my problem: In the first line of my map, there are only zeroes, but somehow there appear 200's in the array, when i try to load the map. I try to solve this problem since hours and I just can't figure out, what I'm doing wrong here. Maybe I'm dumb or blind or something, but pleeease help me, I get crazy :D Here comes the code that shows off what I mean: SuperStrict Global levelWidth:Short Global levelArray:Short[399, 14] Local x:Short Local y:Short Function loadLevel(nr:Short) Local y:Short Local x:Short Local data:Short Select nr Case 1 RestoreData level001 Case 2 'RestoreData level002 EndSelect ReadData levelWidth For y=0 To 14 For x=0 To levelWidth ReadData data levelArray[x, y]=data Next Next EndFunction Graphics(640, 480, 32, 0) HideMouse loadLevel(1) Repeat Cls For y=0 To 14 For x=0 To levelWidth DrawText(levelArray[x, y], x*40, y*20) Next Next Flip Until KeyHit(KEY_ESCAPE) '====== LEVEL DATA ====== #level001 DefData 39 'width of the map DefData 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 000, 000, 000, 000, 000, 000, 000, 000, 200, 200, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 200, 200, 200, 200, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 000, 100, 000, 000, 200, 200, 200, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 DefData 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 |
| ||
the array size should be 399x15 not 399x14. Because there are fifteen lines of code and it crashes as a result of that. other than that the code is fine. there are a couple of 200s in lines 12 and 13 and the last line but other than that it is OK. when I run it it does not show any 200s in the first line |
| ||
There are two problems here. The first, as mentioned, is that the second array index in levelArray:Short[399, 14] ranges from 0 to 13, not 0 to 14. The second is the difference between having Debug mode turned on or off. With Debug on the 'out of bounds' array index of 14 will cause a crash and an error message. With Debug off there is no bounds checking. If you assign a value to levelArray[x,14] the compiler will blindly attempt to do this even though the 14 is larger then the maximum allowed index of 13. The result is that the value is assigned to the wrong memory location. This can still cause a crash if that location is not available to your program. But with the 399 there is plenty of extra space in the array so your program never crashes. It just puts the value into the wrong array element. |
| ||
Shorts are slower than Ints :) |
| ||
the array size should be 399x15 not 399x14. Because there are fifteen lines of code and it crashes as a result of that. Oh, yes, that's it! Haven't noticed that the array indexing changed since B3D. *shame* The second is the difference between having Debug mode turned on or off. Again yes, should have turned the debugger on. *even more shame* [qoute]Shorts are slower than Ints :) [/qoute] I didn't know this. Will change all shorts to ints. Thank you guys, you helped me a lot! :) |
| ||
If you're looking to save memory you could still keep the array as an array of shorts |