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.
love.graphics.newimage returns nil in module
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 2
- Joined: Sun Oct 22, 2017 11:43 pm
Re: love.graphics.newimage returns nil in module
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
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
-
- Prole
- Posts: 2
- Joined: Sun Oct 22, 2017 11:43 pm
Re: love.graphics.newimage returns nil in module
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?
- 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
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).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?
So if you have a file that contains this...
Code: Select all
print("begin")
require "something"
print("end")
Who is online
Users browsing this forum: plexity and 3 guests