Big List of Defold Pro Tips!

Easily desaturate the color of sprites with this custom sprite shader.

Based on this post

A modified sprite shader (other shaders can be modified in same way) that adds ability to change saturation in the same way tinting was previously mentioned. I changed the example slightly (did I misunderstand this?) because it seems fully 1 makes whatever is being unsaturated fully desaturated, but in the code it was ignoring a full value of 1. A value of 0 does not modify it however. Default value is 0.

materials.zip (1.3 KB)

Put into main folder and the shader can be applied to sprites.

12 Likes

By default, Defold renders different parts of your game separately. As it renders the different parts, you have the opportunity to manipulate each added layer as it is composed into the final image your players see.

This (among other things) is controlled in the .render_script

If you want to customize the settings of your game’s rendering you must make a new render script. Copy and paste the folder “render” from the “builtins” folder and paste it into the main folder of your project.

Next you need to modify the new copy of the main.render to point to the new copy of your main.render_script

Next you need to tell Defold to use your new copy of the render script and not the standard one.

Open the game.project and scroll until you see the boostrap section. Change the render: so that it points to your main.render script. Once you select the main.render script it will automatically add the c at the end, which is the eventual compiled version.

You can also manually type the path for entries like these if you know it. You do not have to press the … button and then search for it. If you do manually type the render_script url in don’t forget to add the c at the end.

Now that you have a new copy of the render script, you can change it to better fit the needs of your game… very radically if you wish. You can remove entire sections so they don’t render at all if you want.

The default render script is meant for 2d games - an orthographic projection without perspective. It ignores z depth in terms of scaling things with distance, and only uses z depth for layering. Defold is still a 3d engine always. By default, what looks like a 2d scene is still something rendered in a 3d world although in a flat way.

Though Defold is optimized for 2d games, it is possible to make 3d games with it, and to use 3d effects with it. Though making 3d games with Defold currently requires extra work on your part that may change in the future as features are built up by everyone involved.

If you have a 3d game with UI you’ll want to set the projection so that it is suitable for the 3d scene, and then set it back to orthographic projection (plus other things such as adjust camera) before you render your UI.

You don’t have to make a fully 3d game. You could make some 3d models and use them in special areas of your game, and then render as a layer in your render script.

You can still render 3d content in an orthographic projection too, but it will look flat.

You may have noticed the Cameras feature either as an option to add in the Defold Editor or when you glanced at the Camera Manual and thought that by changing the z distance of the camera you could change how everything seen by the camera can be scaled to be zoomed in and out, but hopefully after reading the previous text in this post you can understand that this would not be the case without changing the projection type.

Pro Tip! Read the Manuals!

Later I do plan to make some kinds of 3d games, and I’ll share the render scripts of how the back and forth with changing the settings. It’s still relatively new to me, and trying to explain things can help with learning them better too!

5 Likes

How to set your game up if you want to make a pixel art game. With Defold, you can make great looking pixel art games. If you wanted to, you could even apply shaders to your game to make your game look like it’s being played on an old style screen (2) (topic for another post).

Say you want to make a NES style game. The resolution of the NES is 256 x 240. You’ll make your assets to fit this resolution.

Here is a screenshot of the NES game Castelvania in native resolution from an emulator.

Playing on a computer in Window mode you’ll probably want your users to have an option to play at a larger screen, at least twice the size.

After you create your project, edit the game.project and set its display width and height to that of double the NES

While you’re there change your graphic sampling so that they are set to nearest

In your render_script, you will make it so that your game assets made for the NES resolution are scaled up during rendering to fix the game’s resolution.

Here is a sample render script to study

pixel.lua (2.8 KB)

This script includes three features 1. it upscales assets to fit your game’s view 2. it constraints the view to be proportional while the game’s window is resized 3. it centers the view inside of the view differently depending on if the window is wider or taller.

You’ll want to modify this script further to optimize and adjust it for the needs of your pixel art game.

And another style to study

Unfortunately at this time (Version 1.2.78)

  • there is no way to change the game window size via scripting. If there was, then in your game’s option menu you could give the user the ability to select 1x, 2x, 3x etc. window scaling so that its scaling is pixel perfect
  • there is no way to change to and from fullscreen mode with scripting
  • while in fullscreen mode, the game is scaled to a bad resolution, and doesn’t scale to the full resolution of a user’s monitor - a maximized game window looks much better than a fullscreen mode window, which looks stretched and blurry

If you want to use pixel fonts in your game you’ll need to know that they do not currently respect the sampling set in game.project - to fix this, make a copy of the font.material from the builtins/font folder, and add a sampler to it with these settings

Then in your .font files for pixel fonts change its material to the new font material.

Your fonts will look right in game, but currently in the editor they will look blurry due to a bug.


These are the basics for starting to make a pixel art game with Defold, the rest is up to you - or another post.

11 Likes

Offline searchable Defold references.

Thanks to @britzl

Download this

https://github.com/britzl/defolddocset

Use it either with https://kapeli.com/dash on Mac or https://zealdocs.org/ for Windows / Linux

Either rebuild his way, or copy and paste the defold.docset folder (if it looks recent enough) into the appropriate folder / or open it with the app of your choice. For me on Windows with Zeal the folder is C:\Users\Brian\AppData\Local\Zeal\Zeal\docsets which you can see in the General tab in options for Zeal.

9 Likes

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