Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon

15. August 2016


Welcome back to the ongoing Defold Game Engine tutorial series, today we are going to build on the Sprites and Animation tutorial and look at three critical aspects of Defold game programming, scripting and messages.  Defold is programmed using the Lua scripting language which is beyond the scope of what we will be covering in this tutorial.  However we have already completed a Lua programming course for beginners available here.  If you are unfamiliar with Lua, it will teach you everything you need to know to get started with the language.

As with all tutorials in this series, there is an HD video version available here.


Setting up Input


If you created a default project, you will see a folder named Input, like so:


If for some reason you don’t have this input_binding file, you can create one by right clicking, selecting New->Input Binding File


The Input Binding File is a special file that maps various input devices to a text string that identifies the action to perform when that input occurs.  Double click the game.input_binding file and it will open in the editor:


You will notice there is a category for keys, mouse, gamepad, touch and text.  Right click the category you want to add an input control for and select the Add ___ trigger option.



This will create a new entry.  In the input value, drop it down and select the key/button/action you want to bind.  In the value name it.  In this example I’ve mapped KEY_SPACE to the event toggle_move.



The cool thing about this approach is you can map multiple input devices to the same action, like so:



Now the spacebar and left mouse button with both fire off the toggle_move action.  You will see how this works in just a split second.




Now that we have some input firing, lets set up a script to handle it.  Building on the project we made in the last tutorial, add a script file to your game.  In my case it looks like:



We already covered the basics of creating a script in this earlier tutorial if you need to details on how to proceed creating a script.  In the previous tutorial we created an animation called walk.  Create another one called “Idle” using the same process, but add just a single frame of animation, so it looks like this:



We are now going to add the code to toggle between the two animation states.  In your freshly created .script file, add the following code:

local currentAnimation = 0

function init(self)".","acquire_input_focus")

function on_input(self, action_id, action)
    if action_id == hash("toggle_move") and action.pressed == true then
      if self.currentAnimation == 1 then"#sprite","play_animation", {id = hash("walk")})
        self.currentAnimation = 0
      else"#sprite","play_animation", {id = hash("idle")})
        self.currentAnimation = 1
      return true -- input handled.


There are a couple key things here… first you notice the local variable currentAnimation.  This is simply going to track which animation is playing so we can switch between them.


Next we have the init() callback function.  This function is called ONCE by the Defold game engine, when the game object this script is attached to is created.  This is where you would do your various initialization and setup logic.  In this case we are telling Defold that we want to have input sent to this game object.  That way when you hit the keyboard or press a mouse for example, this scripts on_input() function will be called.  This is done by sending a message, message passing is very important and we will cover it shortly.  For now know that the first parameter in the post() call is where to send the message.  “.” is shortcode for “to the object this script is attached”.  Meanwhile ‘#’ is shorthand for “to this script itself”.  Dont worry, we will cover this in more detail in a moment.  The second parameter is the message we want to send.   In this case we are sending the message “acquire_input”, which is a predefined message type in GameObject.


Now that we’ve told Defold we respond to input, we not implement the on_input() callback function.  First we check to see what the action id is, but this value will be encoded, so simply pass it into the hash() function.  Basically we test to see if our toggle_move action that we defined earlier has been fired.  Remember, this is wired to the left mouse button or spacebar being hit.  We also check to make sure it’s a pressed event (as opposed to a release).  Next we toggle our input, if our currentAnimation value is 0 we are currently walking so now we want to change to the idle animation, and vice versa.  Notice that we change the animation once again by using a message.  In this case we are passing the message “play_animation” to the object “#sprite”.  This refers to a fragment, part of an underlying message url… dont worry, it’ll make sense in a moment.  For now just think of #sprite as saying, send this message to the sprite component of this object. 



If you look at the documentation for any given class… such as Sprite, you will see the various messages it will respond to.



Sending and receiving messages is the core of how objects in Defold communicate.  It’s a way of having different objects able to communicate and share data or respond to actions or events, while remaining loosely coupled.


Now if you run this code, it will start with the player walking.  Hitting space bar or left clicking will fire off the toggle_move event, and the animation should now switch.  Let’s look a bit closer at how you can send messages between objects.

What I did is created a new folder named other, inside of which I created a game object named other and attached a script to it, named inventively enough other.script, like so:



In your main.collection, add an instance of other, like so:



