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

Dead asset elimination? #1768

Closed
IBwWG opened this issue Mar 8, 2016 · 8 comments
Closed

Dead asset elimination? #1768

IBwWG opened this issue Mar 8, 2016 · 8 comments

Comments

@IBwWG
Copy link
Contributor

IBwWG commented Mar 8, 2016

  • Flixel version: dev
  • OpenFL version: 3.6.0
  • Lime version: 2.9.0
  • Affected targets: windows release

Code snippet reproducing the issue:

package;

import flixel.FlxSprite;
import flixel.FlxState;

class PlayState extends FlxState
{
    override public function create():Void
    {
        var sprite = new FlxSprite().loadGraphic(Reflect.getProperty(AssetPaths, "dolphin__png"));
        add(sprite);
    }
}

Using this dolphin.png


Observed behavior: Sprite placeholder appears instead of dolphin.png image, despite the fact that dolphin.png was correctly copied into the bin/assets/images/ folder (clean build.)
Expected behavior: That the requested image would appear, like it does in flash debug/release targets.

I tried to make a workaround, thinking if I loaded all my assets the normal way, then those that I accessed with Reflect would be available afterwards. Not so. The following code:

var spriteWorkaround = new FlxSprite().loadGraphic(AssetPaths.dolphin__png);
add(spriteWorkaround);
var sprite = new FlxSprite().loadGraphic(Reflect.getProperty(AssetPaths, "dolphin__png"));
add(sprite);

...correctly loads dolphin.png the first time, but the second time gives a placeholder still.

@Gama11
Copy link
Member

Gama11 commented Mar 8, 2016

Why use AssetPaths at all here? Would be much nicer to just use the String path, instead of Reflection (which should be avoided).

@IBwWG
Copy link
Contributor Author

IBwWG commented Mar 8, 2016

Only because I thought that was The Way It Is Done In HaxeFlixel. If I may pass the question back to you, why use AssetPaths normally, and why not use it here? (And why should Reflection be avoided? It seems to have so many handy DRY uses.)

Also, none of the following worked (all just gave a placeholder):

var sprite = new FlxSprite().loadGraphic("dolphin__png");
var sprite = new FlxSprite().loadGraphic("images/dolphin__png");
var sprite = new FlxSprite().loadGraphic("assets/images/dolphin__png");

...oops. Of course, the filename has a dot, not two underscores, so this works:

var sprite = new FlxSprite().loadGraphic("assets/images/dolphin.png");

@larsiusprime
Copy link
Member

Reflection has two issues I know of:

  1. It's slow, especially if you use it in core loops
  2. Dead Code Elimination can't account for it

@Gama11
Copy link
Member

Gama11 commented Mar 8, 2016

AssetPaths just creates variables containing the String paths to assets. This provides

  • auto-comletion in IDEs with Haxe compiler completion
  • a certain degree of type-safety - if an asset is renamed / moved / deleted, the variable name will change, resulting in a compile-time error (normally this would be a runtime error with String paths)

So, using Reflection on AssetPaths... defeats the whole point of AssetPaths. Reflection leads to runtime errors and is not type-safe.

@larsiusprime
Copy link
Member

If you're using loadGraphic function with OpenFL's regular Assets class, I think you just give the actual asset name, so just "assets/images/dolphin.png" should do the trick.

@IBwWG
Copy link
Contributor Author

IBwWG commented Mar 8, 2016

I see. Thanks both of you for your help, I understand much better now.

@IBwWG IBwWG closed this as completed Mar 8, 2016
@larsiusprime
Copy link
Member

Good luck!

@IBwWG
Copy link
Contributor Author

IBwWG commented Mar 10, 2016

Thanks :)

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

No branches or pull requests

3 participants