#NodeSystem

1 messages · Page 1 of 1 (latest)

half aspen
#
using System.Collections.Generic;
using UnityEngine;

namespace Content
{
    public class Node
    {
        public Node(Vector2 position, bool nodeStatus)
        {
            Position = position;
            NodeStatus = nodeStatus;
        }

        public Vector2 Position { get; set; }
        public bool NodeStatus { get; set; }
    }

    public class NodeSystem : MonoBehaviour
    {
        public NodeSystem()
        {
            FreeNodes = new List<Node>();
            ConnectedNodes = new List<Node>();
        }

        public List<Node> FreeNodes { get; set; }
        public List<Node> ConnectedNodes { get; set; }

        public void Start()
        {
        }

        //MergeNodeSystem
        public void MergeNodeSystem(NodeSystem incomingNodeSystem) //todo use raycasting to detect nearby nodes
        {
            List<Node> duplicates = new List<Node>();
            foreach (Node incomingNode in incomingNodeSystem.FreeNodes)
            {
                foreach (Node ourNode in FreeNodes)
                {
                    if (incomingNode.Position == ourNode.Position)
                    {
                        duplicates.Add(incomingNode);
                        duplicates.Add(ourNode);
                    }
                }
            }

            foreach (Node duplicate in duplicates)
            {
                incomingNodeSystem.FreeNodes.Remove(duplicate);
                FreeNodes.Remove(duplicate);
                incomingNodeSystem.ConnectedNodes.Add(duplicate);
                ConnectedNodes.Add(duplicate);
            }
        }

        public void AddNode(Vector2 position)
        {
            FreeNodes.Add(new Node(position,
                true)); 
        }

        public void RemoveNode(Node node)
        {
            FreeNodes.Remove(node);
        }
    }
}
#

had to remove some redundant bits due to message limit

hollow notch
#

ok, so you have nothing marked as [Serialized] there

half aspen
#

and contextual comments

#

do I just add serializefield to the freenodes and connected nodes?

hollow notch
#

wait, how are you expecting to save this, it's not a monobehaviour or a scriptable object

half aspen
#

in another file which way goes over message limit

#

lemme break it into parts

#
private void OnSceneGUI()
        {
            Handles.color = Color.red;

            // Loop through the attachPoints array
            for (int i = 0; i < _nodeSystem.FreeNodes.Count; i++)
            {
                Vector2 newPosition =
                    Handles.PositionHandle(_nodeSystem.transform.TransformPoint(_nodeSystem.FreeNodes[i].Position),
                        Quaternion.identity);

                // Check if the point has moved
                if (newPosition != _nodeSystem.FreeNodes[i].Position)
                {
                    Undo.RecordObject(_nodeSystem, "Move Attach Point");
                    _nodeSystem.FreeNodes[i].Position = _nodeSystem.transform.InverseTransformPoint(newPosition);
                }

                // Draw a sphere handle at the attach point's position
                EditorGUI.BeginChangeCheck();
                float handleSize =
                    HandleUtility.GetHandleSize(
                        _nodeSystem.transform.TransformPoint(_nodeSystem.FreeNodes[i].Position)) *
                    0.1f;
                Vector2 newPointPosition = Handles.FreeMoveHandle(
                    _nodeSystem.transform.TransformPoint(_nodeSystem.FreeNodes[i].Position),
                    handleSize, Vector2.one, Handles.SphereHandleCap);
                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(_nodeSystem, "Move Sphere Handle");
                    _nodeSystem.FreeNodes[i].Position = _nodeSystem.transform.InverseTransformPoint(newPointPosition);
                }
            }
        }```
#
public override void OnInspectorGUI()
        {
            // Draw the default inspector for the script
            DrawDefaultInspector();


            //NodeSystem nodeSystem = (NodeSystem)target;
            for (int i = 0; i < _nodeSystem.FreeNodes.Count; i++)
            {
                EditorGUILayout.BeginHorizontal();
                _nodeSystem.FreeNodes[i].Position =
                    EditorGUILayout.Vector2Field("Free Node " + (i + 1), _nodeSystem.FreeNodes[i].Position);
                if (GUILayout.Button("Remove Node", GUILayout.Width(100), GUILayout.Height(20)))
                {
                    _nodeSystem.FreeNodes.RemoveAt(i);
                }

                EditorGUILayout.EndHorizontal();
            }

            if (GUILayout.Button("Generate Attach Points"))
            {
                GenerateAttachPoints(_nodeSystem);
            }

            // Add a button to add a new point to the array
            if (GUILayout.Button("Add Node"))
            {
                Undo.RecordObject(_nodeSystem, "Add Attach Point");
                _nodeSystem.AddNode(Vector2.zero); //create new empty node
            }
        }```
