Page 2 of 2

Re: How to draw all objects on screen?

Posted: Tue Nov 29, 2016 1:08 am
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*

Re: How to draw all objects on screen?

Posted: Tue Nov 29, 2016 5:08 am
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.

Re: How to draw all objects on screen?

Posted: Tue Nov 29, 2016 12:46 pm
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
}