Other does absolutely nothing, but is going to illustrate how two game objects can communicate with … um… each other.  Ugh.

In other.script, add the following code:

function update(self, dt)"main:/character", "marco", { msg = "You out there?", meaningOfLife = 42 })


The update() function is the callback called by the Defold engine every single pass through your game loop.  In this case we are simple sending a message to our character game object in the main folder.  The message is called “marco” (for marco/polo).  You will notice we also pass a table with some pretty useful data in it…  msg and meaningOfLife, the first a string the second a number.  The data you fill in this table is completely optional… in fact you dont need to pass data at all.


The important part to understand is the URL we are using to post the message to.  It’s a lot like the URL you use to go to a website.  The first part (in this case) is referring to the collection the target is in, the second part is the name itself.  It doesn’t work exactly like what you might expect though, as here is the structure:


The folder name has ZERO importance in this scenario… it’s just there as a handy way for you to organize things.  The root of the url (main:/) refers entirely to the name of the collection, then the remainder of the URL, refer to values within that collection.

So we are sending this message to the character game object inside main.collection:



If we had instead wanted to send the message directly to the sprite attached to the character, we could have sent the message to main:/character#sprite


Now we can modify our code inside main to listen for this message:

function on_message(self, message_id, message, sender)
    if(message_id == hash("marco")) then


The on_message() function is called when the object your script is attached to receives a message.  In this case we check to see if the message was “marco”.  Again we encode it using the hash() function.  If you run this code you will see Polo written over and over in the console.  You will also see the data that was sent in the message, remember that table we encoded earlier.   Note the use of pprint().  This is a handy function that performs a pretty print of a table you pass in.  Basically it prints the table out in a nice easily read form.


There are a couple special message targets available as well.  @physics, @render and @system.  These actually send messages to the Defold game engine itself, the physics, rendering and system sub systems respectively.  For example, we can see that the render subsystem responds to the following messages:



So we could easily change the background color sending a message valued clear_color to @render like so:"@render:","clear_color", { color = vmath.vector4(0,0,1,1) })

In this case our data is a Vector4 with the value (0,0,1,1).  This is an RGBA color value, basically saying no red, no green, full blue and full opacity.  AKA… blue.  This will cause our background color to be blue instead of black.


This messaging system may at first seem very alien, but it quickly becomes intuitive.  You do have to keep the reference handy to see what messages each object responds to.




Finally let’s look quickly at the concept of Properties before moving on.  You’ve already dealt with several built in properties.  For example, when you created a game object, it had several built in properties already defined:



You can also easily create your own properties.  This is a great way to make code reusable, or set it to so that a coder can code, but expose some data to a designer.  The process is simple, in our script file, simply add:"MyProperty",42)

To the top of the script file.

f you are wondering just where the heck came from, it’s simply a member of Game Object which you can think of as your base class.


You can define a property anywhere, except inside the various different Defold callbacks (such as Init()).  Now you can use this property anywhere just like any other local variable, like so:



The cool part however is, now if you check in the Editor for that script, you will see the property is exposed and can easily be overriden:



The Video


blog comments powered by Disqus

Month List

Popular Comments

Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon

6. December 2016


The HTML5 powered Babylon.js 3D game engine just released version 2.5 today.  BabylonJS is open source (Apache License) and one of the best available native HTML5 3D game engines available.  I previously featured it in the Closer Look game engine series if you want more details on the engine.  We are also currently producing an in-depth BabylonJS tutorial series if you want to learn how to use this powerful engine.


The 2.5 release is packed full of new features, updates and fixes from the release notes:

