Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a global CachedGraphics persistence variable. #1241

Merged
merged 2 commits into from
Jul 25, 2014
Merged

Added a global CachedGraphics persistence variable. #1241

merged 2 commits into from
Jul 25, 2014

Conversation

LaughingLeader
Copy link

Summary

If specified, CachedGraphics will use the Persist:Null<Bool> value supplied by the
constructor. Otherwise, it defaults to the value of
defaultPersist in CachedGraphics.

This allows globally setting what the persist value of CachedGraphics
should default to, making it easier to keep desired graphics persistent between state
changes.

Changes

  1. Persist in CachedGraphics's constructor becomes a Null<Bool> variable, so that if Persist is null, persist in the CachedGraphics is set to the default value (the value of CachedGraphics.defaultPersist).
  2. CachedGraphics gets a simple defaultPersist:Bool so that persistence with all created CachedGraphics can be globally toggled.

Reasoning

  1. Simply put, if one wishes to keep certain graphics persistent between state changes (FlxSprite, FlxText and so on), they have to manually adjust the cachedGraphics.persist variable for each object. This can be made easier with a global variable.
  2. Rather than risk missing some objects, it's considerably easier to just set CachedGraphics.defaultPersist = true, create the objects that you wish to be persistent, and then CachedGraphics.defaultPersist = false.
  3. Persistence is handy for objects that will be used in Menu and Play states of a game. My own project has a Leaderboard menu that generates quite a bit of names and entries. Only having to create these objects once is a significant advantage, since this menu is accessible from most areas of the game. Having an easy way to control the persistence of objects as a whole is extremely handy for this.

Update

Thanks to the feedback from gamedevsam and Gama11, I believe this static variable is clearer with its naming convention and position within CachedGraphics. Thanks guys.

If specified, CachedGraphics will use the persist value supplied by the
constructor. Otherwise, it defaults to the value of
persistentCachedGraphics in FlxG.bitmap (BitmapFrontEnd).

This allows globally setting what the persist value of CachedGraphics
should default to,
making it easier to keep desired graphics persistent between state
changes.
{
key = Key;
bitmap = Bitmap;
persist = Persist;
persist = Persist != null ? Persist : FlxG.bitmap.persistentCachedGraphics;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of assigning this at construction time, why not leave value to null by default, and do this check during destruction time (when it's actually used)? As it stands, if you create some sprites, and then set FlxG.bitmap.persistentCachedGraphics to true after, it won't do anything since the value has been already set in each sprite.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with that is you're essentially making the persist state all or nothing. The value in assigning it at creation time is you can designate specific graphics to be persistent.

For instance, I would want a reoccurring menu to be persistent, if it's a menu that is accessible from both my main menu and my game (options screens, help screens, etc.).

Yet I don't need all the objects in my game to persist within my main menu, and so on.

The advantage of having a value be used globally at construction time is this - you can switch the default persist state to what you desire, before you create your menus. Then once they're created, you can switch it back off.

It makes things a little easier if you can have things default to what you want beforehand - that way you don't have to manually make sure each and every element of your persistent menu has persist set to true.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plus, I'm not entirely sure what the value would be in having absolutely everything persistent or not. Not on the global scale of things, anyway.

Maybe a renaming of the variable would make it more clear on what it does? Something like defaultPersitence? defaultCachedGraphicsPersistence?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would definitely prefer a static CachedGraphics.defaultPersist, because that follows the pattern we already have for FlxCamera.defaultZoom and FlxCamera.defaultCameras.

Gama11 added a commit that referenced this pull request Jul 25, 2014
Added a global CachedGraphics persistence variable.
@Gama11 Gama11 merged commit 5d0d892 into HaxeFlixel:dev Jul 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants