Big List of Defold Pro Tips!

General information on shaders for materials.

If you are not very familiar with shader terminology (I’m still newbie myself). This document goes over the language, and for example explains such terms used in shaders as uniform or lowp. A little nicer presentation of similar information can be found here.

More info on shaders in Defold Material manual. What vertex shader attributes that are available are listed there.

This post by @sicher explains how to use render targets for post processing. I’m working on putting together various extra useful shaders and will include an example for how to apply shaders to your entire game screen or parts of it (giving your rendered screen a sepia tone but not applying it to your UI for example), such as for making a CRT like view of your game.

These docs are also useful to study

5 Likes

Dynamically loading and running Lua script with Defold.

Thanks to @sicher

References (1) (2)

If for some reason (such as NPC dialog and movement scripting - which I’ll talk about in another post) you need to load scripts dynamically (as in you don’t know what names they are beforehand, are loading their names from another arbitrary data file) then there are some special considerations requires due of the way Defold bundles files.

First put the lua scripts you want to be able to run into a special folder such as “main/extra_scripts”. Next open your game.project file in a text editor and add custom_resources with the path to your scripts. custom_resources can be a path to a folder or a single file, and you can have multiple paths separated by commas.

NOTE! It’s important to not have the leading slash on this one when you are selecting folders and not single files. At least for me on Windows if I have a leading slash Defold gives an access denied error.

script = ("/path/to/script.lua")
local code = sys.load_resource(script)
local f = assert(loadstring(code))

See reference (2) for more info.

5 Likes

If nothing seems to work when you want to check for on_input…

Remember to have

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

in your init

3 Likes

Basics of scripting NPC text taking advantage of coroutines

This post is based on this article which inspired me to try this with Defold.

scripted_dialog_rpg_text_starter_coroutines.zip (49.8 KB)

Attached is a very rough example project. I’ll upload a nicer, cleaner project later, but this starter can still give people an idea. It uses arbitrary script loading and running like mentioned in previous post. For text, it uses Lua’s coroutines. This kind of setup would be very useful with all kinds of games where you interact with scripted objects!

7 Likes

And remember to acquire input focus for your collection proxy as well if the script you are trying to acquire input focus for is loaded via a proxy. Read more about this in the “Input and Collection Proxies” in the Input documentation.

6 Likes

Detecting the current OS information for selective code. Sometimes you want to enable or disable code, or have dummy simulated code go off depending on which platform you are testing.

Is there a list of all current possibilities somewhere? Can someone check what the Linux one is?

local sysinfo = sys.get_sys_info()
if sysinfo.system_name == “Android” then

elseif sysinfo.system_name == “iPhone OS” then

elseif sysinfo.system_name == “Windows” then

elseif sysinfo.system_name == “Linux” then

elseif sysinfo.system_name == “Darwin” then – This means it’s a Mac OSX build

elseif sysinfo.system_name == “HTML5” then

end

If you are targeting iOS / Android you can get more specific information with device_model and manufacturer, which can be useful.

You can detect browser for HTML builds by parsing the information provided by user_agent.

local sysinfo = sys.get_sys_info()
if sysinfo.system_name == “HTML” then
local user_agent = sysinfo.user_agent

end

As far as I can tell, there is currently no (easy) way to detect different versions of Window, Linux or Max OS X.

More info on sys.get_sys_info


Detecting device language can be done in the same way. If you are planning to localize your game to ship with many translations you’ll want to

When you setup your localization you can look up the ISO codes for the languages you support, and if on the first start the device is a language you support you could default to it.

local sysinfo = sys.get_sys_info()
if sysinfo.device_language == “en” then

7 Likes

Recording gameplay video directly in Defold. Need some video you can quickly put onto youtube? Defold has built in support, although doesn’t support audio yet?

– Record a video in 30 fps given that the native game fps is 60:
msg.post("@system:", “start_record”, { file_name = “test_rec.ivf” } )

