Skip to content

Instantly share code, notes, and snippets.

@TigerHix
Last active August 6, 2023 14:34
Show Gist options
  • Save TigerHix/81cfa66a8f810165c426d1b5157677b5 to your computer and use it in GitHub Desktop.
Save TigerHix/81cfa66a8f810165c426d1b5157677b5 to your computer and use it in GitHub Desktop.
using UnityEngine;
using Warudo.Core;
using Warudo.Core.Attributes;
using Warudo.Core.Data;
using Warudo.Core.Graphs;
using Warudo.Core.Server;
namespace Playground {
[NodeType(Id = nameof(StructuredDataExampleNode))]
public class StructuredDataExampleNode : Node {
[DataInput]
public MyCustomData[] Data;
/**
* Think of structured data as a way to define a custom serializable type.
*/
public class MyCustomData : StructuredData {
[DataInput]
public int MyInt = 7;
[DataInput]
public string[] MyStringArray;
/**
* StructuredData is also an Entity, so it has OnCreate()/OnDestroy() methods.
*/
protected override void OnCreate() {
base.OnCreate();
Watch(nameof(MyInt), () => {
Context.Service.Toast(ToastSeverity.Info, "Holy moly, my int changed!", "It is now " + MyInt);
});
}
protected override void OnDestroy() {
base.OnDestroy();
// Cleanup stuff here
}
/**
* For per-frame events though, only OnUpdate() is available.
*/
protected override void OnUpdate() {
base.OnUpdate();
}
/**
* StructuredData supports triggers (buttons in editor) as well.
*/
[Trigger]
public void Trigger() {
Context.Service.Toast(ToastSeverity.Info, "Holy moly, I am triggered!", "MyInt is " + MyInt);
}
}
[DataInput]
public MyCustomDataWithParent[] DataWithParent;
/**
* What if you want to reference the StructuredDataExampleNode instance *inside* a MyCustomData instance it owns?
* You can inherit from the generic-typed StructuredData class:
*/
public class MyCustomDataWithParent : StructuredData<StructuredDataExampleNode> {
[DataInput]
public string ParentNodeId;
protected override void OnAssignedParent() {
base.OnAssignedParent();
ParentNodeId = Parent.Id.ToString(); // Parent is of type StructuredDataExampleNode
BroadcastDataInput(nameof(ParentNodeId));
}
protected override void OnCreate() {
base.OnCreate();
// Note that Entity.OnCreate() is called before StructuredData.OnAssignedParent(), so Parent is still null here!
Debug.Log("Parent is null? " + (Parent == null));
}
}
[DataInput]
public MyCustomDataWithHeader[] DataWithHeader;
/**
* Sometimes you want to make your StructuredData collapsible, so that it doesn't take up too much space in the editor.
* Just like CharacterAsset.Meshes, or FloatPendulumPhysicsNode.Arms.
* To do this, implement ICollapsibleStructuredData and return a header string.
*/
public class MyCustomDataWithHeader : StructuredData, ICollapsibleStructuredData {
[DataInput]
public string MyString;
[DataInput]
public int OtherField;
public string GetHeader() {
return MyString ?? "String not set";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment