Implementing a dialog system for construct2 adventure games

For developers using the Construct 2 Javascript SDK

Post » Fri Apr 18, 2014 8:53 pm

Hi, I'm currently trying to implement a dialog system for construct2 (we are building an adventure game).
The idea is to have a spritefont object that hovers over the character's head with text that changes according to the game's current state and actors (we want to avoid code bloat in the eventsheet).

I've already implemented a plugin that has a getNextDialogLine(actor_a, actor_b, state) function that's returning the next dialog line
between two actors in the scene according to the game state.

How do I bind this function to a Sprite Font element? should I write a behavior? can I access it throught the plugin? any clues will be welcomed :mrgreen:

Thanks,
Josh
Last edited by shukshuk on Thu Apr 24, 2014 9:15 pm, edited 1 time in total.
B
4
Posts: 8
Reputation: 216

Post » Sat Apr 19, 2014 1:59 am

Hey, @shukshuk! Could you by chance further clarify your question about binding a function to a Sprite Font Element? Are you wanting to update the Sprite Font element with a value returned from your getNextDialogLine(actor_a,actor_b,state) Plugin Action?
B
9
S
2
G
1
Posts: 41
Reputation: 708

Post » Sat Apr 19, 2014 4:17 pm

Thanks for the reply @509Dave16.
I want to manage the dialogs externally.
I'll try to elaborate some more:
I have two actors, let's call them Joe and Jasper
So I have a sprite of joe, with a sprite font named dialogbox-joe pinned to it, and a sprite of jasper with a sprite font named dialogbox-jasper
When calling getNextDialogLine("joe","jasper", somestate) let's say its joe's turn to speak; this is what I want to do from within the plugin
1) set dialogbox-jasper to be invisible
2) set text for dialogbox-joe
3) set dialogbox-joe to visible

I don't mind changing my design, but I do want to keep the code contained in the plugin.
B
4
Posts: 8
Reputation: 216

Post » Sat Apr 19, 2014 4:52 pm

Code: Select all
I understand what you are talking about now. I've been attempting to look into how to access the Plugin Object Type Instances(basically just instances of objects). If you look at the 'Object type' and 'Instances' manual entries under the 'SDK Reference', you'll find useful information regarding the attainment of instances of an Object Type. I'm still researching on how to do this, because I've seen a few other requests like yours. Through Javascript you need to try obtaining the text contents of the Prototypes\Functions\Objects that are assigned to an Instance. Here are a few lines of code that you can put in a Plugin Action that should get you started:
Code: Select all
      var objectTypesArray = this.runtime.types_by_index;
      var araLength = objectTypesArray.length;
      var instValuesArray = [];
      for (var araIndex = 0; araIndex < araLength;  araIndex++)
      {
            var instObj = this.runtime.createInstance(objectTypesArray[araIndex],this.layer);
            var valueStr = "";
            for(var item in instObj)
            {
               valueStr += item + " : " + instObj[item] + "\n";
            }
            valueStr+= "\n\n\n\n\n";
            instValuesArray[araIndex] = valueStr;
      }

You want to most likely set an entire String representation of the instValuesArray to a property of the Plugin you built(or a test Plugin). Then access that property through an expression at runtime in the Event Sheet, setting an Array Plugin Instances first dimension first element to that property's string. Then use the Array's Download Action to save the Array in a file as JSON. You'll have to use a text editor and what not format the element's text in the JSON file. Basically you have to inspect this String to find out where the Names of the Object Type and the Instance Properties\Variables are in the chain. Then from there figure out how to access them so that you can set for example your SpriteFont's text. I hope this helps somehow.
B
9
S
2
G
1
Posts: 41
Reputation: 708

Post » Mon Apr 21, 2014 1:53 am

