HaxeFlixel / HaxeFlixel/flixel
Delay destruction of graphics by a small amount of time
Nobody has claimed this yet.
- Dominant language
- Haxe
- Stars
- 2.2k
- Forks
- 522
- Avg merge
- 34m
- Merged PRs (30d)
- 1
Description
I thought of this a while back but never wrote it down; it would be nice if decrementing the useCount of a FlxGraphic didn't immediately destroy it. graphic.persists prevents this, but I think there are obvious cases where a slight deferral is preferred. Examples:
- Here: https://github.com/HaxeFlixel/flixel/issues/3268
- When switching to a new instance of the current state, if some of the previous instance's assets are all immediately loaded again, it would be nice if it simply did not remove them on destruction.
I imagine that in most cases we could simply wait 1 frame.
Possible Solutions
- in FlxGraphic.checkUseCount we could do:
function checkUseCountDeferred()
{
if (canDestroyUnused())
{
var frameCount = 0;
FlxG.signals.postUpdate.add(function onUpdate()
{
if (++frameCount > 2)
{
FlxG.signals.postUpdate.remove(onUpdate);
checkUseCount();
}
});
}
}
function checkUseCount()
{
if (canDestroyUnused())
FlxG.bitmap.remove(this);
}
function canDestroyUnused()
{
return useCount <= 0 && destroyOnNoUse && !persist;
}
- Deprecate destroyOnNoUse and add:
enum FlxGraphicAutoDestroy
{
NEVER;
STATE_SWITCH;
NO_USES;
NO_USES_DEFERRED(numFrames:Int);
// Or perhaps just: NO_USES(?numFrames:Int), but I hate Null<Int> here
}
There may be other types of deferral in the future so perhaps FlxGraphicAutoDestroy.NO_USES(?deferral:FlxDestroyDeferral) with deferral options IMMEDIATE (or null) or FRAMES(numFrames:Int)
Impact on existing code
If we changed the default behavior of FlxGraphics to defer destroy, games that edited bitmaps, and expected immediate destruction on no use, will have their edits unexpectedly maintained. No impacts if the default behavior remains "immediate".
FlxGraphicsResource
Very tangential, and I only have a rough idea, but It might also be a good idea to further separate FlxGraphic from the cached resource. Where FlxGraphics have a reference to a globally managed resource or a unique, locally managed copy of it. This might also help with https://github.com/HaxeFlixel/flixel/issues/3540
Global Default
As outlined in #3411: a way to set the default auto destroy via FlxG.assets.defaultCachingMode or FlxG.assets.defaultAutoDestroy
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with flixel/graphics/FlxGraphic.hx, especially checkUseCount, and inspect FlxG.signals.postUpdate and FlxG.bitmap.remove. Review the related issues #3268, #3411, and #3540 to clarify the intended deferral and default-caching behavior. Done requires an agreed design that preserves immediate-destruction compatibility while specifying deferred destruction and its scope.
Written by the indexing model from the issue text.
Assessment
- Domain
- game-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100