Character Trait Model: How Unhappy Is Unhappy

Modelling character traits can be tricky: an example problem has been presented by Jon Ingold in this GDC talk. I discuss the problem below and present a sample model that solves it. I provide an implementation in a C# class.

For character trait one may think of say the character happiness, or a relationship-with-X trait.

The problem is presented from minute 36 of the talk: the first idea that comes to mind in modelling a character trait is by using a number. Greater the number, better the state of the trait. This doesn’t work very well.

How Unhappy is Unhappy

From Jon Ingold GDC talk.

Then Ingold quickly jumps to a proposed solution, which consists in tracking two numbers, positive and negative experiences:

Unhappy vector

From Jon Ingold GDC talk.

Apart from the fact that changes are modelled more appropriately using two variables, what was exactly the problem with using one number?

Here is how I understood the problem: suppose you want to model the happiness of a character in a gameplay. You say that the variable HappinessLevel determines HappinessState according to these values:

HappinessLevel >= 5 = VERY_HAPPY
0 < HappinessLevel < 5 = HAPPY
-5 < HappinessLevel <= 0 = SAD
HappinessLevel <= -5 = SUICIDAL

The character goes through many episodes in two different game-plays: in one the character has 2 positive episodes, and 8 negative ones, and so goes SUICIDAL: 80% of the episodes were negative.

In another gameplay, the character has 20 positive episodes and 25 negative ones. Character is still SUICIDAL, but actually only  55% of episodes were negative! Something clearly does not work :-(

Taking the hint from the talk above, I’ve implemented a generic class model for Character Trait that considers the whole set of the episodes. The set of states and their level can be injected; moreover you can have a “decay %” so that for each new episode, all previous ones have a decay, so older the episode less relevant it gets :-) (by default decay is 1 so its turned off).

You find the class and a test (for Unity) in this zip. It can clearly be refined ad infinitum in function of specific needs, e.g. having episodes that are both positive and negative and so on.

A couple tests:

First test no decay test 1 output
Second test with decay Log with decay

Thanks to Daniele Giardini for campaigning for LINQ removal from the code.

Follow me on Twitter where I post about game design, game development, Unity3d 2D, HTML5, applied / serious games.

Social Share Toolbar

Is Unity WebGL loading? A simple loader

F = (When you publish your Unity application or prototype in WebGL a problem that your testers or players may have is that while the browser is loading the application, no progress bar appears and so they may be tempted to reload the page again and so… F() ).

A simple fix to reassure them is to paste this script at the bottom of the index.html file that Unity produces at build:

<script>
  /**
   * Created by
   * Matteo Bicocchi @pupunzi
   * Pietro Polsinelli @ppolsinelli
   * Follow us as we are creating a game on #football 
   * with a story: "Football Voodoom" :-)
   * */

  var appName = "<b>" + Module.codeUrl.replace("Release/", "").replace(".js","") + "</b> ";
  var canvas = document.getElementsByTagName("canvas")[0];
  var canvasRect = canvas.getBoundingClientRect();

  var loader = document.createElement("div");
  loader.id = "loader";

  var loaderWidth = canvas.offsetWidth;
  var loaderHeight = canvas.offsetHeight;

  document.body.appendChild(loader);

  loader.style.position = "absolute";

  loader.style.top = 0;
  loader.style.bottom = 0;
  loader.style.left = 0;
  loader.style.right = 0;
  loader.style.margin = "auto";
  loader.style.width = loaderWidth + "px";
  loader.style.height = loaderHeight + "px";
  loader.style.background = "transparent";
  loader.style.color = "#fff";
  loader.style.opacity = 0.8;
  loader.style.fontSize = "16px";
  loader.style.letterSpacing = "3px";
  loader.style.fontWeight = "100";
  loader.style.textAlign = "left";
  loader.style.padding =  "30px";
  loader.style.boxSizing = "border-box";

  var dotCount = 0;
  var checkDisplay = setInterval(function(){

    if(Module.progress.progress > 0) {
      loader.style.display="none";
      clearInterval(checkDisplay);
    } else {

      loader.innerHTML = "Loading " + appName;

      for (var x = 0; x<dotCount; x++){
        loader.innerHTML+= ".";
      }
      
      dotCount = dotCount >= 40 ? 0 : ++dotCount;
    }
  },100)
</script>

This will show immediately “Loading…” and will animate it until the app loader appears. See it here:

Unity WebGL custom loader

Cool no? The JavaScript is really basic and should work wherever WebGL does.

 

 

 

Social Share Toolbar

Tips For The Pragmatic Unity Developer

In the video below I’ve presented several productivity tips / hacks for the great Unity3d developer, that cultivates his / her essential qualities (impatience laziness hubris):

 

Sources & References

The three most essential plugins are DOTween, Text Mesh Pro and Console Pro.

Jet Brains IDE for C# preview here.

The “auto-save on play” script is here, The “no more hot reload script” is here, both by the same cool guy.

The scene object enable and disable script is this:

public class TheScene : MonoBehaviour
    {
        public GameObject[] activateOnStartup, deactivateOnStartup;
        
        void Start()
        {
            foreach (GameObject go in activateOnStartup)
            {
                go.SetActive(true);
                if (go.GetComponent<CanvasGroup>() != null)
                    go.GetComponent<CanvasGroup>().alpha = 1;
            }
            foreach (GameObject go in deactivateOnStartup)
            {
                go.SetActive(false);
                if (go.GetComponent<CanvasGroup>() != null)
                    go.GetComponent<CanvasGroup>().alpha = 0;
            }
        }
    }

Follow me on Twitter where I post about game design, game development, Unity3d 2D, HTML5, applied / serious games.