My last post wasn't very helpful. However I think I found what you need. So here's some more code:
Code: Select all
var objectTypesArray = this.runtime.types_by_index;
var NameTypePairs = {};
var TypeInstNameIndexPairs = {};
for(var araIndex in objectTypesArray)
{
    var objTypeID = objectTypesArray[araIndex];
    var objInst = this.runtime.createInstance(objTypeID, this.layer);
    var instVarNames = objInst.instance_var_names;
    var instVars = inst.instance_vars;
    var InstNameIndexPairs = {};
    for(var instVarIndex in instVarNames)
    {
      var name = instVarNames[instVarIndex];
      var index = instVarIndex;
      InstNameIndexPairs[name] = index;
    }
    var objType = objInst.type;
    this.runtime.DestroyInstance(objInst);
    var objName = objType.name;
    NameTypePairs[objName] = objType;
    TypeInstNameIndexPairs[objName] = InstNameIndexPairs;
}
var dialogboxType = NameTypePairs["dialogbox"];
var dialogboxTypeInstVars = TypeInstNameIndexPairs["dialogbox"];
var nameVarIndex = dialogboxTypeInstVars["name"];
var dialogboxInst = dialogboxType.instances;

for(var instIndex in dialogboxInst)
{
   var inst = dialogboxInst[instIndex];
   if(inst.instance_vars[nameVarIndex] = "Joe")
   {
       var instJoe = inst;
   }
   if(inst.instance_vars[nameVarIndex] = "Jasper")
   {
       var instJasper = inst;
   }
}

instJasper.visible = false;
instJoe.text = ".........";
instJoe.visible = true;

This is assuming that the 'name' Instance Variable of the dialogbox Object Type.
B
9
S
2
G
1
Posts: 41
Reputation: 708

Post » Mon Apr 21, 2014 1:18 pm

Thanks, @509Dave16!
I'll test it out and let you know.
B
4
Posts: 8
Reputation: 216

Post » Thu Apr 24, 2014 11:44 pm

@509Dave16, I'm trying this, and it is a good direction with one problem.
When I call the line:
Code: Select all
var objInst = this.runtime.createInstance(objTypeID, this.layer);

lt tries to create a new instance of a singleton plugin (like keyboard for example) and throws an exception.

I wanted to filter by the typeName, but apparently you are using your objInst for getting the type name. is there any other way to get the current instances typeNames? do you know any way around this?
B
4
Posts: 8
Reputation: 216

Post » Fri Apr 25, 2014 4:07 am

I'm really sorry about the last code snippet I gave you. I made some incorrect assumptions about the 'types_by_index'. It actually does contain Type objects and not Type string identifiers. So I changed the code snippet to reflect that as well as fix the one little error that you probably already found. And you can access the flag 'singleglobal'(which indicates if a Plugin is a singleton) from the plugin object like this:
Code: Select all
type.plugin.singleglobal

Also here's the revised code snippet:
Code: Select all
       var objectTypesArray = this.runtime.types_by_index;
      var NameTypePairs = {};
      var TypeInstNameIndexPairs = {};
      for(var araIndex in objectTypesArray)
      {
         var objType = objectTypesArray[araIndex];
         var isSingleGlobal = objType.plugin.singleglobal;
         var objName = objType.name;
         if(!isSingleGlobal)
         {
            var objInst = this.runtime.createInstance(objType, this.layer);
            var instVarNames = objInst.instance_var_names;
            var instVars = objInst.instance_vars;
            var InstNameIndexPairs = {};
            for(var instVarIndex in instVarNames)
            {
              var name = instVarNames[instVarIndex];
              var index = instVarIndex;
              InstNameIndexPairs[name] = index;
            }
            this.runtime.DestroyInstance(objInst);
            TypeInstNameIndexPairs[objName] = InstNameIndexPairs;
         }
         NameTypePairs[objName] = objType;
      }
B
9
S
2
G
1
Posts: 41
Reputation: 708

Post » Fri Apr 25, 2014 9:07 am

Thanks,
I took your latest code and modified it a bit.

Apparently you don't have to create new instances.
You can just get the the existing instances of each one of your objects and manipulate them using:
Code: Select all
var objInsts = objType.instances

so I'm just taking the relevant object types and picking their instances.

Thanks for all your help!
B
4
Posts: 8
Reputation: 216

Post » Fri Apr 25, 2014 2:11 pm

Ya. You're right actually now that I think about it. I was thinking about it all wrong. Thanks for your clarifications. I'm glad it worked out in the end for you.
B
9
S
2
G
1
Posts: 41
Reputation: 708

Next

Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests