Page 1 of 1

Problem to separate lua files

Posted: Thu Jul 17, 2014 12:54 am
by joaovvr
Hey guys,
I've already finished my game and it was running normal(without any problems), but it's all in main.lua file and i decided to separate in other files like enemy.lua, player.lua,...
I was doing this when I stumbled upon a problem I can not describe, but I'll post the code of the game here so you know better what it is.

I really hope you guys can help me, because once it works out and files get separated and the game running without any problem, finally I'll be able to add more things and finish the game :rofl:

I apologize for the source code be in portuguese :death:
my first game.love
(644.14 KiB) Downloaded 108 times

Re: Problem to separate lua files

Posted: Thu Jul 17, 2014 6:04 am
by player_258
hi !

browsing through your code the first problem i found was this:

Code: Select all

nave = {}

-- Configurações da Nave --
function novaNave(x, y)
  nave = {}
  nave.x = x or 0
  nave.y = y or 0
  --more code
end

function nave.load()
  -- Cria a nave --
  nave = novaNave(largura / 2 - 32, altura - 96)
end

function nave.update(dt)
 --code
end

function nave.draw()
 --code
end

in the first line you create an empty table called "nave"
in the nave table you create the update and draw callbacks
nave.update
nave.draw

when love.load is ran, it calls nave.load which then calls novaNave(x, y)
novaNave(x, y) overwrites your current "nave" table with an empty one, then
you set values for position, image, size, etc

because of this, nave.update, nave.draw dont exist anymore because
you overwrote them.

what i think you meant to do was this

Code: Select all

nave = {}

-- Configurações da Nave --
function novaNave(x, y)
  local nave = {} --create a new local table, not overwriting the global one
  nave.x = x or 0
  nave.y = y or 0
  --stuff
  return nave --return it to whoever called it
end

function nave.load()
  -- Cria a nave --
  naveObject = novaNave(largura / 2 - 32, altura - 96) --a new naveObject
end
notice that i changed "nave" to "naveObject" because otherwise i would be overwriting
nave (the one with the update and draw callbacks) again.

otherwise you could just use this dirty fix

Code: Select all

nave = {}

-- Configurações da Nave --
function novaNave(x, y)
  nave.x = x or 0
  nave.y = y or 0
  --more code
end

function nave.load()
  -- Cria a nave --
  novaNave(largura / 2 - 32, altura - 96)
end
this would add position, image, etc variables to your current nave table
that would result in a huge nave table with update and draw callbacks, position, image, size, etc variables
not that is a bad thing performance wise, but still

that should fix the first problem that appears when you click the start button. :awesome: