Hints on improving performance


(Mutlu) #1

Hello flashpunk community!!! i ve been using FP and following the forums for sometime this is the first time i am posting anything. I am making a game using FP that takes place on huge Worlds. For example 10000*1000 pixels. not all of that is visible on the screen i use dynamic camera with zoom in/out(by adjusting FP.screen.scale). I am having some performance issues, in which my fixed frame rate(thats 24fps) falls to as low as 5 fps. I use a little code like this,

	override public function render():void
	{
		if (onCamera)
		{
			super.render();
		}
	}

on graphic entities and the overall performance seems to be OK for now. Any other tricks and suggestions??? And also does anybody have an idea what’s the optimal memory usage for a browser swf?

note: the whole 10K1k map is full with entities smallest being 2020…


(Bora Kasap) #2

i’m not sure this thing works, because i’ve heard something about FP doesn’t render entities not on the camera, so that makes sense… also i understood right now why FP using diferent two functions for update and render stuff…

so, i think it is better you have to find a way to stop update functions for entities while they’re not on camera…

also, what are you using for standart tiles, i mean ground tiles, are you using tileset full of entities? or are you using background image? even you use tiles for ground, it’s better you use screenshot of your ground as an image file sized same with your game window size(window’s size +1 tile size for horizontal and vertical sliding for both x and y sizes) and slide it like a typewriter’s paper sliding while your hero moving around… then you can put your different tiles over your ground image to create an illusion.


(Mutlu) #3

i am using entities for 20*20 tiles. every slot in the grid is occupied by an entity. It has to be like that for sake of the game,(the entities(meaning the background) are responsive to the mouse location…)


(Mutlu) #4

by the way the above snippet seems to be working i am getting higher frame rates but i am not sure could be something else, do you think it doesnt make sense?


(Bora Kasap) #5

you may right of course, i said i’m not sure about that… so, i’ve remembered a topic about that, that may help you


(Mutlu) #6

Recycling helps a lot but i need to change the logic on how i build the maps, to bring back recycled entities. Is there any way to call them back with instance name or anything? world.create() asks for class type and i guess it doesn’t bring back my recycled entities…


(Bora Kasap) #7

which logic are you using to build the maps already?


(David Williams) #8

world.create() will bring back entities that have been removed using world.recycle()


(Alex Larioza) #9

The most important tip is to examine what is being executed every frame. If a function/calculation doesn’t need to be done every frame, its best to put it off only when needed. Caching values that don’t change (like velocity of an object that doesn’t change direction) is also very import. Here are some others:

Use vectors over arrays

Use multiplication over division. Use 10 * 0.5 rather than 10 / 5)

Use bitwise operators (I don’t like to do this because it often makes code harder to read)

Avoid useless iterations of loops. If you’re searching for something in a loop and find it, break from the loop no need to keep checking elements.

Use Flash Events. Helps with putting off executing code until it really needs to be executed.

Profile! Most bottlenecks are project specific, so you have to examine your program at run-time to see what’s causing slow downs.


(Willard Torres) #10

I’m guessing you mean 10 / 2, but yeah, my teacher mentioned that multiplication is less intensive than division.

Also, I read somewhere that it’s better to use the Draw class only sparingly, since apparently it uses up memory. Is this correct?


(Alex Larioza) #11

I’m guessing you mean 10 / 2, but yeah, my teacher mentioned that multiplication is less intensive than division.

Yeah…this is why I need coffee in the morning!

Also, I read somewhere that it’s better to use the Draw class only sparingly, since apparently it uses up memory. Is this correct?

Are you referring to FlashPunk’s Draw class? I’m not entirely sure but I also seem to remember the docs mentioning that the draw class was slow.


(Mutlu) #12

right now i am hand coding with for loops in the active world class for testing. the entities are constructed on the world begin function.


(Ssnyder Coder) #13

So, you say your map of 100001000 pixels is full of tile entities that are as small as 2020 pixels. If all those entities are loaded in the game at the same time, then that could be as much as 25,000 entities. That many entities might have an impact on your performance.

There may be a way for you keep most of the map saved to a file, loading only the parts of the map that your player is near. Perhaps you could do something similar with entities, loading only the ones near your player. This stack exchange question has a few examples of other games that did something like this.


(Mutlu) #14

Let’s see vectors over arrays…

Never ever used vectors on anything before.I guess i ll have to start reading as3 referance…

Although i worked before on some commercial projects as a game designer, I am very new at programming( it been only 3 month since i wrote my first “hello world” on ansi C).


(Jacob Albano) #15

The reason Draw usage is recommended against is because it uses the Flash vector drawing API, which is slower than the bitmap blitting that Flashpunk’s graphic system uses.


(Ultima2876) #16

Vectors are so easy to use it’s not even funny. They’re basically arrays where you specify the type of variable that will be contained in them:

private var _myVector: Vector.<Entity> = new Vector.<Entity>;

From there they basically work exactly the same as an array.

for(var i: int = 0; i < _myVector.length; i++)
{
  if(_myVector[i] != null)
  {
    _myVector[i].x = 50;
    _myVector[i].y = 50;
  }
}

_myVector.length = 0; //fast clear of the vector (same as with arrays)

As for recycling entities, destroying them as they go offscreen etc… it might be better to make them inactive and invisible if they’re offscreen. If you set myEntity.active = false and myEntity.visible = false, they will not be updated or drawn at all. They will still be contained within the entity list so there will be some overhead just for having them there, but it will be a very small amount of overhead compared to what you have currently.

If you really wanted to recycle and recreate them you could keep a Dictionary of entities in memory. Before you recycle an entity, update its entry in the Dictionary (keyed by a unique reference for each entity; we’ll call this entityId) with any properties that are important (eg x position, y position, state, whatever is relevant to your game). Then when you recreate an entity with World.create, check if it has an entry in that Dictionary and if it finds it, copy the data back. Some helpful code for this:

//Game class or wherever!

private var _entityList: Dictionary = new Dictionary;  //create a dictionary

_entityList[myEntity.entityId] = new GameEntity(myEntity);  //you probably want to use a custom base class for all your entities that has an entityId variable. Make the GameEntity constructor clone whatever is passed to it!

//GameEntity.as

//in your GameEntity class do something like this
private static var _currentId: uint = 0;
private var _entityId: uint;

//constructor
public function GameEntity(clone: GameEntity = null): void
{
  if(clone == null)
  {
    //we're not cloning an entity this time, this is a new entity. Give it a unique ID!
    _entityId = ++_currentId;  //increment static current ID so they're always unique
  }
  else
  {
    //copy all relevant variables here!
  }
}

//we want a getter for entityId!
public function get entityId(): uint
{
  return _entityId;
}