Big List of Defold Pro Tips!

If you are not using hot code reloading you absolutely should be. It can save you a ton of time when you are working on specific areas of your code without needing to rebuild over and over again. I changed my key map for hot code reload to F2.

3 Likes

Having different screens, loading different screens, switching between screens.

This post references this thread. As well as the official docs:

Think of collections as a way to represent a screen. Main menu, game, splash …

Then you have a central script which loads or unloads collections as you switch screens.

If you would like you could add a super light weight inbetween screen which acts as loading screen -
or have unique light weight loading screens depending on what kind of activities you are going to,
and some screens you go to you may already know they are super light weight and don’t need a loading screen inbetween.

Remember that some screens you may want to not be screens you switch to but ones which act as overlays.

  1. The parent object’s script of the collection proxies must ask for input if you want the child screens to be able to get input
  2. If you get the below error edit your .collection files in a text editor and change their name: from default - the file name is NOT this name

ERROR:GAMEOBJECT: The collection 'default' could not be created since there is already a socket with the same name.

Remember that even in the different screens you may be loading / unloading proxy collections as you need them for that screen.

Below is an example showing how to switch between screens - but as mentioned there are further improvements you can make.

ScreenSwitcher.zip (31.6 KB)

7 Likes

Note that you could use collection proxies at any level. Screens or levels are natural candidates though but you are not restricted to that. In theory you could dynamically load every single object that appears through a proxy—although that is not recommended.

3 Likes

Defining default parameter arguments for functions. This is one way to do it.


function say(msg)
  
  local msg = msg or "Hello" -- if msg is not defined it will be nil
  print(msg)
  
end

say() -- prints "Hello" because no parameter is passed

This style of defining variables is also useful if you have a script which loops that you want to initialize variables once to not be nil without resetting them after they have a truthy value.

local health = health or 0
6 Likes

Sending messages has a cost. Every time you send a message it involves copying and serializing a Lua table. Don’t be afraid to use the messaging system, but try to use it for the right things only. Collisions in systems with many objects for example require extra consideration and optimizations to not overload the message system.

Your on_message functions can also slow down the system depending on how complex that code is. If you are sending many messages it may be this area that needs to be optimized. Use the profiler to see.

6 Likes

Detecting input on edges / corners of the screen. This is very easy to do. You can detect half of the screen for some input and the other half for another. You can detect only the far edges of your screens. Are you using custom rendering? You will want to look at action.screen_x and action.screen_y.

This post by @britzl explains one method for doing this.

Reasons for using this input style: picture book, player movement for a platformer, driving game with steering…

2 Likes

Get current app version to display to your users.

sys.get_config("project.version")

There are many kinds of version number conventions. The way I prefer to do it with my games is 1.0.year.month.day.build so for example today’s would be 1.0.16.4.25 for the first build and 1.0.16.4.25.2 for the second build of the day. I’ve used other systems before but I like this one because you can see when the version was made live at a glance.

You can also use sys.get_config to get anything listed in the game.project file.

sys.get_config("project.title")
sys.get_config("display.width")
sys.get_config("physics.scale")

And so on.

5 Likes

If you are using Tiled to edit your Tilemaps and they’re big, you can quickly create the tilemap file in Defold like this:
Create a tilemap as usual and select the tile source. Select a tile and place it at the origin, then place another tile at a random place, such as 5,5. Save and close the tilemap.

Then in the Project Explorer, right click on the tilemap file and select open with Text Editor. You should see the 2 cell tables for the tiles you added. In the bottom cell set the x and y values of to be how many tiles high and wide your tilemap will be. If you have extra layers, add the values to that as well.

This should make your tilemap the right size for the import.
Well it worked for me :slight_smile:

1 Like

I’m not sure whether this feature flew under everyone’s radar or not, but we re-did the search mechanism a few weeks ago – enabling filtering of your search results. It’s also more liberal nowadays with displaying words inside of articles, etc. Don’t hesitate to PM me if you have any site feedback or find any bugs!

4 Likes

Do not worry this thread is not forever dead… after I finish the book I plan to continue posting more tips! I’ve been making many interesting discoveries and tricks that may not be immediately obvious and found some things which are usable but not documented at all!

4 Likes

Great. Please post any lacking documentation and we’ll fix that.

2 Likes

Demonstrating the various easing equations built in with how they work with go.animate

Working on this for the book. Run this example, and press up / down to go through the different easing equations. Then left click to animate the Defold logo to the new position. Ghosts of the Defold logo are created as it is repositioned by the animation to so the path clearly, and a white dot is left at the original position to more clearly show how some of the easing equations differ.

I plan to improve it, such as having a mode for the logo to move around randomly within the space after it completes each animation. Todo…

Web version: https://www.bookofdefold.com/examples/DefoldEasingEquations/

EasingEquations.zip (18.6 KB)

16 Likes

Nice! Thanks for sharing!

2 Likes

I just found this and it made my day - I’ve been trying to get the low-res font in Labyrinth to filter by nearest for a while and I put it on the back burner, now I can fix that!

2 Likes

f.lux will cause any Defold game (well, probably the entire screen) to stutter during the transition to “night shift”

2 Likes

Configuration settings retrieved with sys.get_config() are returned as string values; if you’re reading something that needs to be treated as a number (such as display height), use the Lua ‘tonumber’ function to convert the string into a number:

tonumber(sys.get_config("display.height"))
2 Likes

To get the size of a sprite that has been scaled, multiply the width/height by the sprite’s scale amount; for example:

local spriteWidth = go.get("#spriteAsteroid", "size.x") * go.get("#spriteAsteroid", "scale.x")
1 Like

There are a few issues with this snippet of code:

  1. You call it spriteWidth but you base it on size.y. It should either be spriteHeight or you should base it on sprite.x
  2. You are ignoring the sprite scale. You need to also use go.get("#spriteAsteroid", "scale.y")
  3. Sprites can be non uniformly scaled. I think you should multiply with go.get_scale_vector().y instead of go.get_scale()

I recently created an example of how to detect clicks on game objects with a sprite component and had to use to tale into account both scale of game object and of the sprite to get correct results.

5 Likes

Gosh darn it…sorry 'bout the typos, and thanks for the info in #3!

Correcting my original post now… :slight_smile:

Bryan

2 Likes

@Pkeod, did you ever finish your supersampling vs multisampling in Defold investigation? Any results to share?