love.graphics.newimage returns nil in module

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
minenice55
Prole
Posts: 2
Joined: Sun Oct 22, 2017 11:43 pm

love.graphics.newimage returns nil in module

Post 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.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: love.graphics.newimage returns nil in module

Post 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
minenice55
Prole
Posts: 2
Joined: Sun Oct 22, 2017 11:43 pm

Re: love.graphics.newimage returns nil in module

Post 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?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: love.graphics.newimage returns nil in module

Post 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".
Post Reply

Who is online

Users browsing this forum: No registered users and 54 guests