how to determine the object type to pass to createInstance

0 favourites
  • 5 posts
From the Asset Store
Change delay, create new lines, "backspace" the text
  • 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:

    [quote:1v5c1z11]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:

        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;
        };
    [/code:1v5c1z11]
    
    This code works just fine in the development environment. I pass the [b]typeName[/b] 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 [b]name[/b]s 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?
  • 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.

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

    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;
    };[/code:38jvi0km]
    
    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?
  • Type name will be minified while exporting. So you need to have another name-reference mapping by actions in event sheet.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Rex Thank you! I get it now -- and it works great.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)