FindData returns the position of the first occurence of the information specified in SearchValue. This field can be a string, a float, or an integer value.
The StartPos parameter controls the starting position of the search. You can set the absolute position you want or if you set it to -1, it'll start searching from the current data pointers poition. When the Direction parameter is set to True(1), FindData searches backwards from StartPos to 0; otherwise it searches forward (from StartPos to end of GetDataQuantity).
You can use the returned value in connection with Restore to reset the internal data pointer. Any future calls of ReadData() functions would then start grabbing the information from that position.
FACTS:
* FindData does not change the data pointer.
* FindData searches use explicit type matching. So if you search for an Integer value, then it'll only compare integer values on the data heap. The same goes for Floats and Strings searches.
Mini Tutorial #1:
; create an array that hold the names of the data types Dim DataTypes$(2) DataTypes$(0) = "Integer" DataTypes$(1) = "Float" DataTypes$(2) = "String" ; Restore to position of the string "B" Restore FindData("B",1,0) ; Print Data info Print "Current Data Pointer: " + Str$(GetDataPointer()) Print "Current Data Type : " + DataTypes$(GetDataType()) Print "Data value : " + ReadData$() Print "" ; Restore to position of the value #2 within the data statements Restore 2 Print "Current Data Pointer: " + Str$(GetDataPointer()) Print "Current Data Type : " + DataTypes$(GetDataType()) Print "Data value : " + Str$(ReadData()) Print "" ; embedded data Data 9,8,7,6,5,4,3,2,1 Data "A", "B", "C", "D", "E", "F" ; Display the Screen and wait for the user to press a key Sync WaitKey |
This example would output. Current Data Pointer: 10 Current Data Type : String Data value : B Current Data Pointer: 2 Current Data Type : Integer Data value : 7 |
Mini Tutorial #2: Loading Level Data Stored In Data Statements
This example uses FindData() to locate some simple tile maps stored as data statements.
While you can store level data inside the program as data statements, it's generally best to store them externally or as an resource. Check out LoadLevel and #AddResource for some more information on that.
; 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 past 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 |
|