Page 1 of 1

love.graphics.newimage returns nil in module

Posted: Mon Oct 23, 2017 12:01 am
by minenice55
I'm brand new to Love and even Lua as a whole so I may be making some dumb mistake, but when I set "image", for instance, with love.graphics.newimage and a corresponding file path and draw it with love.graphics.draw in main.lua, it runs fine. However, when I run it in karatekaanim.lua (which I called with the require function), I get a drawable expected, got nil error. I'll attatch the .love.
https://drive.google.com/file/d/0B9qZRT ... sp=sharing

Thank you in advance.

Re: love.graphics.newimage returns nil in module

Posted: Mon Oct 23, 2017 1:02 am
by Azhukar
Here's the sequence of events:
1. 'require' will run the file, meaning it will define (A)love.load to be a function you specified
2. function (B)love.load will get reassigned in main.lua after the require
3. framework will then call (B)love.load

This means (A)love.load from the 'require' will never get called and as such the image will never be loaded. To fix this, only assign love.load in one place. It is a purely semantic function that gets executed after all main.lua code. You can define and load your image outside love.load.

See https://love2d.org/wiki/love.run

Re: love.graphics.newimage returns nil in module

Posted: Tue Oct 24, 2017 12:21 am
by minenice55
Thanks for the answer! I just had to load the images outside of love.load (not what I was expecting but ok)! So it seems like main.lua runs code on other lua files, then it's own, correct?

Re: love.graphics.newimage returns nil in module

Posted: Tue Oct 24, 2017 8:50 am
by bartbes
minenice55 wrote: Tue Oct 24, 2017 12:21 am So it seems like main.lua runs code on other lua files, then it's own, correct?
No love runs main.lua to completion, and it and conf.lua are the only files it runs. What you're seeing though is the effect of require. It's no magical "register this for later" function, it instead runs the relevant file right when you call require (unless it is cached).

So if you have a file that contains this...

Code: Select all

print("begin")
require "something"
print("end")
Then you'll first see "begin", then all of "something" runs, then you see "end".