Page 1 of 1

Using the love api in other classes/tables other than main.

Posted: Mon Sep 22, 2014 4:07 am
by Visions
Hello all. First time posting here. I am pretty new to Love. So far I like it. I did have a question that I believe is a bit noob, but nevertheless I need to figure it out.

So calling the love api from another class other than the main class/table ex.

Code: Select all

image = love.graphics.newImage('p1.png')
It returns nil for love. In my former experiences I have applied the following techniques to get around this:

singletons - if the api was limited to the main class I can just make the call through a singleton
dependency injection - Just passing the "object" to the child class can give it access to the api
another way I have seen it done specifically in lua is
mocking function - I seen an example of this in the anim8 library (https://github.com/kikito/anim8)

Is there an easier way to get around this? Or are any of these 3 technices something I may have to apply? I appreciate any help.

Thanks,
Visions

Re: Using the love api in other classes/tables other than ma

Posted: Mon Sep 22, 2014 5:39 am
by rmcode
Without the .love file it is hard to say what the actual problem is.

Normally you wouldn't have to do anything special to call love functions from other modules. Are you sure you are using the correct folder structure?

Re: Using the love api in other classes/tables other than ma

Posted: Mon Sep 22, 2014 9:55 am
by Plu
love should be a global that's everywhere, unless your other file is either outside of love (ie; you're calling it through your own Lua interpreter) or if it's running inside a thread or channel, which by default don't load any love modules.

But yeah; include a .love and we'll be able to say more.

(EDIT: Or if you've overridden love with nil somewhere; it is just a variable after all)

Re: Using the love api in other classes/tables other than ma

Posted: Tue Sep 23, 2014 3:27 am
by Visions
Thanks for the replies. It turns out that I was pointing at the directory incorrectly. I had to point to the png as if it was being directed from the main class

like this
from symbol.lua:

Code: Select all

image = love.graphics.newImage("/assets/symbols/p1.png")
as oppose to this

Code: Select all

image = love.graphics.newImage("../assets/symbols/p1.png")

folder structure

assets
--> symbol
_ --> p1.png
view
--> symbol.lua

conf.lua
main.lua

Not sure if that lame diagram even helps but that was the issue. Thanks again.