Page 1 of 1
game resource management?
Posted: Fri Sep 16, 2016 1:00 am
by sunsflower
hi, I'm having a problem of manage resource in lua...for example, In a game there must be many many textures and many, say sprites, that draw those textures or part of them. Problem is, the reference of these textures are scattered all over the code by using a simple table value storing them in sprite(like a class), so that when I want unload some textures(or other user-defined resources), they may not be garbage collected because god knows where there is a reference that is still valid...
so instead of this, I can use resource ids(string or number) to index resources in a big pool thing. But this requires a table(maybe a large one) access for so many sprites every frame. Will it do damage to performance?
thanks!
Re: game resource management?
Posted: Fri Sep 16, 2016 2:15 am
by raidho36
Every operation impacts performance. But "sprites[ obj.sprite ]" and "sprites[ obj.spriteId ]" are not at all different. If you however put sprite reference into each individual object, then that's one extra table lookup operation per each sprite rendering operation.
One important thing about getting code to run fast is making it perfectly predictable, so that when you do something you can just assume that everything is already lined up neatly and nothing will break. This way you don't need to check any inputs, and you don't need to check any outputs. Values sanitizing can very seriously hurt performance, especially for trivial functions, so ideally, if you can code your program in a way that you don't need to check anything because it's already 100% certain that the check will never fail, you drop the check altogether. So for instance when you delete a sprite, it's when you already know that no object within the scene uses it anymore, so that when you actually do this nothing is damaged - as opposed to checking every time if sprite is still used somewhere so that you don't delete it by accident and cause crash.
Of course the last point doesn't applies to LÖVE because if it's used anywhere it will stay, and because you can't "delete" it anyway, it has to be garbage collected. But idea is the same still. Which might be your answer - just clear your "system" reference of the sprite, and when it's no longer actually used by in-game objects, it'll get deleted from memory by itself (garbage collector will do it).
Re: game resource management?
Posted: Sat Sep 17, 2016 12:13 am
by sunsflower
thanks much! I've reduced almost all type checks(which caused a lot a trouble at first though..)and the code is clean again
The resource id may be more difficult to achieve because of the "scattered reference" everywhere but I've used weak tables to store those reference so that they won't hurt