0 Favourites

A handy method every plugin should have...

  • This has been super useful for me.

    instanceProto.fire = function(eventName)
    {
        this.runtime.trigger(cr.plugins_.MyPlugin.prototype.cnds[eventName], this);
    };

    This function allows for easy firing of conditions. For example, if you have a condition

    Cnds.prototype.OnError = function()
    {
        return true;
    };

    You can, from basically anywhere, fire that condition with a call to

    this.fire("OnError");

    or, from within an anonymous embedded method

    var self = this;
    somelibrary.AsynchronousCall().then(
        function(result)
        {
            self.status = JSON.stringify(result);
            self.fire("OnSuccess");
        },
        function(result)
        {
            self.status = JSON.stringify(result);
            self.fire("OnError");
        });

    The <font face="Courier New, Courier, mono">fire</font> method could be made safer with a check that the event exists, but the above is the simplest form. I would suggest adding this to the template files for plugins and behaviors. What do others think?

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • As an addendum, in the plugin I'm working on now, I have the following set up:

    instanceProto.setError= function(errorString)
    {
        this.status.lastError = errorString;
        this.runtime.trigger(cr.plugins_.MyPlugin.prototype.cnds.OnError, this);
    }
    
    instanceProto.fire = function(eventName)
    {
        if (cr.plugins_.MyPlugin.prototype.cnds[eventName])
        {
            this.runtime.trigger(cr.plugins_.MyPlugin.prototype.cnds[eventName], this);
        }
        else
        {
            this.setError("Condition " + eventName + " not found");
        }
    };
    

    then I can set up an event that OnError prints out the error message. Of course, it may be better in other situations to log to the javascript console instead of (or in addition to) the above, but I found it handy, so I thought I'd pass on the tip.

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