0 Favourites

Implementing a dialog system for construct2 adventure games

  • 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

    Thanks,

    Josh

  • 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?

  • 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.

  • [/code:2wuzpy2g]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:2wuzpy2g]
    		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;
    		}
    [/code:2wuzpy2g]
    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.
  • My last post wasn't very helpful. However I think I found what you need. So here's some more code:

    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;
    [/code:3goj65s5]
    This is assuming that the 'name' Instance Variable of the dialogbox Object Type.
  • Thanks, 509Dave16!

    I'll test it out and let you know.

  • 509Dave16, I'm trying this, and it is a good direction with one problem.

    When I call the line:

    var objInst = this.runtime.createInstance(objTypeID, this.layer);[/code:1dw0g48t]
    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?
  • 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:

    type.plugin.singleglobal[/code:1cy5vqaj]
    Also here's the revised code snippet:
    [code:1cy5vqaj]
    		 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;
    		}
    [/code:1cy5vqaj]
  • 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:

    var objInsts = objType.instances [/code:1t1n3x7v]
    so I'm just taking the relevant object types and picking their instances.
    
    Thanks for all your help!
  • 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.

  • could you share this plugin?interesting to see it

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • It might take some time, but I'm planning to.

    BWT: Ashley suggested against all the object referencing we discussed here since it doesn't work in production.

    Instead I have to init each one of the actors using an action and keep a reference to it that way instead.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
1,068 148,694
Reflextions's avatar
Reflextions
Unread sticky
763 305,709
asikanddestroy's avatar
asikanddestroy
Unread hot topic
256 37,731
tutbarao's avatar
tutbarao