Skip to content

Instantly share code, notes, and snippets.

@lazlo-bonin
Created July 17, 2018 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazlo-bonin/ee2dd7f48e7562e65c383fb10f9c49f6 to your computer and use it in GitHub Desktop.
Save lazlo-bonin/ee2dd7f48e7562e65c383fb10f9c49f6 to your computer and use it in GitHub Desktop.
using System;
using Bolt;
using Ludiq;
using UnityEngine;
public class TestComponent : MonoBehaviour
{
public event Action<TestArgs> testEvent;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
Debug.Log("Triggering Test.SuperEvent");
if (testEvent != null)
{
testEvent(new TestArgs { testArg = true });
}
}
}
}
public class TestArgs
{
public bool testArg;
}
[UnitCategory("Events")]
public sealed class TestEventUnit : EventUnit<TestArgs>
{
public new class Data : EventUnit<TestArgs>.Data
{
public TestComponent testComponent;
}
[DoNotSerialize]
public ValueInput testComponent { get; private set; }
[DoNotSerialize]
public ValueOutput testArg { get; private set; }
protected override bool register => false;
protected override void Definition()
{
base.Definition();
testComponent = ValueInput<TestComponent>(nameof(testComponent));
testArg = ValueOutput<bool>(nameof(testArg));
}
public override void StartListening(GraphStack stack)
{
var data = stack.GetElementData<Data>(this);
if (data.isListening)
{
return;
}
var reference = stack.ToReference();
var testComponent = Flow.FetchValue<TestComponent>(this.testComponent, reference);
if (testComponent == null)
{
return;
}
Action<TestArgs> handler = args => Trigger(reference, args);
testComponent.testEvent += handler;
data.testComponent = testComponent;
data.handler = handler;
data.isListening = true;
}
public override void StopListening(GraphStack stack)
{
var data = stack.GetElementData<Data>(this);
if (!data.isListening)
{
return;
}
if (data.testComponent != null)
{
data.testComponent.testEvent -= (Action<TestArgs>)data.handler;
}
data.isListening = false;
}
protected override void AssignArguments(Flow flow, TestArgs args)
{
base.AssignArguments(flow, args);
flow.SetValue(testArg, args.testArg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment