[SOLVED] Is there a smart way to getInstance("type" + "name")?


(Bora Kasap) #1

Is there a smart way to get instance of a type by name in flashpunk to make better performance than getInstance(“name”)?


(rostok) #2

getInstance() is implemented with Dictionary that is pretty fast, why would you want it to be faster?


(Ultima2876) #3

As a general rule, you shouldn’t start worrying about optimising performance until you’re having performance problems. You’d be surprised just how overblown ‘performance issues’ really are with stuff like this, and how little of a difference hours of working out a ‘faster’ solution can make!


(Jacob Albano) #4

Just like Ultima2876 said…unless you’re making many calls to it per frame, getInstance() is really not a function you should be worrying about.

You’d really be surprised at what you can get away with these days, even in a comparatively slow language like as3. My games involve a lot of message passing, which entails a ton of string comparisons, iteration through every entity in the world, and arrays being created and discarded as I use vararg functions. I’ve yet to notice any slowdown because of it.


(Bora Kasap) #5

Yeah, i got it now, actually i’m learning the basics of programming, like i shouldn’t use this kind of functions inside an update like continuous function, thats unnecessary, a complete fault.


(Jacob Albano) #6

Honestly, it’s not that big of a deal. Doing getInstance() is almost always faster than any string comparison search that a collision function would use under the hood, since only entities with the name property set will be added to the dictionary of named instances.

I remember being in your position and trying to optimize in every possible place. Don’t. It’s a waste of your time and it’ll get in the way of actually making a game Flashpunk uses a lot of crazy Flash tricks to keep performance high, and you really don’t need to worry about micromanaging these small issues.

Remember that every frame, for a game running at 800*600, Flashpunk is rendering a bare minimum of 480,000 pixels – and that’s just when no other entities being displayed. If you have a screen full of graphics, those 480K pixels might be overwritten many times over, and even then it takes a lot to cause performance to drop. Computers are crazy fast. Flashpunk is a well designed library. Don’t worry about performance until you start having problems.