Page 1 of 1

Trying to split code into two files

Posted: Sun Jul 30, 2017 12:19 am
by dyl4n130
I have two files: main.lua and map.lua. main.lua contains all my code for a quick programming exercise I am working on. I am trying to have another file that contains 3 functions: load, update, and draw. These functions handle the defining of map objects, the collisions of the map objects, and the drawing of map objects respectively. These work perfectly fine within main.lua however I want to seperate them just for ease of use. I have googled around a bit and placed require("map") at the top of my load funtion in main.lue however when I attempt to run the game I get this error: "attempt to preform arithmetic on global 'woy' (a nil value)" and 'woy' is one of my variables, this error occurs on line 16 which is one of the first lines within the update functions. How should I fix this?

EDIT: okay so last night I tried to make a similar post but it didnt post so I thought it was lost so this morning I made this post only to find that the other one did actually post. So now I have the same question up twice, sorry about that. Im going to leave this one up since I think it is worded better

Re: Trying to split code into two files

Posted: Mon Jul 31, 2017 9:39 pm
by Мэтю
If you post a .love file would be better, so we can see what's going on with your code. Anyways, maybe the problem is related to your variable scope. So, you got your "woy" variable inside your map file, if you defined it as local:

Code: Select all

local woy = ...
, then its only going to be "visible" within your map file. If you want to acess this variable from another scope, there are ways you can do it. One way is to define it as global, so you can acess it outside its file scope, although it's not recommended. Another way you can do something like the below code:

Code: Select all

-- map.lua 
local map = {}

function map:load()
      map.woy = "whatever goes here"
end

function map:update(dt)
      --Your update code
end

function map:draw()
      --Your draw code
end

return map

Code: Select all

--main.lua

local map = require 'map' -- We can do it since your file map.lua returns the table map, now this <local map> refers to the local map table within <map.lua> 

function love.load()
      map:load() -- Calls the 'load' function from map
end

function love.update(dt)
      map:update(dt)
end

function love.draw()
      map:draw()
      love.graphics.print(map.woy)
end
And there we go. C:

If it were the real problem, there plentty articles about code scope, I've read an interesting one:

http://blogs.love2d.org/content/scope-within-lua

Hope it helps C:

Re: Trying to split code into two files

Posted: Mon Aug 07, 2017 3:02 am
by dyl4n130
Мэтю wrote: Mon Jul 31, 2017 9:39 pm If you post a .love file would be better, so we can see what's going on with your code. Anyways, maybe the problem is related to your variable scope. So, you got your "woy" variable inside your map file, if you defined it as local:

Code: Select all

local woy = ...
, then its only going to be "visible" within your map file. If you want to acess this variable from another scope, there are ways you can do it. One way is to define it as global, so you can acess it outside its file scope, although it's not recommended. Another way you can do something like the below code:

Code: Select all

-- map.lua 
local map = {}

function map:load()
      map.woy = "whatever goes here"
end

function map:update(dt)
      --Your update code
end

function map:draw()
      --Your draw code
end

return map

Code: Select all

--main.lua

local map = require 'map' -- We can do it since your file map.lua returns the table map, now this <local map> refers to the local map table within <map.lua> 

function love.load()
      map:load() -- Calls the 'load' function from map
end

function love.update(dt)
      map:update(dt)
end

function love.draw()
      map:draw()
      love.graphics.print(map.woy)
end
And there we go. C:

If it were the real problem, there plentty articles about code scope, I've read an interesting one:

http://blogs.love2d.org/content/scope-within-lua

Hope it helps C:

thank you very much for the reply, i will try this

Re: Trying to split code into two files

Posted: Mon Aug 07, 2017 5:22 am
by easy82
Hello! One of the best things in Löve that you can easily learn from others. If you have any games in .love format, rename them to .zip, and have a look inside! You can learn a lot from others' solutions.