Scripts / Mult-Dim Array - retrieving array data from a file

Author
Message
Saintyboy
8
Years of Service
User Offline
Joined: 18th Jun 2015
Playing:
Posted: 19th Oct 2015 00:17 Edited at: 19th Oct 2015 00:23
Ok so I have my multi dim array working fine and now I want to load in the data at start of the game from a file but I keep getting an 'attempt to index field'?'(a nil value)' error and wondered if anyone can see where i'm going wrong
sample data is in a file named Categories - as follows:

Name use calories
apple food 75
chair sit 2000
water drink 01
tree wood 7000

I didn't know whether to separate them with commas?

My code is as follows:
( I tried changing 'file' to io and "*.n" to 1 but no joy)

objtable={}
for i=1,5 do
objtable[i]={} --creates new row in table
end

function catagorisation_tables_init(e)
-- this is where we are setting up the catagorisation of all objects

local file = io.open("Catagories", "r")
local lines = {}
for line in io.lines("Catagories") do

i=line
objtable[i][1] = file:read("*n","*l")
objtable[i][2] = file:read("*n","*l")
objtable[i][3] = file:read("*n","*l")
end

io.close(file)

end
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 19th Oct 2015 10:38
So you want it to be like objtable[1][1] = apples
Objtable[1][2] = food
Etc right?
I believe you will need a way to get the next item (new line or separator)

Also I'm not sure why you have defined lines as an array, this might be confusing the io.lines call

And if that doesn't work I would try adding the for loop inside the init(e) statement
life\'s one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, AMD R9 200 series , directx 11
Saintyboy
8
Years of Service
User Offline
Joined: 18th Jun 2015
Playing:
Posted: 19th Oct 2015 15:04
Hi smallg thanks for the reply - alas I'd tried without defining the lines as an array as indeed I had the for loop in the init loop but neither sorted the issue .

Does my read statement look ok?
Also in the data file should I separate each variable with a comma?
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 19th Oct 2015 16:51
Ignore what you want to do, but this is the script I wrote to get all the weapons between levels, perhaps you can modify it to fit your needs.



"weapons_ids.txt" file looks like this:
4;5;6;7;8;9;10;12;3;
Those numbers are the weapons ids (numbers) inn my map. (notice in your map, they can change)
I'm using regular expressions, and ";" as delimiter, and it work fine, at least for me.

hth

3omm
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
Saintyboy
8
Years of Service
User Offline
Joined: 18th Jun 2015
Playing:
Posted: 20th Oct 2015 00:26
thanks for the reply 3com - sadly your code relates to linear data in a file which is something I have no issue with. My problem is retrieving and saving data to a 2d matrix (i.e. columns and rows variable[z][x]) - it seem game guru doesn't like using for-do loops in an 'io' procedure to retrieve data from a matrix.

Login to post a reply

Server time is: 2024-05-05 02:51:14
Your offset time is: 2024-05-05 02:51:14