– To write a video in 60 fps given that the native game fps is 60:
msg.post("@system:", “start_record”, { file_name = “test_rec.ivf”, frame_period = 1, fps = 60 } )

– Stop recording
msg.post("@system:", “stop_record”)

NOTE: You MUST stop recording with the proper code to end up with correct video. Otherwise it will be corrupt.

I’m not sure what Defold is using to make VP8/IVF, but if it had an option to produce WEBM files that would be handy too!

If you do want a WEBM you can use ffmpeg

ffmpeg -i output_vp8.ivf -vcodec copy output.webm

Of course other tools such as fraps or playclaw work as well for recording gameplay video.

7 Likes

The road to making juicy buttons - on_input actions : “pressed”, “repeated” or “released” If you want to make juicy buttons then you need to polish them up so they feel good to press. When mouse is hovered make them glow, and scale them up a bit. When mouse is down (but not clicked) make the button shrink, and change its image to a depressed version. Don’t forget to play sounds on hover over / down! Don’t make the button do anything until they have actually fully clicked, their mouse is still hover it, and they let go of their mouse button. This feels the best, though there are some situations where you want instant clicks and not “fully depressed and released while hovering” inputs.

While you’re checking input, you can check for the three actions too

action.pressed – this happens as soon as you click down
action.repeated – while you keep a click held down
action.released – this happens as soon as you released the click

For juicy buttons, you’ll also need to check that their mouse cursor (or finger touch x,y) is still within the rectangle of the button when the action.released goes off. The best buttons stay depressed in a down state as long as the mouse is still pressed down even if it moves out of its rectangle, and while this happens blocks hovers etc. with other inputs until that unique event is finalized. I’m working on an example of a juicy button system for UI that I’ll upload soon.

5 Likes

Hi Pkeod!
In the next release (1.2.79), we’ve fixed the name for the web: “HTML5” (emscripten is the build tool we use, not a platform)

Also, detecting the browser can be done by parsing the “user_agent” field of the sys info. There seems to be a multitude of ways to parse the string, so I’ll just refer to google for more info on how to do it.

4 Likes

Full screen anti-aliasing would be nice.


I’m wondering if Defold team already has some materials / shaders for this? If you use any shaders beyond stock could you share them? Would save me some work.

Shaders for Defold I have working or am preparing. If this area of Defold was expanded (more textures for example - I can’t do texture based bump mapping effects at the moment) more would be possible. I am still newbie too and some are trouble to me so might not make it for now. Also, I’m heavily referencing past made shaders that are publicly available to study.

  • FXAA / SMAA - will try others too
  • Shadows
  • Blurs (a few including directional motion blur, box blur, gaussian blur)
  • Anaglyph 3d (3d glasses effect)
  • Color replacement
  • Inner / Outer Glows
  • Inverted Color
  • Lens Distortions
  • Gradient Map
  • Bloom
  • Emboss
  • Deformations - water, flag, sin wave …
  • Sharpen
  • Hue shift
  • Contrast
  • Mask Color to Transparent - useful in some situations
  • Sepia
  • CRT (a few types)
  • Vignette
  • Dot Matrix
  • Film Grain
  • Noise overlay
  • Scanlines
  • Pixelation
  • Chroma Shift / translation / displacement (can make neat effects)

More is possible but that’s the todo list for now.

4 Likes

I had not even noticed user_agent was already available. Thank you and glad to hear! Is Linux just Linux or? Will update post.

1 Like

Linux is “Linux” yes :slight_smile:

2 Likes

Pre-hashing your hashes.

If you are using hashes often in many places you can optimize some by pre-hashing once, rather than to run hash() on strings over and over as your scripts run. Doing this is easy, and a good practice - at least I assume so!

– Pre-hashing input message hashes
local move_up_hash = hash(“move_up”)
local move_down_hash = hash(“move_down”)
local move_left_hash = hash(“move_left”)
local move_right_hash = hash(“move_right”)

function on_message(self, message_id, message, sender)

if message_id == move_up_hash then
	  
	  ...
	  