hollow notch
#

I still see nothing there which will save anything

half aspen
#
        private void GenerateAttachPoints(NodeSystem nodeSystem)
        {
            SpriteRenderer spriteRenderer = nodeSystem.GetComponentInChildren<SpriteRenderer>();

            if (spriteRenderer == null)
            {
                Debug.LogError("No SpriteRenderer found on NodeSystem's game object or any of its children.");
                return;
            }
            float halfHeight = 0.5f, halfWidth = 0.5f; //TEMPORARY
            nodeSystem.AddNode(new Vector2(-halfWidth, 0)); // Left side
            nodeSystem.AddNode(new Vector2(halfWidth, 0)); // Right side
            nodeSystem.AddNode(new Vector2(0, halfHeight)); // Top
            nodeSystem.AddNode(new Vector2(0, -halfHeight)); // Bottom
        }
#

nodeSystem.AddNode should work shouldn't it? as theres an instance of the Nodesystem attached to the object we are editing

#

Unity seems to know this

#

as if I click off the prefab and back on all my changes persist

hollow notch
#

you cannot just have POCO's and save them into thin air, you need to code a save system for them

half aspen
#

what is POCO?

hollow notch
#

Plain old C# object

half aspen
#

ok

half aspen
hollow notch
#

because you are using [serializefield] inside a Monobehavior or a ScriptableObject. Ask yourself, where do I expect Unity to save this stuff?

half aspen
#

My answer given all my prior experience would be that it should be attached to the prefab, in the meta.cs file just as if I had edited a transform component on that object

#

I'm no professional of course which is why I'm looking for help understanding

hollow notch
#

but how can you attach a POCO to a Prefab, you can't use Add Component because it is not a Component

half aspen
#

from the tutorials I had watched I thought a component was a behavoir that was charaterized by being able to be attached to many different objects so to be modular

hollow notch
#

true but your classes are non of those things

half aspen
#

oh?

hollow notch
#

public class Node
where is the inheritance from a Component or Behaviour?

#

wait my bad

#
 public class NodeSystem : MonoBehaviour
    {
        public NodeSystem()
        {
            FreeNodes = new List<Node>();
            ConnectedNodes = new List<Node>();
        }

        public List<Node> FreeNodes { get; set; }
        public List<Node> ConnectedNodes { get; set; }

here it is, I was looking at the Node class.
in that case it's simple

[System.Serializable]
public class Node

job done

half aspen
#

really??

#

oh unity crashed while we were chatting haha

hollow notch
#

yep, NodeSystem is serialized because it is a Monobehaviour.
Node was not because it is a POCO and you did not mark it as Serializable

half aspen
#

ok I did that but it still isn't working, let me send a short video to demo what I'm trying to do since I found it super hard to explain

hollow notch
#

so now in your editor code you just need
SetDirty(_nodeSystem);
AssetDatabase.Save();

hollow notch
half aspen
half aspen
hollow notch
#

OnDisable or OnFocus is favorite

half aspen
#

TIL

#

those are both new to me

half aspen
hollow notch
#

might it not be a good idea to refer to the documentation?

#

actually forget OnFocus, just use OnDisable

#

Also I would highly reccommend using UIToolkit for new editor scripts rather than IMGUI

half aspen
#

is that something from package manager?

hollow notch
#

yes, it's the new Unity Ui system

half aspen
#

gotcha

half aspen
hollow notch
#

yep, Editor only has OnDisable and OnDestroy, but using OnDisable to save should be sufficient

half aspen
#

This right?

hollow notch
#

looks about right

half aspen
#

throws an error whenever window is closed as target is null

#

doesnt seem to save either

hollow notch
#

is target not _nodeSystem?

half aspen
#

you'd think

#

it is nodesystem and has the corresponding type

hollow notch
#

then it cannot be null, or, at least it should not be

half aspen
#

even when it works though it won't save