Big List of Defold Pro Tips!

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

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