end

end

4 Likes

I can definetly see the use of having a much more extended library of shaders/post effects in Defold and those are all good suggestions.

For me personally though I would like to create at least some simple shaders for 3D models (lambert, blinn, phong, ramp, toon) - problem is that I do not have the time.

3 Likes

Yes, hashing action_ids and message_ids is a good idea.

3 Likes

I can try to find some that would be possible to make work.

https://en.wikibooks.org/wiki/Category:GLSL_Programming

Many are listed here, and have examples.

This is still mostly new territory for me though, but I can do some tests and see what will work. The reason I want FXAA and SMAA or some AA for post processing that’s fast enough for mobile is because of removing jagged edges from rendering 3d models.

Although FXAA may not work for this specific case.

FXAA Off

FXAA On

MSAA might be better. Thoughts? …I don’t know what would look good and be efficient.

3 Likes

Humus (an old colleague) is quite obsessed with AA and his site could be a source of inspiration.

3 Likes

They do seem to be appropriate for what I need. Thank you! I’ll give it a shot getting it to work with Defold.

Been doing some tests and while it does work in some of the model very well the pixel art sections are just textures, so they are not detected in the same way as the geometry edges.

Going to more things next.

https://blogs.msdn.microsoft.com/shawnhar/2011/04/26/supersampling/

https://blogs.msdn.microsoft.com/shawnhar/2011/04/27/multisampling/

2 Likes

Posting code blocks.

I keep forgetting to not use quotes and to use pre-formatted text when posting code blocks. If the code you post doesn’t look right this is probably what’s wrong. Paste the text first, select it, then apply the Preformatted text option from the menu.

But want to know something even better?

Blockquote

function on_message(self, message_id, message, sender)
if message_id == hash(“start_level”) then
– some script tells us to start loading the level
msg.post("#proxy", “async_load”)
– store sender for later notification
self.loader = sender
elseif message_id == hash(“proxy_loaded”) then
– enable the collection and let the loader know
msg.post(sender, “enable”)
msg.post(self.loader, message_id)
end
end

Preformatted text / Codeblock

function on_message(self, message_id, message, sender)
    if message_id == hash("start_level") then
        -- some script tells us to start loading the level
        msg.post("#proxy", "async_load")
        -- store sender for later notification
        self.loader = sender
    elseif message_id == hash("proxy_loaded") then
        -- enable the collection and let the loader know
        msg.post(sender, "enable")
        msg.post(self.loader, message_id)
    end
end

But want to know something even better? You can turn on a bit of syntax highlighting too and have nice code blocks without needing to indent. Here is how:

```lua
function on_message(self, message_id, message, sender)
```
function on_message(self, message_id, message, sender)
    if message_id == hash("start_level") then
        -- some script tells us to start loading the level
        msg.post("#proxy", "async_load")
        -- store sender for later notification
        self.loader = sender
    elseif message_id == hash("proxy_loaded") then
        -- enable the collection and let the loader know
        msg.post(sender, "enable")
        msg.post(self.loader, message_id)
    end
end
4 Likes

Beta builds of Defold

There are publicly available beta builds of Defold.

New stable builds are released every two weeks.

Unless you have a good reason you probably shouldn’t download the betas, especially if you are trying to be productive with a project as it might have serious issues that would stop you! They may be unstable, they may crash more often than stable builds would. Don’t use the unstable alpha / beta builds unless you have a good reason to such as being asked to for testing purposes.

Beta builds for Defold can be download directly here:

http://d.defold.com/beta/

(Plus Alpha although I just checked and it says it’s two version behind Stable, but also has the HTML5 system_name fix so maybe bleeding edge but not version number updated? I don’t know)

http://d.defold.com/alpha/

Current stable builds of Defold, as well as past stable builds, (useful if you build a project in a version that for some reason won’t work in later version…) are here:

http://d.defold.com/stable/

Using the auto update feature is the best way to download new versions of Defold, but there may be cases where you need to download again cleanly.

1 Like