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

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
Visions
Prole
Posts: 2
Joined: Mon Sep 22, 2014 2:11 am

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

Post 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
User avatar
rmcode
Party member
Posts: 454
Joined: Tue Jul 15, 2014 12:04 pm
Location: Germany
Contact:

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

Post 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?
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

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

Post 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)
Visions
Prole
Posts: 2
Joined: Mon Sep 22, 2014 2:11 am

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

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

Who is online

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