## Trying to split code into two files

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

### Trying to split code into two files

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

Мэтю
Prole
Posts: 31
Joined: Mon Jan 06, 2014 1:24 pm
Location: Espírito Santo, Brazil
Contact:

### Re: Trying to split code into two files

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 = {}

map.woy = "whatever goes here"
end

function map:update(dt)
end

function map:draw()
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>

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:
World needs love.

dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

### Re: Trying to split code into two files

Мэтю 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 = {}

map.woy = "whatever goes here"
end

function map:update(dt)
end

function map:draw()
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>

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

easy82
Party member
Posts: 184
Joined: Thu Apr 18, 2013 10:46 pm
Location: Hungary

### Re: Trying to split code into two files

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.

### Who is online

Users browsing this forum: No registered users and 63 guests