Social Share Toolbar

Games Need Depth: My Research on Football

I’ve just published a post on Gamasutra with some reflections and sources on creating depth in games via narrative research, and how I am trying to do that on Football Voodoom.

Social Share Toolbar

Untangling GameObject State in Unity

In this short post and video I try to discuss and clarify a few points about GameObject state in Unity with respect to game, scene and “runtime” scope. It is a bit more complex than one may understand initially, so bear with me a little.

When you start developing scenes in Unity, it won’t take long before you start asking questions like:

How can I get the same GameObject in different scenes?

Why do static properties sometimes get reset across scenes?

Why when I reload a scene I get duplicated objects which are meant to be singletons?

How can I comply with the (highly practical) principle “Make the game runnable from every scene” when I have global instances from other scenes?

Here are some answers.

Here is the full schema I refer to:

GameObject State in Unity

While MonoBehaviour’s  life-cycle is quite well documented e.g. both directly in Unity docs here and also by third parties e.g. here, how to handle GameObject’s persistence in and across scenes may be more obscure.

So here are written (partial) answers to the questions above:

How can I get the same GameObject in different scenes?
Would be probably better to reword this as “how can I persist and share data across scenes” – and there are many ways to do that :-)

Why do static properties sometimes get reset across scenes?
Only static properties which are GameObjects present in the scene will get reset (typically singletons). Other static properties will be preserved across the virtual machine.

Why when I reload a scene I get duplicated objects which are meant to be singletons?
That is because you marked those objects with DontDestroyOnLoad and created them (also) in other scenes. Create them via code (not in hierarchy) checking before their existence.

How can I comply with the (highly practical) principle “Make the game runnable from every scene” when I have global instances from other scenes?
This is best done just as explained above: create the global objects in every scene via code if they don’t already exist.

 

Unity Execution Order

Unity Execution Order – from Unity documentation.

If you are in need of learning some good patterns in software game development, a really nice book is Game Programming Patterns. Here are also 50 Tips for Working with Unity (Best Practices) which I reread from time to time, understanding progressively more and more of them (but still not all :D).

Thanks to Daniele Giardini for some feedback on the state scheme above.

Follow me on Twitter where I post about game design, game development, Unity3d 2D, HTML5, applied / serious games.

Social Share Toolbar

Creating Match 3 Games in Unity

How to create quickly a match 3 game in Unity? Or any kind of match game? I have been working on an original match 3 game (an applied game actually) together with Unity 2D guru Daniele Giardini and I took some notes, which I present in the video below. This is not a tutorial: I just point out some concepts you will probably need to deal with.

Consider also that this is all done with Unity 5, which by introducing 2D, sprites (and also new UI) has made it much simpler to create this kind of games. And as in all my games, its actually Unity plus Gamelogic Grids component, as in my models (and probably in games in general) grids are pervasive.

Here is the video:

I started writing my tests from GameLogic match 3 sample project. The code project is based on GameLogic’s Grids for Unity component, and I’m using as much as possible the DOTween’ component for smoothing movements and effects.

The GameLogic fellows compiled an exhaustive survey of match games, including a wonderful infographic on match game structure spectrum:

2015-09-18 16_28_08-Microsoft Edge

Subtleties of match 3 game mechanics

explodingSubtleties of game design mechanics emerge once you are creating a prototype: sequential matching, peculiarities of click / swipe matching, generative universe (no need of generational AI) … discrete / continuous matches … .

If you believe that game design is basically a formal activity, mainly to do with mechanics (which actually I believe to be too simplistic) you need to be a developer at least at some level, because in building the prototype you understand the mechanics, with all its possible variations.

References

Match Game Mechanics: An exhaustive survey.

GameLogic Match 3 sample project.

Grids for Unity GameLogic FAQ.

Match 3 game draft in Unity tutorial with full code.

You Must Build a Boat.

Follow me on Twitter where I post about game design, game development, Unity3d 2D, HTML5, serious games.

Social Share Toolbar

Simple Behaviour Trees for your game in Javascript and C#

A little while ago I tried to understand what Behaviour Trees are about, as in a couple of games I’m developing I have sets of NPCs that I have to manage and I would like to somehow decouple their behaviour from the main game flow. Here you can find a good explanation of behaviour trees.

Sample behaviour tree

Sample behaviour tree

Got this, I searched for implementations (I wanted both Javascript and C# implementations) and I didn’t like them because of the amount of code and classes required. Then on StackOverflow I found this very simple example:

Stack Overflow simple behaviour trees

Encouraged by this,  with Matteo Bicocchi we started writing our own implementations, as I simply could not believe that the code needed to be so complex 😉 . We first wrote the Javascript implementation, and the two main points we had to deal with have been:

1. Distinguish between the tree description and the instance tree being run: you need a concrete instance as you need to be independent of “tick” speed and need to be able for each instance to find what is the current node.

2. You need to handle single node completion asynchronously e.g. if you have a “chase” node, this may take a long time to resolve.

Now we have our implementation, its very simple code and can be useful in a wide variety of game development situations – surely its not a complete solution but it works fine.

Here is a simple Js Fiddle example you can play with directly in the browser.

Fiddle with behaviour trees

and here are full sources in both Javascript and C# on GitHub. The C# code is a simple port from Javascript, surely it could be refined (branch it!). It also contains two classes making the C# example run, its made with a for Unity (extends MonoBehaviour) but the code can be used anywhere.

The code is mostly commented in the Javascript version.

Follow me on Twitter where I post about game design, game development, Unity3d 2D, HTML5, applied games.

Social Share Toolbar