Now that we have a file written on the hard disk, "C:\test\example.txt" we can then open the file and read it into a variable. From there we can put each packet of parsed data into a table/array. I consider this an array because I use numerical indices. Everyone is welcome to use the function to parse their strings within their game level.
What I'm doing is reading reference manuals about lua and sharing with you what I'm learning on the way.
----------------------------------------------------------------------------------
--I actually do not use l_gamedata in this script. It is shown here to
--demonstrate that you can have a table within a table and that
--you can name each element of the table rather than referring
--to them numerically in sequence. Although it is not necessary I always
--(remember I'm old school) like to list my tables/arrays, local variables
--that are used in the script. All global variables I use in the game would be in
--another lua file just as Lee does with his global variables. You can put them in Lee's global.lua
--file except for every time there is a new version, anything you add will be erased.
--Just like Lee, I prefix the variable with "g_" just as I do the local variables with "l_"
--One other thing I like to do is to use variable names that are descriptive.
local l_gamedata = {} --table
--elements of l_gamedata table
l_gamedata.weaponSlotGot = {} -- a table within a table - index is 1-9
l_gamedata.weaponSlotPref = {} -- a table within a table - index is 1-9
l_gamedata.playerHealth = 0
l_gamedata.playerLives = 0
local l_file = "" -- the file to be used
local l_list = {} --list of the parsed data as an array
local l_state = 0
local l_start = 0
local l_str2parse = ""
local l_parsechar = ""
function parsing_test_init(e)
l_start = 1
l_state = 1
-- Opens a file in read mode
l_file = io.open("c:\\test\\example.txt", "r") -- just as "w" was used for write, "r" is used for read
-- assigns first line of the file to variable
-- if you remember from the write file example there is only one line in this file
-- you are able to write more than one line and read more than one line in a file
-- however, for this example we keep it simple main reason for this example
-- is to introduce you to a parsing function which you can use to call any
-- string of text with a parser character.
l_str2parse = l_file:read()
l_parsechar = "|" -- this identifies the parser character
end --function init
function parsing_test_main(e)
if l_start == 1 then
l_start = 0
-- start code goes here that runs only once
l_list=get_parsed_data(l_str2parse,l_parsechar)
else
if l_state == 1 then
--code for state 1 goes here
-- closes the opened file
--Prompt("line = " .. l_str2parse)
l_file:close()
l_state = 2
elseif l_state == 2 then
--code for state 2 goes here
-- the l_list table is basically an array that contains the data from the parsed string
TextCenterOnX(50,25,3,l_list[1] )
TextCenterOnX(50,35,3,l_list[2] )
TextCenterOnX(50,45,3,l_list[3] )
TextCenterOnX(50,55,3,l_list[4] )
TextCenterOnX(50,65,3,l_list[5] )
TextCenterOnX(50,75,3,l_list[6] )
elseif l_state == 3 then
--code for state 3 goes here
elseif l_state == 4 then
--code for state 4 goes here
end
end
end --function main
function get_parsed_data(pString, pParser) -- p stands for parameter there are 2 params for this function that need to be passed
local data_Table = {}
local fparser = "(.-)" .. pParser
local last_end = 1 -- last end increases by '1' until end of string
-- current_num_of_parser is the current number of the parser within the string
-- track_end_of_str is a variable that increments by 1 until the end of the string
-- parsed_data is the parsed packet of data
local current_num_of_parser, track_end_of_str, parsed_data = pString:find(fparser, 1)
while current_num_of_parser do
if current_num_of_parser ~= 1 or parsed_data ~= "" then
table.insert(data_Table,parsed_data) -- this inserts or adds the data packed to the table
end
last_end = track_end_of_str+1 -- increment last_end by 1
current_num_of_parser, track_end_of_str, parsed_data = pString:find(fparser, last_end)
end --while
-- after the while loop ends there is one last packet to add to table
if last_end <= #pString then
parsed_data = pString:sub(last_end)
table.insert(data_Table, parsed_data)
end
return data_Table -- return the table to the table list named l_list
end --function get_parsed_data
Alienware Aurora R7 with SSD 256GB boot drive ( C: ) and a secondary drive ( D: ) that is 2TB
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 with Intel Turbo-burst
Installed RAM 16.0 GB
64-bit operating system, x64-based processor
Windows 10 Home
NVIDIA GeForce GTX 1070 with 8192 MB GDDR5 and 8095 MB shared system memory