Major updates
  • New StandardRenderingPipeline effect to support screen space lens flare and depth of field. Demo - (Julien Moreau-Mathis)
  • New HighlightLayer object to enable highlights rendering. Demo - (sebavan)
  • Babylon.js now supports right handed system with scene.useRightHandedSystem = true (deltakosh)
  • Babylon.js is now compiled with optimize-js to get faster initial load (deltakosh)
  • Canvas2D moved to a separate folder in main repo. Now you need to also include babylon.cavans2d.js to get Canvas@D feature (deltakosh)
  • New BoneIKController Demo (abow)
  • New BoneLookController Demo (abow)
  • You can now build your own version of babylon.js with gulp build-custom Doc - (deltakosh)
  • Added node.doNotSerialize to prevent specific nodes to be serialized by SceneSerializer(deltakosh)
  • Added scene.multiPick and scene.multiPickWithRay to return an array of pickedMesh objects (deltakosh)
  • Added Effect.GetVertexShaderSource() and Effect.GetFragmentShaderSource() (deltakosh)
  • New Texture.LoadFromDataString() to help loading base64 encoded textures (deltakosh)
  • Added Engine detection of the compresed texture formats supported by Hw / browser. You can specify those formats you have files for using Engine.setTextureFormatToUse(), and an appropriate one will be chosen. (Palmer-JC)
  • Added Ray.intersectsMesh,, Ray.hide (abow)
  • Added AbstractMesh.setPivotPoint, AbstractMesh.getPivotPoint, AbstractMesh.getAbsolutePivotPoint (abow)
  • Added Debug.AxesViewer and Debug.BoneAxesViewer (abow)
  • Added Bone.getAbsolutePositionFromLocal and getLocalPositionFromAbsolute (abow)
  • Added Bone.setRotation, Bone.getRotation, Bone.setRotationQuaternion, Bone.getRotationQuaternion (abow)
  • Added Bone.getAbsolutePosition and Bone.getAbsolutePositionToRef (abow)
  • Added Bone.translate, Bone.setPosition, Bone.setAbsolutePosition (abow)
  • Added Bone.setYawPitchRoll, Bone.setRotationMatrix, Bone.setScale, Bone.setAxisAngle (abow)
  • Added Bone.rotate (abow)
  • Added Bone.scale (abow)
  • Added Camera.getDirection, AbstractMesh.getDirection, Bone.getDirection (abow)
  • Added subdivisionsX, subdivisionsY option to GroundMesh (abow)
  • New Tools.CreateScreenshot function will capture all canvas data. Previous implementation is now called CreateScreenshotUsingRenderTarget (deltakosh)
  • Cube textures are now cached by texture cache (deltakosh)
  • Added onAnimationEnd callback for sprite.playAnimation (deltakosh)
  • Added support for non square textures for sprites (deltakosh)
  • Added support for texture arrays (deltakosh)
  • Added camera.isInFrustum and camera.isCompletelyInFrustum. Can be used with meshes, submeshes and boundingInfo (deltakosh)
  • Several memory allocation reduction (benaadams)
  • Several GPU state change reduction (benaadams)
  • MapTexture: add supersample mode to double font quality. (nockawa)
  • New SPS feature : solid particle intersection with other solid particle or with any mesh particle.intersectsMesh() (jerome)
  • New invertUV parameter an all ribbon based shapes : ribbon, tube, lathe, basic and custom extrusion (jerome)
  • Text2D: new fontSuperSample setting to use high quality font (nockawa)
  • PerfCounter class added to monitor time/counter and expose min/max/average/lastSecondAverage/current metrics. Updated engine/scene current counter to use this class, exposing new properties as well to access the PerfCounter object (nockawa)
  • Better keyboard event handling which is now done at canvas level and not at window level (deltakosh)
  • New scene.hoverCursor property to define a custom cursor when moving mouse over meshes (deltakosh)
  • Canvas2D: (nockawa)
    • Performance metrics added
    • Text2D super sampling to enhance quality in World Space Canvas
    • World Space Canvas is now rendering in an adaptive way for its resolution to fit the on screen projected one to achieve a good rendering quality
    • Transparent Primitives are now drawn with Instanced Array when supported
    • New property in Canvas2D (instances) that contains all instances of canvas2d Temechon
  • WebVR Camera was updated to be conform with the current specs. (RaananW)
  • New "CubeTextureTask" function will allow you to load a CubeTexture in the assetsManager. (agallouin)
  • Scene.stopAnimation has now an optional second parameter, the name of the animation to kill. Usefull if a mesh has multiple animations. (agallouin)
