Data Structuring and Accessing Troubles.

Sorry about the fairly broad concept of a question but here’s what I’m looking for:

I have a script with a three layer struct system…

Struct A holds a list of Struct B (and dynamically creates new instances of Struct B conditionally)
Struct B does the same with Struct C.

The script and the script above it, both need to be able to access fields and properties of Struct C… I try using a iteration system, but it tells me that the Lists need to be static, however I have more than one instance of these structs, and each one has its own list of structs. So I cannot use Static Lists.

Moreover, the usage of lists is only temporary, I will actually need to be able to access specific instances of those structs conditionally as well.

I originally was using individual scripts instead of structs (I don’t really know which would be the better choice, but each instance needs to be able to store it’s enum state, and scripts didn’t do that so I figured I’d just use structs and try it, yet the same problems are coming up as before… there must be a way to access that far down a chain… (I obviously have pretty strong script coupling here, reason being is it is not actually a game, I just like using Unity to visualize activity in my program and this forum because people are a lot nicer and faster to reply than stack exchange.)

If you need an example of the code I’m using then just let me know, but I think I’ve portrayed the situation…?

After reading your question, I think I maybe know what you’re saying. Who knows :slight_smile:

So, what I’d suggest is that you try to write the very simplest version of this code that you can imagine (just the point that 's troubling you, not extra stuff) and see if you can get some feedback from that. :slight_smile:

1 Like
using System.Collections.Generic;

public class NewScript
{
    public enum Classifier { AC, GD, GC, GCr, NC, DD, SD, ED }

    public struct Node
    {
        public static List<Input> inputs;
        public UnityEngine.Vector3 _location;

        public Node(UnityEngine.Vector3 location, int numInput = 0)
        {
            _location = location;

            if (numInput > 0)
            {
                for (int i = 0; i < numInput; i++)
                {
                    inputs.Add(new Input());
                }
            }
        }
    }

    public struct Input
    {
        public static List<Receiver> receivers;
        public UnityEngine.Vector3 _location;

        public Input(UnityEngine.Vector3 location, int numRcvrs = 1)
        {
            _location = location;

            for (int i = 0; i < numRcvrs; i++)
            {
                receivers.Add(new Receiver());
            }
        }
    }

    public struct Receiver
    {
        public Classifier classification;
        public int strength;

        public Receiver(Classifier classifier, int capacity = 1)
        {
            classification = classifier;
            strength = capacity;
        }

    }
}

Now then, how would I from either the containing class, or a separate class (once I’ve got the reference to the instance of this one) be able to get the properties/fields from “Receiver?” Bearing in mind that there are multiple nodes, each containing multiple receivers, each containing multiple Receivers.
**Note: I’m using the Vector3 “Location” as a way to reference particular instances of each, thus why they all have attached.

Thanks for the help.

Okay, first I’d like to address 1 part of your earlier post that I did not understand. You said that different scripts cannot store their enum state. I do not know what that means. They can’t store an enum variable? (because they can), or something else?

Well, in your example, you could use:

newScriptVar.Node.inputs[index].receiver[recIndex].fieldName;

I think so? :slight_smile:

Yeah, well I mean, I don’t know how to access the state of an instance of a script… How would I do that? Also I’ll give that a shot, and I also just randomly realized that I’ve been having many troubles getting the correct index of my lists and arrays, because I was starting at the zeroeth index… XD silly me… tsk tsk tsk

Okay, could you please clarify your terminology so I’m not guessing. What is “…access the state of an instance of a script” ?
a) You do not know how to have a variable for a script?
b) once you have it, how to use it?
c) something else?

:slight_smile:

Once I “set” the enum state of a particular instance (internally), how would I then later “get” that enum state from an external script?

Do you mean like this?

NewScript ns; // assume this is created/set/available.. ? :)
// obviously your inputs and receiver lists would have to be filled, too.
Classifier classy = ns.Node.inputs[index].receiver[recIndex].classification;

That’s a partial guess, but I think that’s what you were asking.

1 Like

Wait, I believe there is an error in my logic here.

I believe you can access them, rather than what I said before, but like so:

ns.Input.receivers[recIndex].etc;

There is really no point in the ‘Node’ access, because the list in Input is static.
If you want unique lists for each Node’s Input and in turn each Input’s receivers, the lists shouldn’t be static. Then, we’d be back to my previous code example, I believe.

1 Like

Aye, they aren’t meant to be static, it was a temporary fix to ignore other errors. They are each unique lists. I’ll play around with these and see what I can get
Thanks. :slight_smile:

Seems I’ve got it all, now I’m working on custom editors for it… (oops… bad choice) but I think I can finally start moving on to the main framework of my system. You’ve helped out on many posts! Thanks @methos5k :slight_smile: really appreciate the time and guidance you’ve provided. I’ll definitely share the final project once I get that far. :stuck_out_tongue:

Cool, glad to hear it’s working out for you.
You’re welcome. Good luck with the rest of your project :slight_smile:

1 Like