[ Android ] Game running extremely slow

Hello Defold community!

I’m a complete newbie, so please I’m sorry if I’m being too stupid :confused:

I’m creating a small flappy bird clone (wow, how original…) just to learn how to use the engine, but after building it and running on a Moto G2, I see the game is running super slowly.

It’s ok on Desktop, but I can’t figure out what’s wrong.

Thank you in advance, and again, sorry if it’s a stupid question.

1 Like

Awesome of you to give the engine a try!

Are you doing any animation yourself in the scripts update function? In that case make sure to use the delta time, dt, argument instead of a fixed step.

So instead of:

function update(self, dt)
    self.anim_value = self.anim_value + 1.0 
    ...
end

you would use dt instead:

function update(self, dt)
    self.anim_value = self.anim_value + dt
    ...
end

If you feel that the performance is the problem on the other hand, keep the calculations to a minimum inside the update functions since these are run each frame. Maybe try to rely on the built in gameobject/GUI animation system instead in cases where possible.

Let us know how it goes! :smile:

3 Likes

Welcome to Defold!

There are several things that may cause this. See Sven’s post for some tips. Another thing to watch out for is too many messages sent each frame. Mobile devices can easily choke on that. Some print() statements in the code can help see if that’s the problem.

Also, many layers of graphics can bee too much for the mobile’s graphics card to handle too.

Hope you find the issue, otherwise just drop another line and we’ll help you do some profiling.

Cheers,

Mikael

3 Likes

You’ve already received some nice tips from Sven and Mikael, but if you’re still not able to figure it out you might try:

  • Use the visual or html profiler to figure out where the bottleneck is (scroll down on this page: http://www.defold.com/doc/debugging). Are you constantly above the frame budget? Is it the number of draw calls? Is it a script that takes too long?
  • Have you modified the render script or any of the shaders?
  • If you’re having problems with many draw calls you should consider reducing the number of atlases (combine them). If you have a lot of gui scenes you should consider using layers to optimise draw calls (see gui documentation).
  • Have you tried the game on another mobile device? If so, is it equally slow or does the game run at the expected speed? If there’s a noticeable difference it could be an issue with the GPU and shaders if you have customised them.
4 Likes

First, Thank you all, I’m really amazed on how responsive and cheerful you guys are :scream:.

Now, this is the code I’m running:

local gravity = -0.03
local tapped = false

local max_bird_angle_left  = vmath.quat_rotation_z((math.pi) / 4)	
local max_bird_angle_right = vmath.quat_rotation_z(-math.pi / 2) 

function init(self)

    msg.post(".", "acquire_input_focus")

    self.velocity = vmath.vector3(0, 0, 0)

    self.angle          = vmath.quat()
    self.rotation_angle = vmath.quat()
    self.bird_direction = vmath.quat()

    self.rotation_speed = 0.001

end

function update(self, dt)

    local pos = go.get_position()

    pos = pos + self.velocity

    self.velocity.y = self.velocity.y + gravity * dt

    self.rotation_angle = vmath.slerp(self.rotation_speed, self.rotation_angle, max_bird_angle_right)

    go.set_position(pos)
    go.set_rotation(self.rotation_angle)

end

function on_message(self, message_id, message, sender)
    if(message_id == hash("tap") and tapped == false) then	
	self.velocity.y = 0.24
	self.rotation_angle = vmath.quat_rotation_z(45 * math.pi / 180)
	tapped = true
    else 
	if(self.velocity.y < -0.02) then 
   	 	self.velocity.y = -0.02; 
   	 	tapped = false
   	end
    end
end

function on_input(self, action_id, action)

    if action_id == hash("tap") then
	msg.post('#', 'tap')
    end
end

( Sorry for the hacky tap thing, I still don’t know how to handle taps! )

I followed the car game tutorial but also wanted to make something different.


The project is organized in folders. Is that bad?

  • main
    • audio
      • sfx
      • bgm
    • collections
      • bird.collection
      • background.collection
    • scripts
      • bird.script
    • sprites
      • game_sprites.atlas

My project settings at the moment are:

graphics

default_texture_min_filter: linear
default_texture_mag_filter: nearest

max_debug_vertices: 10000
texture_profiles: /builtins/graphics/default.texture_profiles


On the desktop I notice that when I scale up the window, to something like 800x600 the game starts getting slow.

Do you have any clue on what’s going on?

TIA!

~ mobs

1 Like

Nothing in your setup sticks out what I can see. Can you try to run the web profiler (see Björn’s post above for a link to how) and post a screenshot on what it captures?

And no worries, folders are a good idea. It’s one of the goals with the editor that you should be able to organize and easily re-organize your files as you wish.

2 Likes

Can’t see anything that should be bad for performance in that code. But one thing I just realised you could try, if the game is “slow”, is that you might want to try to enable variable_dt in game.project.

1 Like

Ok, here’s what I got from the profiler:

After enabling the variable_dt in game.project I noticed the game runs a little bit faster when I scale the screen up. It’s like if I had to make my calculations based on the screen resolution, I don’t know.

Thanks! The web based profiler gives better information but from what your screen it might be too much graphics to render.

Do you have lots of background graphics? Big textures in many layers?

Would you mind trying the web profiler as well?

Well, I have 2 sprites in the background.collection (for the background of course), each of them 144x256 .png images.

For the bird, I have 3 small 17x16 (pretty irregular) PNG sprites.
One animation ( with these 3 frames) in the game_sprite.atlas.

No at all!

( Beginners can’t post multiple images, so I’m dividing it in multiple posts )

Thanks a lot! If you click one of those large red frames at the top, you will get details on what happens there.

The red ones are frames where the game stalls. If you get a lot of those you may see a slowdown, or experience hitches.

It seems like your graphics is okay, but just to be safe. Try to remove all backgrounds and see if the game speeds up.

1 Like

In general you seem to have quite a good frame time anyway (<4ms at the selected frame), the spikes of 18ms is still almost 55 FPS. I don’t see any performance problem really, could it just be that some of your animations don’t depend on the frame delta?

1 Like

I rebuilt the project and clicked in the red bar, this is what I got:

After maximizing the window and running the profiler I got this:

As I said, when the window size is bigger, the game gets really slow. It’s worth mentioning that when it comes to Moto G the screen is smaller but the dpi is still high: 720 x 1280 pixels (~294 ppi).

It’s weird…

The VSync.wait in this snapshot means that the video card essentially is waiting for the previous frame to finish to avoid screen tearing, so not really a performance issue. As I said in my previous reply, I would suspect the game “feels slow” due to some inconsistencies with not using delta time when updating the animations. Are you updating animations/positions/rotations anywhere else?

1 Like

No, I’m not. If you take a look a line 26:

self.velocity.y = self.velocity.y + gravity * dt

I’m using the delta time to update self.velocity.y.

It works well on windows, with a 320x480 window, but when I scale the window up, or deploy to Android, the game runs slow.

Is there a way to include you in the list of developers, or make the project available so that you can mess around to find out what is happening?

I’d be happy to solve this. But anyways, you all have helped me a lot! :smile:

I made a little video to illustrate this…

The video recorder slows down the game a little, but you still can notice at 0:31 seconds when I change the resolution, the game starts slowing down a lot.

I really want to use this engine… I don’t know if I’m doing something wrong in the project configuration. I’d appreciate if any of you who already made any android game using Defold could make a simple tutorial on how to setup a project for android, with configurations and optimizations, because I think I have done something wrong, but I can’t figure out what.

Thanks again!