Page 1 of 1

Question about changing sprites

Posted: Thu Sep 28, 2017 8:03 am
by emanevitaerc
Sorry, this question really isn't LOVE specific, but it pertains to game development potentially in LOVE. So, keeping track of graphical state is annoying. It can lead to unintended behavior whose origins are obnoxious to track down, and it couples game logic with graphics. I had the (probably not original) idea to resolve these problems by having a sort of "stateless" test in the update function of each drawn entity, like so:

Code: Select all

if self.isStanding then
    self.sprite = sprite.standing
elseif self.isWalking then
    self.sprite = sprite.walking
    self.sprite:update(dt) --probably an easy way to have this not trigger on the first frame you play, but that's not important for this example
elseif self.isDucking then
    self.sprite = sprite.ducking
end
And so on, and you could have nested scenarios as well.

My question is, is there anything significantly wrong with doing it this way? I feel like it's fairly obvious, but I've never seen anyone use it. It's obviously not as efficient as just setting it one time, coupled with the player's behavior, but does testing a few conditions every frame really affect performance that much, in practice? I'm pretty clueless when it comes to performance, so I'd appreciate any feedback.

Re: Question about changing sprites

Posted: Thu Sep 28, 2017 8:50 am
by sphyrth
Nothing really wrong since it works for you. The only significant disadvantage I can say is that you'll have to do a lot of re-writing if ever you plan on adding and/or removing states.

Re: Question about changing sprites

Posted: Fri Sep 29, 2017 12:34 am
by emanevitaerc
Thanks for the reply, I'll keep that in mind :awesome:

Re: Question about changing sprites

Posted: Fri Sep 29, 2017 1:18 am
by alloyed
I actually go one step further, I don't even keep sprites in objects: instead I have a sprite/animation manager that I pull from, and that has its own internal state to do, for example, switching between frames over time. then my draw methods look something like

Code: Select all

function player:draw()
    assert(Animations:get("player", self.spriteState)):draw(...)
end