Hi, it's the first time I am in this situation and I am not sure if my approach is the right one:
I have some guys roaming across the stage, who are being targeted by turrets as in any tower defence game.
A bit of overall lag when they're many would be acceptable, but at some point I lose control of their "status" (like burn, or freeze).
I suppose due to the lag because when they are only a few it's not an issue.
I tried keeping everything simple:
almost everything is handled by a single enterframe function in the level file, that sets a "tick" each second (frame based) and changes values.
Later in the same function those values are read, and with a bunch of conditions based on these values things happen.
I am now playing with profiler to lighten the heaviest functions, but I think I used all the tricks I know: from localizing anything to loading the fewest texturepacks.
I still see some potential by pooling status sprites (everything else is already in a pool) but I am not sure it would be enough.
To make a practical example: every actor has a property to define its status, which is a table made by 2 entries: the first one is a counter that starts from 0 and is increased on every new "tick", the second one is the limit it should reach before annihilation (this is also handled by the same function that increase it)
Pseudocode goes like this:
function onTick()
--increase status time
if actor.status[1]<=actor.status[2] then
actor.status[1] = actor.status[1]+1
else
actor.status = nil
end
--remove graphics relative to status
if actor.status == nil then
actor.statusSprite:removeFromParent()
actor.statusSprite = nil
else
--apply status damage
actor.hp = actor.hp -1
end
-- ... and so on for almost anything
end</pre> |
What I don't understand is how is it possible that some lines of code are executed, while others seems to be skipped: I mean, even if I lose "tick number 34", shouldn't this thing be executed on "tick number 35" and following "ticks"?
So why there is a random condition when I still see flames on a guy who doesn't get any damage from them and they won't ever disappear?
I may avoid adding flames as a new movieClip, but if I have to draw any guy in flames in 3 frames I would exponentially increase the number of textures I need to load...
Thank you
Comments
Fragmenter - animated loop machine and IKONOMIKON - the memory game
https://deluxepixel.com
https://deluxepixel.com
If you didn't, then it's definitely worth trying.
Simple operations like +- don't make great difference. But for math.pow/math.sqrt/etc mutation operators are many times faster.
Also I've seen somewhere (maybe in 'Programming in Lua' book), it was said that perfomance is different when we use short lua ternary operators instead of if/else blocks.
(but I'm not sure if I remember it correctly, and I don't remember what was faster, now I'm also curious about it)
"What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
“The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
I am planning to switch to sinistersoft math operators, but I am not using too many math operators there.
I will try with zerobrane's watch panel to check if the conditions are met or not / what happens there
@hgy29 would it be the same even if the onTick function is local inside the enterframe function? I mean
[edit: sorry confirmed message too early by mistake]
Fragmenter - animated loop machine and IKONOMIKON - the memory game
- The garbage collector will be going crazy.
- There is a lot of overhead creating new sprites.
The best approach would be to create a bunch of actors and reuse them as required. With the status sprite (and possibly others you might have), just have them attached to the actor all the time and make them not visible when not required.
With MovieClips.. dump them. For a single MovieClip with 32 frames of animation (unique graphics) you are creating 32 bitmap objects! It might be a little harder to code yourself but use ONE bitmap object and then fetch TextureRegions from a TexturePack and setTextureRegion() on the bitmap object.
Also think about things that don't need to be processed every frame too. If an actor is always aiming at the player.. could they update their direction every second frame without it not looking or performing bad?
Likes: Apollo14
Everything is already pooled and reused, except the status sprites because they were part of the actors (and they will be there again because I liked it more).
You sound right when you say to drop movieclips: maybe I am skilled enough to code an alternative, but it will take time and I was trying to focus on the game.
If I don't catch a bug somewhere, I suppose it's time to think at this option too..
Thank you
Likes: Apollo14
https://deluxepixel.com
Fragmenter - animated loop machine and IKONOMIKON - the memory game
How do you handle timings?
Thank you
Here's very basic example (maybe there're better ways):
*and definitely it would be better to not have dedicated rooster:run() function for every rooster instance. We should manage all instances with array somehow. Maybe somebody from Lua Gurus will suggest elegant solution
Likes: hgy29, pie
"What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
“The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
with movieclip I have ~50fps when I spawn 300 roosters
while with asynccalls I can afford to spawn 570 roosters and still have same ~50fps
I didn't even optimize code, there's dedicated rooster:run() function for every instance, sort of lame practice
After optimization asynccall animation will be 2x faster or even more.
Likes: pie
"What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
“The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
Likes: pie, SinisterSoft, Apollo14
Likes: Apollo14, antix
https://deluxepixel.com
Likes: antix
https://deluxepixel.com
Likes: Apollo14, SinisterSoft, MoKaLux