A game about forced loneliness, made by TACStudios
1using System;
2
3namespace Unity.VisualScripting
4{
5 /// <summary>
6 /// Creates a struct with its default initializer.
7 /// </summary>
8 [SpecialUnit]
9 public sealed class CreateStruct : Unit
10 {
11 [Obsolete(Serialization.ConstructorWarning)]
12 public CreateStruct() : base() { }
13
14 public CreateStruct(Type type) : base()
15 {
16 Ensure.That(nameof(type)).IsNotNull(type);
17
18 if (!type.IsStruct())
19 {
20 throw new ArgumentException($"Type {type} must be a struct.", nameof(type));
21 }
22
23 this.type = type;
24 }
25
26 [Serialize]
27 public Type type { get; internal set; }
28
29 // Shouldn't happen through normal use, but can happen
30 // if deserialization fails to find the type
31 // https://support.ludiq.io/communities/5/topics/1661-x
32 public override bool canDefine => type != null;
33
34 /// <summary>
35 /// The entry point to create the struct. You can
36 /// still get the return value without connecting this port.
37 /// </summary>
38 [DoNotSerialize]
39 [PortLabelHidden]
40 public ControlInput enter { get; private set; }
41
42 /// <summary>
43 /// The action to call once the struct has been created.
44 /// </summary>
45 [DoNotSerialize]
46 [PortLabelHidden]
47 public ControlOutput exit { get; private set; }
48
49 /// <summary>
50 /// The created struct.
51 /// </summary>
52 [DoNotSerialize]
53 [PortLabelHidden]
54 public ValueOutput output { get; private set; }
55
56 protected override void Definition()
57 {
58 enter = ControlInput(nameof(enter), Enter);
59 exit = ControlOutput(nameof(exit));
60 output = ValueOutput(type, nameof(output), Create);
61
62 Succession(enter, exit);
63 }
64
65 private ControlOutput Enter(Flow flow)
66 {
67 flow.SetValue(output, Activator.CreateInstance(type));
68
69 return exit;
70 }
71
72 private object Create(Flow flow)
73 {
74 return Activator.CreateInstance(type);
75 }
76 }
77}