331 posts
Posted 26 August 2013 - 04:20 AM
eg. i want to have a table with all positions and set them to either true or false, false by defualt
and then call them later like
x = 1
y = 1
if screen[y][x] then
-- stuff
end
and it would return true if on column x on line y was true
220 posts
Location
Germany
Posted 26 August 2013 - 04:25 AM
So you want to have all the content on the screen in a table?
Overwrite the existing functions and store everything.
331 posts
Posted 26 August 2013 - 06:37 AM
Yes I know but how would I do this not overly great with tables somehow
123 posts
Posted 26 August 2013 - 09:33 PM
Tables are the only way to do this. Lua doesn't have arrays, just indexed hashes. Initialize a table like this:
local screen = {}
for r = 1, rows do
screen[r] = {}
for c = 1, cols do
screen[r][c] = false
end
end
Then you can set a value like this:
screen[c][r] = true
You could try and use strings instead of tables, but then you'll have a bunch of code like screen:sub(3,3) == "t" to get a value and setting a value would require rebuilding the string. It would be slower, so stick with tables.
7508 posts
Location
Australia
Posted 26 August 2013 - 10:30 PM
Yes I know but how would I do this not overly great with tables somehow
One of the best resources you could ever read.
2217 posts
Location
3232235883
Posted 26 August 2013 - 10:32 PM
"Lua doesn't have arrays, just indexed hashes"
Wut :x
i usually use strings for these types of things
7508 posts
Location
Australia
Posted 26 August 2013 - 10:54 PM
"Lua doesn't have arrays, just indexed hashes"
Wut :x
Yeah I was pretty confused by that too.
1548 posts
Location
That dark shadow under your bed...
Posted 27 August 2013 - 08:13 AM
in my sig is a link to my utils post, in my utils post is a layer API that does this and more. Just remove the local property of the record table and go. it records lines as strings though so use record.text[y]:sub(x,x) to get the info back or just the getTextAt function
123 posts
Posted 27 August 2013 - 10:56 AM
"Lua doesn't have arrays, just indexed hashes"
Wut :x
Yeah I was pretty confused by that too.
Lua only has 8 basic data types. The only two that can be used like an array are a string and a table. A table is not truly an array in the sense that the memory is a contiguous block of data that provides you random access. It is a data structure that uses a lookup table and linked lists of values, which means you are hashing keys and using a collision strategy to find your values. A string is a contiguous block of data, but it is immutable. You can easily get a value from the middle of a string, but you can't easily set it. If you want to replace a character in the middle, you are creating an entirely new string, not just modifying one byte in the middle.