how to determine the object type to pass to createInstance

For developers using the Construct 2 Javascript SDK

Post » Sat Jun 20, 2015 3:55 pm

I'm writing a plugin that creates new instances of objects that exist in the layout.

The SDK Runtime reference has this to say about runtime.createInstance:

runtime.createInstance(type, layer)
Create a new instance of the object type on the given layer. Returns a reference to the created instance.


After some searching the source (and a bit of trial-end-error) I was able to find a way get a reference to a type by its name:

Code: Select all
    function createObject(typeName,x,y) {
        var type = null;
        for( var i=0; i<runtime.types_by_index.length; ++i ) {
            if( runtime.types_by_index[i].name === typeName ) {
                type = runtime.types_by_index[i];
            }
        }
        var layer = runtime.getLayerByName("game-elements");
        var object = runtime.createInstance(type,layer);
        return object;
    };


This code works just fine in the development environment. I pass the typeName of the object, like "cardBackground" or "playButton" and voila, new instances. However, when I export the project to an HTML5 website this code no longer works. This is because the names of the types appear to have been transformed. The new names are strings like "t0" or "t1".

So what is the best way to locate an object instance (or perhaps an instance-type) to use when calling runtime.createInstance?
B
6
S
1
Posts: 10
Reputation: 420

Post » Sun Jun 21, 2015 12:28 am

I had made rex_nickname plugin which could create instance by a string name.
User need to link the string name and the object type by action before using it.
B
108
S
26
G
267
Posts: 4,456
Reputation: 149,747

Post » Sun Jun 21, 2015 2:37 am

@rex, I appreciate your answer, but the code you linked didn't really clarify the issue. It looks like the mapping is created during this function call:

Code: Select all
cr.plugins_.Rex_Nickname.AddNickname = function(nickname, objtype)
{
    cr.plugins_.Rex_Nickname.nickname2objtype[nickname] = {sid:objtype.sid, index:-1};
    cr.plugins_.Rex_Nickname.sid2nickname[objtype.sid.toString()] = nickname;
};


But I think this leaves me with the same problem I asked about in the OP. See, I don't know how to get a reference a specific objtype. Or to be more precise, I can get references to lots of object types, but I don't know how to tell them apart. I don't know which one is "cardBackground" and which one is "cardText".

When you say "user need to link the string name and the object type by action", do you mean the user playing the game?
B
6
S
1
Posts: 10
Reputation: 420

Post » Sun Jun 21, 2015 3:23 am

Type name will be minified while exporting. So you need to have another name-reference mapping by actions in event sheet.
B
108
S
26
G
267
Posts: 4,456
Reputation: 149,747

Post » Mon Jun 22, 2015 3:15 am

@rex Thank you! I get it now -- and it works great.
B
6
S
1
Posts: 10
Reputation: 420


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests