How to draw all objects on screen?

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.
User avatar
Smoggert
Prole
Posts: 29
Joined: Thu Nov 10, 2016 11:23 pm

Re: How to draw all objects on screen?

Post by Smoggert »

Code: Select all

local actors = { n = 0 }
actors.__index = actors

function actors.add(self, actor)
     self.n = self.n + 1
     self[self.n] = actor
end

function actors.draw(self)
	for i=self.n,1,-1 do
		self[i]:draw()
	end
end

function love.load()
   -- actor(name, position, isVisible)
   local chris = actor("Chris", "center", true)
   local tristan = actor("Tristan", "left", false)
   actors:add(chris)
   actors:add(tristan)
   chris:speak("Hello there!")
   tristan:show()
   tristan:speak("Hi there!")
end

function love.draw()
	actors:draw()
end
Try to refrain from using capital letters for objects. Capitals are usually reserved for classes. (weird u didn't give ur actor class a cap?)

All the self stuff isn't rly necessary, but I've been coding stuff like classes for so long I just can't stop writing it like that xD
The issue with current snippit is that you won't be able to use chris or tristan in update code, so you would need either some kinda lookup like actors.getActor(name) function or instead of adding by number u add by name:

Code: Select all

local actors = {}
actors.__index = actors
function actors.add(self, actor)
     self[actor.name] = actor  -- or self[actor:getName()]  not sure how this class library works, never used one for lua
end

function actors.draw(self)
	for name,actor in pairs(self) do
		actor:draw()
	end
end
With this code you can then in your update function call fun stuff like:

Code: Select all

function love.update(dt)
	if SOME TRUE OR FALSE STATEMENT HERE then
		actors["chris"]:speak("Omg it's TRUE")
	end
end
But maybe all of this is already out of the scope of the original idea :awesome: *shrug*
User avatar
LeNitrous
Prole
Posts: 29
Joined: Tue Sep 08, 2015 3:25 am

Re: How to draw all objects on screen?

Post by LeNitrous »

Thanks alot for the help! Just a follow up question, since I use cargo to handle my assets, is there anyway to handle something like this?

Code: Select all

-- inside actor.lua
actor = class{
	init = function(self, name)
		self.name	= name
		self.image	= assets.actors. <name> .idle
	end
-- <name> = self.name
}
Since my files are scructured to be "assets/actors/<name>". Thanks.
User avatar
Smoggert
Prole
Posts: 29
Joined: Thu Nov 10, 2016 11:23 pm

Re: How to draw all objects on screen?

Post by Smoggert »

LeNitrous wrote:Thanks alot for the help! Just a follow up question, since I use cargo to handle my assets, is there anyway to handle something like this?

Since my files are scructured to be "assets/actors/<name>". Thanks.
Since objects in lua are essentially fancy tables. writings assets.actors.whatever.idle is the same as writing assets["actors"]["whatever"]["idle"]

So you should be able to write:

Code: Select all

-- inside actor.lua
actor = class{
	init = function(self, name)
		self.name	= name
		self.image	= assets.actors[self.name].idle
	end
-- <name> = self.name
}
You could also make sure you actually loaded this asset by doing:
(This is not necesary but will make debugging easier when the game crashes because you forgot to add an image to your asset table.)

Code: Select all

-- inside actor.lua
actor = class{
	init = function(self, name)
		self.name	= name
		self.image	= assert(assets.actors[self.name].idle, ("Unable to acquire image for actor %s, in state idle"):format(self.name))
	end
-- <name> = self.name
}
Post Reply

Who is online

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