It is important to understand the technical constraints of the platform(s) where your game is supposed to run and to optimize your game for the platform(s) while developing your game. There are several aspects to consider:
Defold will create a dependency tree when building and bundling your application. The build system will start from the bootstrap collection specified in the game.project file and inspect every referenced collection, game object and component to build a list of the assets that are in use. It is only these assets that will get included in the final application bundle. Anything not directly referenced will get excluded. While it is good to know that unused assets will not be included you as a developer still needs to consider what goes into the final application and the size of the individual assets and the total size of the application bundle. Some target platforms and distribution channels have limitations on application size:
According to a 2017 study it was shown that “For every 6 MB increase to an APK’s size, we see a decrease in the install conversion rate of 1%.” (source)
To get a better understanding of what makes up the size of your application you can generate a build report when bundling. It is quite common that sounds and graphics is what takes up the bulk of the size of any game.
You can use an application manifest file to remove engine components that you do not need. You can for instance remove the included physics engines if your game doesn’t use physics.
Defold supports .ogg and .wav files where .ogg is typically used for music and .wav for sound effects. Sounds must be 16-bit with a sampling rate of 44100 so any optimizations must be done on the sounds before encoding them. You can edit the sounds in an external sound editor software to reduce the quality or convert from .wav to .ogg.
You have several options when it comes to optimizing the graphics used by your game but the first thing to do is to check the size of the graphics that gets added to an atlas or used as a tilesource. You should never use a larger size on the graphics than is actually needed in your game. Importing large images and scaling them down to the appropriate size is a waste of texture memory and should be avoided. Start by adjusting the size of the images using external image editing software to the actual size needed in your game. For things such as background images it might also be ok to use a small image and scale it up to the desired size. Once you have the images down to the correct size and added to atlases or used in tilesources you also need to consider the size of the atlases themselves. The maximum atlas size that can be used varies between platforms and graphics hardware.
This forum posts suggests several tips on how to resize multiple images using scripts or third party software.
If an atlas is too large you need to either split it into several smaller atlases or scale the entire atlas using a texture profile. The texture profile system in Defold allows you to not only scale entire atlases but also to apply compression algorithms to reduce the size of the atlas on disk. You can read more about texture profiles in the manual.
You can read more about how to optimize and manage textures in this forum post.
Another way of reducing initial application size is to exclude parts of the game content from the application bundle and make this content downloadable on demand. Excluded content can be anything from entire levels to unlockable characters, skins, weapons or vehicles. Defold provides a system called Live Update for excluding content for download on demand. Learn more in the Live Update manual.
Android builds must support both 32-bit and 64-bit CPU architectures. When you bundle for Android you can specify which CPU architectures to include:
Google Play has support for multiple APKs per release of a game, which means that you can reduce the application size by generating two APKs, one per CPU architecture, and uploading both to Google Play.
Before trying to optimize a game with the goal to increase the speed at which the game runs you need to know where your bottlenecks are. What is actually taking up most of the time in a frame of your game? Is it the rendering? Is it your game logic? Is it the scene graph? To figure this out it is recommended to use the built-in profiling tools. Use the on-screen or web profiler to sample the performance of your game and then make a decision if and what to optimize. Once you have a better understanding of what takes time you can start addressing the problems.
Reducing script execution time is needed if the profiler shows high values for the
Script scope. As a general rule of thumb you should of course try to run as little code as possible every frame. Running a lot of code in
on_input() every frame is likely to have an impact on your game’s performance, especially on low end devices. Some guidelines are:
Don’t poll for changes if you can get a callback. Don’t manually animate something or perform a task that can be handed over to the engine (eg go.animate vs manually animating something).
If you create loads of short lived objects such as Lua tables every frame this will eventually trigger the garbage collector of Lua. When this happens it can manifest itself as small hitches/spikes in frame time. Re-use tables where you can and really try to avoid creating Lua tables inside loops and similar constructs if possible.
If you do a lot of message handling or have many input events to deal with it is recommended to pre-hash the strings. Consider this piece of code:
function on_message(self, message_id, message, sender) if message_id == hash("message1") then msg.post(sender, hash("message3")) elseif message_id == hash("message2") then msg.post(sender, hash("message4")) end end
In the above scenario the hashed string would be recreated every time a message is received. This can be improved by creating the hashed strings once and use the hashed versions when handling messages:
local MESSAGE1 = hash("message1") local MESSAGE2 = hash("message2") local MESSAGE3 = hash("message3") local MESSAGE4 = hash("message4") function on_message(self, message_id, message, sender) if message_id == MESSAGE1 then msg.post(sender, MESSAGE3) elseif message_id == MESSAGE2 then msg.post(sender, MESSAGE4) end end
Message passing or in other ways addressing a game object or component can be done both by providing an id as a string or hash or as a URL. If a string or hash is used it will internally be translated into a URL. It is therefore recommended to cache URLs that are used often, to get the best possible performance out of the system. Consider the following:
local pos = go.get_position("enemy") local pos = go.get_position(hash("enemy")) local pos = go.get_position(msg.url("enemy")) -- do something with pos
In all three cases the position of a game object with id
enemy would be retrieved. In the first and second case the id (string or hash) would be converted into a URL before being used. This tells us that it’s better to cache URLs and use the cached version for the best possible performance:
function init(self) self.enemy_url = msg.url("enemy") end function update(self, dt) local pos = go.get_position(self.enemy_url) -- do something with pos end
Reducing the time it takes to render a frame is needed if the profiler shows high values in the
Render Script scopes. There are several things to consider when trying to increase reduce the time it takes to render a frame:
builtins/materials) and reduce shader precision to gain some speed on low end devices. All shaders are using
highpprecision and a change to for instance
mediumpcan in some cases improve performance slightly.
Reducing the scene graph complexity is needed if the profiler shows high values in the
GameObject scope and more specifically for the
UpdateTransform sample. Some actions to take:
enablemessage to each game object.
This section is not yet finished. Topics that will be covered:
This section is not yet finished. Topics that will be covered: