Data allows you to embed constant data (integers, floats and strings) in your source code. Use the ReadData, ReadData#, and ReadData$ functions to get the data.
Programmers use Data statements in many different ways, but if I had to generalize, they're mainly used to initialize some array or type with some standard information (numeric and string). Some possible usages in game programming could be anything from setting up our games default high score list, through to things like animation data, character properties and even map and level descriptions themselves.
Bellow you'll find a few examples of how you might use data statements.
FACTS:
* Data can only store constant / Literals data in your source code.
* Data statements can include expressions, providing the result can be computed at compile time. Meaning you can use expressions that contain constants, literals and many built in functions (Maths and Strings functions mainly), but the expression can't include Variables/Arrays or User defined functions. Since such expressions can only be calculated when the program is running.
* Data statements are always consisted to be global. So placing a Data statement inside a function, won't make the data only available inside the function, rather it'll just be added to the global heap of data.
* If you want store large binary files in your program, we recommend using resource buffers instead. (See #AddResource)
I N D E X
Mini Tutorial #1: Basic Data Statements
Ib this first example we're justget soime idea of the nuts and blots functionality of embedding data in our programs.
; read the defined data ; read strings data Print ReadData$() Print ReadData$() ; read integers Print ReadData() Print ReadData() ; read floats Print ReadData#() Print ReadData#() ; embedded data Data "Play", "Basic", 3 Data 42, 3.21, 1.1 ; Display the Screen and wait for the user to press a key Sync WaitKey |
This example would output.
Top
Mini Tutorial #2: Initializing High Score Table
This example shows a way we can store a list of high score information in our program. The program declares a type with a name and score fields, creates an array and then reads the data. Once it's all been read it displays the high score table to the screen.
; Declare a type to hold the name and score of the players ; in our games high score list Type tHighScore Score Name$ EndType Dim HighScore(5) As tHighScore ; Set the data pointer to the start of the high score list ; in this programs data heap. Restore DefaultHighScoreTable ; Read the data and store it in the high score table For lp=1 To 5 HIghScore(lp) =New tHighScore ; read the next string from the data heap ; and store it in this type HighScore(lp).Name$ =ReadData$() ; read the next integer from the data heap ; and store that in the score field HighScore(lp).Score =ReadData() Next ; ------------------------------------------- ; Display the high score table on the screen ; ------------------------------------------- LoadFont "verdana",1, 32 For lp =1 To 5 Name$ =HighScore(lp).Name$ Score$=Digits$(HighScore(lp).Score,8) ypos=100+lp*50 Ink ARGB(255,255,255,255) Text 400-(16+GetTextWidth(name$)),ypos,Name$ Ink ARGB(255,30,50,80) Text 400,ypos,Score$ Next Sync WaitKey ; ------------------------------------------- DefaultHighScoreTable: ; ------------------------------------------- Data "PlayBASIC" , 100000 Data "Dude" , 700000 Data "Kev" , 550000 Data "Socks" , 350000 Data "Dude" , 10000 |
Top
Mini Tutorial #3: Dynamically Creating Objects From Data Statements
This example sets up a frame work in our program where we can define the character initialization function with parameters as a list of strings. The program breaks the string data up into a set of fields and then calls the function in question via callfunction. The strings are stored as data statements, but they could just as easily be loaded from an external file. This approach allows might be a little more complicated to wrap your mind around, but it lets us write a generic layers of code, where we don't have to modify the base layer to add different character types and classes. Which is well worth exploring !
` *=---------------------------------------------------------------------=* ` ` This is example uses the CallFUNCTION feature to dynamically call ` the various creation functions from a list of data statements. ` ` *=---------------------------------------------------------------------=* Constant ObjectClass_Circle =1 Constant ObjectClass_Box =2 Type tObject ObjectClass x#,y# size colour EndType Dim Object(10) As tobject // -------------------------------------------------- // Spawn the objects from the data statements bellow // -------------------------------------------------- Repeat obj$=ReadData$() SpawnObject(obj$) Until obj$="" // -------------------------------------------------- // Demo loop // -------------------------------------------------- SetFPS 60 Do Cls 0 For lp=1 To GetArrayElements(Object(),1) If Object(lp) Select Object(lp).Objectclass Case ObjectClass_Circle CircleC Object(lp).x,Object(lp).y,Object(lp).size,true,Object(lp).Colour Case ObjectClass_Box size=Object(lp).size x1#=Object(lp).x-size y1#=Object(lp).y-size x2#=Object(lp).x+size y2#=Object(lp).y+size BoxC x1#,y1#,x2#,y2#,true,Object(lp).Colour EndSelect EndIf Next Sync Loop ; --------------------------------------------------------------------- ; --------------------------------------------------------------------- ; Sample object creation data.. ; Parameter format. X,y,size,colour Data "CreateBall[100,200,50,$ff0000]" Data "CreateBox[400,400,25,$00ff00]" Data "CreateBox[700,100,10,$ff00ff]" Data "" ; end of list of ` *=---------------------------------------------------------------------=* ` >> Spawn Object << ` *=---------------------------------------------------------------------=* ` ` This routine takes in the input string, then parses the string into ` the function to calls name, and it's standard parameter list. ` ` Since this us generic, we can add more object types without having to ` explicitly parse them out in here. As long as the function name ` exists and the input params match, then we're golden. ` ` *=---------------------------------------------------------------------=* Function SpawnObject(obj$) Obj$=Trim$(Obj$," "+Chr$(9)) If Len(obj$)>5 // Get the Function to Calls NAME$ pos=InString(obj$,"[",1,false) FunctionName$=Left$(Obj$,Pos-1) pos++ // Get the param list params$=Mid$(obj$,pos,Len(obj$)-pos) // split this list into string array Dim Fields$(10) Count=SplitToArray(params$,",",Fields$(),1) // convert the fields from strings to values x#=Val#(Fields$(1)) y#=Val#(Fields$(2)) size#=Val#(Fields$(3)) colour=Val(Fields$(4)) // Call this function by name CallFunction FunctionName$,x#,y#,Size#,Colour EndIf EndFunction ` *=---------------------------------------------------------------------=* ` >> Create (Spawn) Ball Object << ` *=---------------------------------------------------------------------=* Function CreateBall(x#,y#,RAdius,Colour) ThisBall=GetFreeCell(Object()) Object(ThisBall).ObjectClass=ObjectClass_Circle Object(ThisBall).x=x# Object(ThisBall).y=y# Object(ThisBall).size=Radius Object(ThisBall).colour=colour EndFunction ` *=---------------------------------------------------------------------=* ` >> Create (Spawn) BOX Object << ` *=---------------------------------------------------------------------=* Function CreateBox(x#,y#,RAdius,Colour) ThisBall=GetFreeCell(Object()) Object(ThisBall).ObjectClass=ObjectClass_Box Object(ThisBall).x=x# Object(ThisBall).y=y# Object(ThisBall).size=Radius Object(ThisBall).colour=colour EndFunction |
Top
Mini Tutorial #4: Loading Levels From Data Ststements
This shows how we can store simple level tile maps inside our program code as data statements.
; create a map with space for 5 levels ThisMap=NewMap(5) ; compute the location of the help files Help_Path$ =ProgramDir$()+"Help\Commands\" ; get the filename of the grass blocks from within ; the help files Blocks$=Help_Path$+"Media\Grass-Tiles.bmp" ; load the blocks into our map LoadMapGFX Blocks$,ThisMap,32,32,RGB(0,0,0),2 ; Load Level from the internal data heap Level1=LoadLevelFromData(ThisMap,"Level1") ; draw this level to the screen DrawMap ThisMap,Level1,10,0 ; Load Level from the internal data heap Level2=LoadLevelFromData(ThisMap,"Level2") ; draw this level to the screen DrawMap ThisMap,Level2,10,360 ; Sync WaitKey // -------------------------------------------------------- // -------------------------------------------------------- // ------------->> Load Level From Data <<----------------- // -------------------------------------------------------- // -------------------------------------------------------- Function LoadLevelFromData(ThisMap,LevelID$) StartPos =FindData(LevelID$) ; set the data pointer pass the String ID Restore StartPos+1 ; get the size Width =ReadData() Height=ReadData() ; create a level to store it in ThisLevel=NewLevel(ThisMap,Width-1,Height-1) For ylp=0 To Height-1 For xlp=0 To Width-1 ThisTile=ReadData() PokeLevelTile ThisMap,THisLevel,Xlp,YLp,ThisTile Next Next EndFunction ThisLevel ; ------------------------------------- Data "Level1" ; ------------------------------------- Data 11,11 Data 1,1,1,1,1,1,1,1,1,1,1 Data 1,2,2,2,2,2,2,2,2,2,1 Data 1,2,3,3,3,3,3,3,3,2,1 Data 1,2,3,4,4,4,4,4,3,2,1 Data 1,2,3,4,5,5,5,4,3,2,1 Data 1,2,3,4,5,9,5,4,3,2,1 Data 1,2,3,4,5,5,5,4,3,2,1 Data 1,2,3,4,4,4,4,4,3,2,1 Data 1,2,3,3,3,3,3,3,3,2,1 Data 1,2,2,2,2,2,2,2,2,2,1 Data 1,1,1,1,1,1,1,1,1,1,1 ; ------------------------------------- Data "Level2" ; ------------------------------------- Data 23,7 Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 Data 0,0,9,0,9,0,9,9,9,0,9,0,0,0,9,0,0,0,0,9,0,0,0 Data 0,0,9,0,9,0,9,0,0,0,9,0,0,0,9,0,0,0,9,0,9,0,0 Data 0,0,9,9,9,0,9,9,0,0,9,0,0,0,9,0,0,0,9,0,9,0,0 Data 0,0,9,0,9,0,9,0,0,0,9,0,0,0,9,0,0,0,9,0,9,0,0 Data 0,0,9,0,9,0,9,9,9,0,9,9,9,0,9,9,9,0,0,9,0,0,0 Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
Top
|