Skip to content

Instantly share code, notes, and snippets.

@lazlo-bonin lazlo-bonin/GuavaEvent.cs Secret
Created Jul 17, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.