Bug fixes
  • Fixed issue with SkeletonViewer not displaying correctly with meshes that have a PoseMatrix (abow)
  • Fixed issue with Quaternion.toEulerAnglesToRef (abow)
  • Fixed issue with Animatable.goToFrame (abow)
  • Fixed issue with instancse and viewports (deltakosh)
  • Fixed issue with FreeCamera not working in fullscreen or when pointer locked (abow)
  • MapTexture: Font Characters are now correctly aligned on Chrome (nockawa)
  • Fixed some missing parameter default values in MeshBuilder.CreateGroundFromHeightMap() and MeshBuilder.CreateTiledGround() (jerome)
  • Fixed cross vector calculation in _computeHeightQuads() that affected all the GroundMesh.getHeightAtCoordinates() and GroundMesh.getNormalAtCoordinates() methods (jerome)
  • Fixed Mesh.CreateDashedLines() missing instance parameter on update (jerome)
  • Added BBox update on each ribbon based shape (ribbon, tube, extrusion, etc) on dynamic updates (jerome)
  • Fixed model shape initial red vertex color set to zero not formerly being taken in account in the SolidParticleSystem (jerome)
  • Fixed billboard when the SPS mesh is parented in the SolidParticleSystem (jerome)
  • Fixed RenderTargetTexture meshes selection (deltakosh)
  • Fixed camera speed computation (deltakosh)
  • Fixed bug with instances, LOD and edgesRendering (deltakosh)
  • Canvas2D: (nockawa)
    • WorldSpaceCanvas2D:
      • Intersection/interaction now works on non squared canvas
    • Primitive:
      • ZOrder fixed in Primitives created inline
      • Z-Order is now correctly distributed along the whole canvas object graph
    • Sprite2D:
      • texture size is now set by default as expected
      • can have no id set
    • Text2D:
      • Fix bad rendering quality on Chrome
      • Rendering above transparent surface is now blending correctly
Breaking changes
  • was renamed to FollowCamera.lockedTarget to avoid conflicts (deltakosh)
  • Removed legacy shaders support (deltakosh)
  • Canvas2D: (nockawa)
    • WorldSpaceCanvas2D:
      • WorldSpaceRenderScale is no longer supported (deprecated because of adaptive feature added).

GameDev News

6. December 2016


The Atomic Game Engine just released a new version available for download here.   This is the first formal binary release in a long time, but there has been constant work on this engine since I featured it in the Closer Look game engine series.  Perhaps the biggest change was the move to open source under the very liberal MIT license but the changes certainly don’t stop there.  The next biggest new feature on the listatomic is probably the fact Atomic now supports the C# language, along side JavaScript and TypeScript (and of course, native C++).


New to this particular release are:

• Support for Atomic C# scripting on Windows, macOS, Linux, Android, and iOS

Visual Studio and Xamarin Studio integration (with VSCode support coming soon!)

• Updated to TypeScript 2.0

• Physically Based Rendering (PBR) - Thanks to @dragonCASTjosh

• New examples and project templates

• Revamped build targeting JavaScript, TypeScript, C#, and native C++

• Updated to Urho3D 1.6 and SDL 2.0.4 with a great number of platform improvements

• Updated to to Monaco VSCode editor 0.6

• Bug fixes, improvements, and optimization

Thunderbeast Games LLC have put together a demo reel of the major new features added to engine in 2016 viewable below.


The Atomic Game Engine is available here while the source is available on Github.

GameDev News

6. December 2016


A couple years ago I did a detailed text tutorial on how to use a debugger which oddly is a massively important skill that simply isn’t taught.  Given that this article is still popular two years later I’ve decided to follow it up with a video version.  This video, Debugging 101, walks through the basic tasks involved in debugging.  It used Visual Studio 2017 and C++ but should be applicable in most languages and IDEs.  The video shows how breakpoints and conditional break points work, how to step into, over and out of your code, how to use the local and watch window, call stacks, how to do memory debugging and more.  Basically the video shows you how to get started using a debugger.


The following is the code used in this example.  There is nothing special to this code, it’s extremely contrived, but it enabled me to show the various features available in most debuggers.

#include <iostream>

