Page 1 of 1

Trouble returning tables from a separate file

Posted: Wed Jan 29, 2020 12:26 am
by DieRandomDie
Here's the contents of my maps.lua file. I can get this to work from the main.lua but I cannot get this to work using return. I have no idea what I'm doing and I'm basing my code off of something that is 7 years old.

Code: Select all

return {
  name = "Dungeon Floor 1",
  map = {
    0,1,1,1,0,1,1,2,
    0,1,0,1,0,1,0,0,
    0,0,1,1,0,1,1,1,
    1,1,1,0,1,1,0,1,
    1,0,1,1,0,1,1,0,
    1,1,1,0,0,1,0,1,
    0,1,0,1,1,1,1,1,
    1,1,1,1,0,1,1,0
  }, --Error is here?
  map[2] = {
    desc="Corridor",
    chest=false,
    trap=false,
    rate=0
  }
}

Re: Trouble returning tables from a separate file

Posted: Wed Jan 29, 2020 1:33 am
by pgimeno
map[2] =
That's not a valid key. Maybe you meant this?

Code: Select all

  map = {
    0,1,1,1,0,1,1,2,
    0,1,0,1,0,1,0,0,
    0,0,1,1,0,1,1,1,
    1,1,1,0,1,1,0,1,
    1,0,1,1,0,1,1,0,
    1,1,1,0,0,1,0,1,
    0,1,0,1,1,1,1,1,
    1,1,1,1,0,1,1,0,
    desc="Corridor",
    chest=false,
    trap=false,
    rate=0
  }
This way, yourtable.map[1] = 0, yourtable.map[2] = 1, yourtable.map[3] = 1, ..., yourtable.map.desc = "Corridor", ...

Re: Trouble returning tables from a separate file

Posted: Wed Jan 29, 2020 5:44 am
by JJSax
To expand on pgimeno's code, you possibly might be thinking about this

Code: Select all

return {
  name = "Dungeon Floor 1",
  map = {
    [1] = {
      0,1,1,1,0,1,1,2,
      0,1,0,1,0,1,0,0,
      0,0,1,1,0,1,1,1,
      1,1,1,0,1,1,0,1,
      1,0,1,1,0,1,1,0,
      1,1,1,0,0,1,0,1,
      0,1,0,1,1,1,1,1,
      1,1,1,1,0,1,1,0
    },
    [2] = {
      desc="Corridor",
      chest=false,
      trap=false,
      rate=0
    }
  }
The reason you were having difficulties is that you were making the table look something like this

Code: Select all

map = {
	[1] = 0,
	[2] = { -- here you were trying to overwrite an index in yourtable.map that doesn't exist until you have the final closing bracket.
  		desc="Corridor",
 		chest=false,
 		trap=false,
  		rate=0
	},
	[3] = 1,
	[4] = 1,
	[5] = 0,
	[6] = 1,
	-- keep going with your numbers.
}

Re: Trouble returning tables from a separate file

Posted: Wed Jan 29, 2020 9:10 am
by raidho36
Just use "map2" instead. Or give it some appropriate name like "parameters".