Reading Data Fail?
BlitzMax Forums/BlitzMax Beginners Area/Reading Data Fail?
| ||
Strict '--Initialisation '---------------- Const scrw:Int = 800 Const scrh:Int = 600 Const scrd:Int = 0 Const bw:Int = 20 Const bh:Int = 20 Type Tmap Field scrw:Int Field scrh:Int Field blckw:Int Field blckh:Int Field map:Int[,] Method New() End Method Method Create(w:Int, h:Int, bw:Int, bh:Int) scrw = w scrh = h blckw = bw blckh = bh map = New Int[w, h] For Local x = 0 To 40'(scrw / blckw) For Local y = 0 To 20'(scrh / blckh) ReadData map[x, y] Next Next End Method Method Draw() For Local x = 0 To scrw / blckw For Local y = 0 To scrh / blckh If map [x, y] = 1 Then SetColor 40, 40, 40 DrawRect x * blckw, y * blckh, blckw, blckh SetColor 0, 0, 0 DrawRect (x * blckw) + 1, (y * blckh) + 1, blckw - 2, blckh - 2 End If Next Next End Method End Type '--Setup '------- AppTitle = "Platform" SetGraphicsDriver GLMax2DDriver() Graphics scrw, scrh, scrd '--Main program loop '------------------- Local map:Tmap = New Tmap map.Create(scrw, scrh, bw, bh) RestoreData room1 While Not KeyHit(KEY_ESCAPE) And AppTerminate() = False Cls map.Draw Flip Wend '--Functions '----------- Function RectsOverlap:Int(x1:Float,y1:Float,w1:Float,h1:Float,x2:Float,y2:Float,w2:Float,h2:Float) If x1 > (x2 + w2) Or (x1 + w1) < x2 Then Return False If y1 > (y2 + h2) Or (y1 + h1) < y2 Then Return False Return True End Function '--Data definitions '------------------ #room1 '--data 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9 DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 DefData 1,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 DefData 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 DefData 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 I'm getting an unhandled exception - attempting to read beyond end of data. But everything seems to be in order, obviously it's nbot lol. Could anyone kindly shed any light on where I'm going wrong? It highlights the 'ReadData map[x, y]' line when the error pops up. Thanks as always! |
| ||
For Local x = 0 To 40'(scrw / blckw) For Local y = 0 To 20'(scrh / blckh) ...should be... For Local x = 0 To 39'(scrw / blckw) For Local y = 0 To 19'(scrh / blckh) [edit]Gah, bloody code tags still broken. :/ Last edited 2013 |
| ||
Ah of course! I've always come-a-cropper when considering whether things start from zero or one. lol Thanks! I also had to review the ordering of my For...Next... loops, as once fixed, it was yielding some unexpected results. And yes, it is annoying that the code tags are still bust. Makes it a bit of an eyesore when posting code. :( |
| ||
Golden rule in Blitzmax - everything is zero-based. Simples. :) |
| ||
Good man, must drill that into myself constantly... :) |
| ||
Why not use For Local x = 0 Until 40'(scrw / blckw) instead? Then you can keep your real length and start at zero… |
| ||
Ooh, I wasn't aware of that usage of 'Until', thanks! The '40' was more of a test as I want the commented out calculation to apply (but wasn't at the time, entirely sure what value it was returning). Thanks again gentlemen! :) |
| ||
In addition to this, I'm toying with the best way to hold data for a 'flick screen' 2D game. The code above obviously holds the 'current' screen the player is in, but what's the best way to go about 'the other' screens? Such as keeping track of which items have been collected within a certain screen (room) etc? Hmm, I'm sure there are many ways of approaching this, just not sure which way to go. |
| ||
Hiya, It depends on the gameplay. I made a similar game that uses the 'flick screen' format. It's a push block puzzle in a maze type of game, however the blocks would only stay in the screen they started in, as opposed to moving from one screen to another. The blocks can move inside a static maze, so I stored the static maze data once for each screen in a TGameScreen type. This TGameScreen holds the data for just one static maze, and 2 sets of data for each movable block. Reason for 2 sets of data for each block is that you to store the initial position for each fresh game, and also the position during the current gameplay. At the start of new game I copy the initial starts positions into the current start positions. The static background is a simple array exactly as you have above. Then I have an array of TGameScreens. To switch to display a different screen was then as simple as having the current screen stored as integer, which would be the index into the array of TGameScreen. I have a Draw method in the TGameScreen type, which then makes the drawing of each screen as :- [pseudo code] Global CurrentScreen = 20 Global GameScreen:TGameScreen[100] Repeat HandleAllGameAspects GameScreen[CurrentScreen].Draw Forever [/pseudo code] For your example of tracking what objects are in which screen, I'd store the object into the screen data itself, as opposed to storing the screen data into the object, if that makes sense. Then as you draw/handle each screen and whatever objects are within it, you can simply add or remove objects to the screen data. The alternative would be searching through every object looking for which screen they are in, not very efficient if you have hundreds or thousands of object to work with. Edited because of my initial poor punctuality :P Last edited 2013 |
| ||
Thanks for the reply, that certainly sounds like a good approach. Will give it a go and see how I get on. Planning it out 'on-paper' beforehand though, certainly seems to aid me doing that. :) Appreciated! :) |