0

How to code custom events

Bardo 2 years ago updated by JasonJonesLASM 2 years ago 6

Hi, i would like to make my own event unit, like the "OnDestinationReached" one that comes with bolt, i tried looking at the metadata but there are only the definitions there and no actual code. Can someone shine some light into this matter? Simple example, i want to do the same thing on 2d, once my transform gets close enough to its destination, trigger the event. Im looking into these things because ill use Bolt mosty for FSM and i would like to have full control of my transitions using my own code. Thanks!! 

PS:I know i can have my script run a update and then trigger an custom event, but i really want to be able to make my own events.

Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):
+5

This should comment and show about all base units stuff you'd need to know and a new event. Enjoy :) http://www.lifeandstylemedia.com/uploads/bolt/MyEventUnit.cs

*ps if you do want your units to integrate Unity's lifecycles, implement IUnityLifecycle.

using System;
using Ludiq;
using Bolt;
using UnityEngine;
namespace Lasm.BoltAddons.SingleUnitTemplate
{
    [TypeIcon(typeof(MyEventUnit))]   // Choose the type icon. @32x - headers; @16x -ports and fuzzy finder categories. Located in Editor Default Resources.
    [UnitSurtitle("")]  // Title above Short Title
    [UnitShortTitle("")] // The replacement name for Unit Title on the unit itself.
    [UnitTitle("")]   // Sets the actual named title of the unit, this is used in the Fuzzy Finder.
    [UnitCategory("")] // Sets unit category in Fuzzy Finder. Subfolders are matched and created.
    public class MyEventUnit : GlobalEvent
    {
        // Set the in value port declaration
        [DoNotSerialize]
        public ValueInput valueIn;
        // Set the out value port declaration
        [DoNotSerialize]
        public ValueOutput valueOut;
        // a variable located at the header of the unit
        [Inspectable] // Auto sets up a variable to be seen and adaptive (aka a property drawer)
        [UnitHeaderInspectable("")] // Sets this up as a header for this variable.
        [InspectorLabel("")] // Sets the label for the inspector in the Graph Inspector and more
        public object headerVariable;
        // Setup all ports and use logic to effect them
        protected override void Definition()
        {
            // object is your type. change it to whatever you really want to use. Neither in or out is needed.
           
             // You can delete this. This is how you declare a ValueInput.
            valueIn = ValueInput<object>("variable");
            // You can delete this. This is how you declare a ValueOutput
            Func<Recursion, object> funcVariableOut = currentFuncVariableOut => VariableOut();
            valueOut = ValueOutput<object>("variableOut", funcVariableOut);
        }
        public object VariableOut()
        {
            // Return your output variable. Make sure the type matches the ValueOutput and Func Recursion.
            return new object();
        }
        public override void StartListening()
        {
            // I'm going to assume you know how to do delegate events, so this is where you add a receiver.
            // MyDelegate += MyMethod;
            // Get the base of this method from GlobalEvent. We need this to do all the work.
            base.StartListening();
        }
        public override void StopListening()
        {
            // MyDelegate -= MyMethod;
            // Get the base of this method from GlobalEvent. We need this to do all the work.
            base.StopListening();
        }
        public void MyEventTrigger()
        {
            Trigger(); // Trigger the out flow on the unit. this is already setup with global events, you just trigger it when you need to.
        }
    }
}

Forgot to mention, but events at the moment needed to be in events category. So [UnitCategory("Events/MyEvents")]

Lazlo did make mention on discord he may consider changing the hard path when events get updated. So, that may change.

Thanks for the template! I still have some questions tho, do you mind if I add you on Discord?

Nope, go in ahead! Happy to help.

i know its JasonJonesLASM, but i dont have your tag with the #0000, im Bardo#0843 there. you can shoot me a message or answer here with your full tag including the number thingies

Weird I can't find you either. Just go on scripting API and leave a message, I'll respond and add you that way.