Trouble returning tables from a separate file

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
DieRandomDie
Prole
Posts: 1
Joined: Wed Jan 29, 2020 12:17 am

Trouble returning tables from a separate file

Post 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
  }
}
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Trouble returning tables from a separate file

Post 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", ...
JJSax
Prole
Posts: 47
Joined: Fri Apr 04, 2014 3:59 am

Re: Trouble returning tables from a separate file

Post 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.
}
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Trouble returning tables from a separate file

Post by raidho36 »

Just use "map2" instead. Or give it some appropriate name like "parameters".
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests