A game about forced loneliness, made by TACStudios
1using System.Collections.Generic;
2using UnityEngine;
3using UnityEngine.SceneManagement;
4
5namespace Unity.VisualScripting
6{
7 [AddComponentMenu("Visual Scripting/Variables")]
8 [DisableAnnotation]
9 [IncludeInSettings(false)]
10 [VisualScriptingHelpURL(typeof(Variables))]
11 public class Variables : LudiqBehaviour, IAotStubbable
12 {
13 [Serialize, Inspectable]
14 public VariableDeclarations declarations { get; internal set; } =
15 new VariableDeclarations() { Kind = VariableKind.Object };
16
17 public static VariableDeclarations Graph(GraphPointer pointer)
18 {
19 Ensure.That(nameof(pointer)).IsNotNull(pointer);
20
21 if (pointer.hasData)
22 {
23 return GraphInstance(pointer);
24 }
25 else
26 {
27 return GraphDefinition(pointer);
28 }
29 }
30
31 public static VariableDeclarations GraphInstance(GraphPointer pointer)
32 {
33 return pointer.GetGraphData<IGraphDataWithVariables>().variables;
34 }
35
36 public static VariableDeclarations GraphDefinition(GraphPointer pointer)
37 {
38 return GraphDefinition((IGraphWithVariables)pointer.graph);
39 }
40
41 public static VariableDeclarations GraphDefinition(IGraphWithVariables graph)
42 {
43 return graph.variables;
44 }
45
46 public static VariableDeclarations Object(GameObject go) => go.GetOrAddComponent<Variables>().declarations;
47
48 public static VariableDeclarations Object(Component component) => Object(component.gameObject);
49
50 public static VariableDeclarations Scene(Scene? scene) => SceneVariables.For(scene);
51
52 public static VariableDeclarations Scene(GameObject go) => Scene(go.scene);
53
54 public static VariableDeclarations Scene(Component component) => Scene(component.gameObject);
55
56 public static VariableDeclarations ActiveScene => Scene(SceneManager.GetActiveScene());
57
58 public static VariableDeclarations Application => ApplicationVariables.current;
59
60 public static VariableDeclarations Saved => SavedVariables.current;
61
62 public static bool ExistOnObject(GameObject go) => go.GetComponent<Variables>() != null;
63
64 public static bool ExistOnObject(Component component) => ExistOnObject(component.gameObject);
65
66 public static bool ExistInScene(Scene? scene) => scene != null && SceneVariables.InstantiatedIn(scene.Value);
67
68 public static bool ExistInActiveScene => ExistInScene(SceneManager.GetActiveScene());
69
70 [ContextMenu("Show Data...")]
71 protected override void ShowData()
72 {
73 base.ShowData();
74 }
75
76 public IEnumerable<object> GetAotStubs(HashSet<object> visited)
77 {
78 // Include the constructors for AOT serialization
79 // https://support.ludiq.io/communities/5/topics/3952-x
80 foreach (var declaration in declarations)
81 {
82 var defaultConstructor = declaration.value?.GetType().GetPublicDefaultConstructor();
83
84 if (defaultConstructor != null)
85 {
86 yield return defaultConstructor;
87 }
88 }
89 }
90 }
91}