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
Before you make a thread asking for help, read this.
Post Reply
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

Trying to split code into two files

Post by dyl4n130 »

I have two files: main.lua and map.lua. Essentially the main is just were I put my code and the map is a load, update, and draw function that handle the defining, collisions, and drawing of map items respectively. I have "require("map")" at the top of my main.lua outside of any functions (I have also tried to place it inside the load function).

When I try to run the program I get this error: "attempt to preform arithmetic on global 'woy' (a nil value)" (woy is a variable name)

How do I fix this?
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Trying to split code into two files

Post by Jasoco »

Can we see the code for both files? Obviously at some point in your code you're referring to "woy" but before it's defined. Where is it defined and where is it being called when it crashes? Make sure you're defining "woy" in "map" if that's where it's being defines and not in "main".
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

Re: Trying to split code into two files

Post by dyl4n130 »

Jasoco wrote: Sat Jul 29, 2017 7:45 pm Can we see the code for both files? Obviously at some point in your code you're referring to "woy" but before it's defined. Where is it defined and where is it being called when it crashes? Make sure you're defining "woy" in "map" if that's where it's being defines and not in "main".
https://pastebin.com/wY3Tv6s4 <- Code from main.lua

https://pastebin.com/dg9bEw4x <- Code from map.lua

http://www.mediafire.com/file/e76xvxv2asx4fm1/love.rar <- Here's everything if you want to try to run it yourself (the two code files and two images) (if you want to just create your own filler images make player 16x16 and walls 16x160)
JoshGrams
Prole
Posts: 33
Joined: Sat May 27, 2017 10:58 am

Re: Trying to split code into two files

Post by JoshGrams »

You can't define love.update and love.load and so on more than once: the latest one will just replace the earlier ones. There are any number of ways to deal with that, but basically the map file should probably define some load and update and draw functions which are called from love.update etc. in your main file. And you might want to look into the various tools for creating lua modules and objects.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 97 guests