// These two functions are used to illustrate how the call stack works
// As well as how step into and step out of behave.
int innerFunction(int input) {
	int meaninglessCounter = 0;
	for (int i = input; i > 0; i--) {
		// First show stepping through the loop
		// Set a conditional breakpoint that breaks when i is a certain value.
	return input;

int outerFunction() {
	int i = 42;
	return innerFunction(i);

class Demo {
	std::string stringValue;
	int intValue;
	bool booleanValue;

		Demo(std::string a, int b, bool c) : stringValue(a), intValue(b), booleanValue(
		c) {};

int main(int argc, char ** argv) {
	// Callstack demo, jump into, jump over example
	int someVal = 0;
	someVal = outerFunction();

	// Data example -- simply create a char buffer, fill it with 'a' then null 
	terminate it so 
	// it can be treated like a string.
	char * data = new char[1000];
	for (int i = 0; i < 1000; i++)
		data[i] = 'a';
	data[999] = 0;
	std::cout << data << std::endl;

	//set a watch on d.  Demonstrates watches and drilling into complex object
	Demo d("Hello", 42, true);
	std::cout << "End of demo" << std::endl;
	delete[] data;
	// delete[] data;  Calling delete again will trigger an exception

Programming , , ,

5. December 2016


The mobile focused cross platform Lua powered game engine Defold just released version 1.2.94.  The release includes several bug fixes but more importantly contains some rather cool new features.  A big one, and something that was glaringly missing, is (initial) skeletal animation support for 3D models.  While a 2D engine, being able to mix in 3D models enables you to do some pretty powerful things, like texture swapped characters or dynamic inventory systems.  Particle system support for flipbook animation powered particles was also added in this release.

Full new feature list:

  • DEF-2308 - Added: Automatic particle resizing from flipbook animation
  • DEF-2161 - Added: Skeleton animations for 3D models
  • DEF-2295 - Added: Cursor and playback control for model animations
  • DEF-2304 - Added: Max GUI scene count per collection is now configurable in game.project
  • DEF-2202 - Fixed: Up-axis for Collada models are now taken into account
  • DEF-2202 - Fixed: Correct UV/texture coordinates for Collada models
  • DEF-2303 - Fixed: Label with dynamically set text remained after go.delete
  • DEF-2308 - Fixed: Automatic sprite resizing from flipbook animation
  • DEF-1972 - Fixed: Disable animating node during complete callback didn't update property fragments
  • DEF-2274 - Fixed: Spine nodes with animated alpha now works as expected
  • DEF-2145 - Fixed: Libraries/dependencies loading performance


You can read more, especially on the use and limitations of the new 3D animation system, in the release notes available here.  If you are interested in learning to use the Defold Engine we have a complete tutorial series available here.  If you just want to learn more be sure to check out our hands on video.

GameDev News

5. December 2016


Just recently at MechCon 2016, Piranha Games ( MechWarrior Online ) announced they were developing MechWarrior 5: Mercenaries as a single player game.  Why is this of interest to readers?  There are two reasons, one big one extremely small.  First is Piranha’s decision to make the game using Unreal Engine.  This is interesting due to the fact that the studio’s previous title MechWarriormechwarrior5 Online was developed using CryEngine.  In fact it may have been one of the highest profile non-Crytek CryEngine titles that actually shipped ( sorry Star Citizen… ).  No doubt MW5 will re-use a number of assets from MWO and could have benefited from a great deal of the existing code, so the decision to change engines couldn’t have been made lightly.  This is a pretty nice win for Unreal Engine and has to sting a bit for Crytek.  Lead programmer Brian Windover gave the following answer on why they chose Unreal Engine:

“While deciding on a technology base for MechWarrior 5: Mercenaries it quickly became clear that Unreal Engine 4 would really hit all the notes for us,” said Lead Programmer Brian Windover. “When we were able to take our past experience developing a MechWarrior title and bring it to life in Unreal Engine 4 so quickly, and with such a high degree of visual fidelity, we had the immediate confidence that it was the clear engine of choice for MechWarrior 5: Mercenaries.”

“Unreal Engine 4’s increasingly versatile Blueprint system enables our designers to quickly prototype their ideas and craft the foundations of an outstanding single-player campaign. Its core technology, intuitive AI framework, and native VR support enables our engineers to introduce truly next-gen features with great ease and reliability. On the artistic side, Unreal Engine 4 is stunning, but it’s also extremely adaptable. It empowers our artists to really push our visuals to the edge, both in detail and scope, while still maintaining excellent performance.”


So, what’s the second reason?  The previous game, Mechwarrior 4:Mercenaries is easily in my top 5 games of all times list.  I also enjoyed MW:O, especially once their terrible publisher was removed from the equation, so I feel the title is in good hands.  Basically I’m just giddy this title was announced and I really look forward to seeing it happen.  The following early alpha video was shown along side the announcement:




For the three people that might be curious, my top 5 games of all times ( not the best games of all time, but certainly the ones that affected me the most) in no particular order is:

  • MechWarrior 4 series
  • Ultima 7
  • Tie Fighter
  • ShadowRun (Genesis Version)
  • Baldur’s Gate 2


Do you have a similar list? 

GameDev News

Month List

Popular Comments