What happened to 'Data'?... =S

BlitzMax Forums/BlitzMax Beginners Area/What happened to 'Data'?... =S

spraycanmansam(Posted 2007) [#1]
Hi guys, I'm new to the BlitzMax scene. I was using an old copy of BlitzBasic from a magazine, and recently bought BlitzMax =D.

I am busy making an asteroids style game and I really need a substitute for the 'data' command ( i think thats what it was called? ).

It's so I can make pre-written levels. How I would have done it in BB is create a data file like so:

.level1
Data 2
Data 1, 50, 50
Data 4, 45, 35

where '.level1' is the level, data 2 is how many objects there are in the list, 1 is the type of asteroid, 50 is the starting x position on the screen, etc..

Then in a function called say newlevel() i would have restored and read the values and ran a for loop for the amount of objects...

I hope you're still with me.. =P My question is how can I emulate this in Blitzmax? The only way I can think of is to read the values from a text file, but I'm unsure of how to do that? Or there may be a function in BMax that I'm not aware of?

Your help is much appreciated!

Cheers,
Sam


Dreamora(Posted 2007) [#2]
DefData and the like you mean? :)


tonyg(Posted 2007) [#3]
Look at 'Defdata'


Yan(Posted 2007) [#4]
http://www.blitzbasic.com/Community/posts.php?topic=41179


spraycanmansam(Posted 2007) [#5]
Thank you very much! Just what I needed. EDIT: fixed my problem, check out the code below and tell me if its the right way to do it?


spraycanmansam(Posted 2007) [#6]
Ok, so after some fiddling I got this to work (it draws '2' little squares at the specified 'x' and 'y' coords ) ---


Graphics 800, 600, 0;


Type block

Field x, y;
Global list:TList;


Function createblock( x, y )

If Not list list = CreateList();
b:block = New block
list.addlast( b );

b.x = x;
b.y = y;

End Function


End Type


RestoreData level1
ReadData num;

For x = 1 To num

ReadData xpos;
ReadData ypos;

block.createblock( xpos, ypos );

Next



While Not KeyHit(key_escape)

Cls

If block.list

For b:block = EachIn block.list
DrawRect( b.x,b.y,10,10 );
Next

End If

Flip

Wend



#level1

DefData 2
DefData 50, 50
DefData 300, 400


spraycanmansam(Posted 2007) [#7]
Ok, now I'm trying to implement this into my code and I've hit a snag...

Is there a way I can restore data by what level you're on? for e.g

-------------------

level = 2

RestoreData lvl + level


#lvl1
DefData blah
DefData blah

#lvl2
DefData blah
DefData blah

-------------------

...or something to that effect?


Dreamora(Posted 2007) [#8]
No, restore are hardcoded links.

What you have to do is write different functions or a function which you give a string that is then selected and the appropritate restore is called.

In any way: I would not store it within the app unless its really about no data ... in any other case external files are much better and far simpler to handle dynamically.


big10p(Posted 2007) [#9]
Cant you bung all the data into an array, instead?


spraycanmansam(Posted 2007) [#10]
I've never been good with arrays big10p...

Thanks Dreamora. How the heck do I do it externally?

This is the last piece of the puzzle I need to figure out to get my game completed!


Jesse(Posted 2007) [#11]
why don't you use rand to generate the locations? it will prevent it from becoming the same old game all of the time, you will only need to use it at the beggining of each level, and it will save you from writting a lot of unnecessary code/data. Defdata can be usefull I just don't think it is that effective in a case like this. Just my 2 cents.


Czar Flavius(Posted 2007) [#12]
Make a text file perhaps with this format...

4
1 50 20 3 60 70 2 90 80 2 10 30
2
1 10 50 4 60 80


The first number tells you the number of asteriods on the level. Then in the list you have the type of asteriod, and its x y. It's not very readable but it doesn't have to be.

In your game open create a level counter and asteriod counter variable. Now open the file.

The first number you encounter is the number of asteriods for the first level. So set the level counter to 1 (or 0?) and the asteriod counter to that number. The very next number you read store as the asteriod type, the next number its x and the next number its y. Then reduce the asteriod counter by 1. Loop through this until the the asteriod counter reaches 0. You've now saved the data for the first level.

Increment the level counter by 1 and set the asteriod counter to the new value. Keep going through the file until you read the end of file EOF.


spraycanmansam(Posted 2007) [#13]
@Jesse
Thats how I had it, but sometimes it would spawn a small rock in level one and sometime a huge rock... and sometimes they'd spawn right on top of you/next to you, i didnt like that! I can see what you mean tho.

Thanks Czar. I'm going to have a good look at what you suggested. At the moment I've set it up to read the values from a separate file called level_data.bmx and use a function in my game called read_level() to store the positions at the start of the level. It works well and I'm not too fussed for this game, I just want to finally finish one! I will need to figure out how to use external files for my next games tho... Thanks All!


sswift(Posted 2007) [#14]
He sacrificed himself to save Picard in Nemesis.


Czar Flavius(Posted 2007) [#15]
Alternatively you could use a special charector at the end of each list of asteriods (or just use the newline) so that you could add or remove asteriods without needing to update the count number.


Jesse(Posted 2007) [#16]
this is what I did on mine to prevent asteroids from spawning just anywhere:
'width = worldwidth
'height = worldheight
Global LeftMax# = width/4
Global RightMin# = LeftMax*3
Global TopMax# = height/4
Global BottomMin# = TopMax*3

Function getx#()
	Local px#
	Repeat 
		px=Rand(0,width)
	Until px < LeftMax Or px > RightMin
	Return px
End Function

Function gety#()
	Local py#
	Repeat
		py = Rand(0,Height)
	Until py<TopMax Or py >BottomMin
	Return py
EndFunction

I am shure different size rocks can also be managed in a similar way.


Snixx(Posted 2007) [#17]
sswift: dont go there... it DIDNT happen