I was wondering if it was possible with lua to create a table that can have any slot be created on the fly. I want to create a 2d universe that has tiles on it. I would start with a ship on 0,0 and would have a large asteroid made from tiles say a few hundred spaces to any direction.
What I would do now is just have a limited sized precreated tilemap scroll into position and do my things there. When I would fly away I would then just empty that table(tilemap) and then use this for another spot in space. Sort of like island tile maps.
If I would be able say do table[x][y] = turrettile and on other x and y spaces have it just be nil. I would not eat up lots of memory? The integer limit is 64 bit so I would be able to have a super huge universe.
How would I create/use a 2 dimensional table like that?
@Pakz Heres an example. This creates a 2D table containing 20 random entries. These are somewhere in the range of 1000 by 1000. You can just create any entry anywhere. Before using a y entry, you have to make sure there’s an x table entry, tab[x]={} before assigning a value to tab[x][y] .
function setup()
tab={} -- create table
-- create 20 random table positions
for z=1,20 do
-- random x,y table position
x=math.random(1000)
y=math.random(1000)
-- table[x] doesnt exits yet, so create it
if tab[x]==nil then
tab[x]={}
end
-- fill table x,y with some value
tab[x][y]=vec2(x,y)
end
-- print used table positions
for a,b in pairs(tab) do
for c,d in pairs(b) do
print(d)
end
end
end
I had been experimenting myself and got this working :
map = {}
-- make a 50 by 50 map section at -100 x and -100 y
for x=-100,-50 do
map[x]={}
for y=-100,-50 do
map[x][y]=2
end
end
-- make a 50 by 50 map section at 100 x and -100 y
for x=100,150 do
map[x]={}
for y=-100,-50 do
map[x][y]=1
end
end
@Pakz As far as I know, only entries that are used fill the table. In my example above, even though I’m using values up to 1000, only the 20 entries I created are in the table. So you can have a very large possible array, but only use a small number of entries in the table.
I had a hunch back when I started looking at godot and its lua like language. I play Minecraft a lot so having super large worlds I think are neat.
I just created a infinite tile map scroller as a test which generates new tiles areas in runtime ahead in its scrolling direction. This seemed to work as it should.
@Pakz I did something like that a long time ago just to see if it could be done. I never did anything with it and probably deleted the code. I would create texture maps in the direction I was going and I might have deleted what was behind me. I don’t remember if I did or just left them.
I probably wil also drop the space game when I do not get hooked to it. I already have 2 versions made with 2 different languages. This time I wil start with adding a building feature where you can place blocks around the ship. Hoping that being able to build a base wil get me into it
The mineable asteroid with tunnel system generator is done so that just needs copying and fitting.
This was my first version if anyone is interested. Needs a keyboard. Does not always work on all mobile devices.