#Check for references in a derived class

1 messages · Page 1 of 1 (latest)

umbral crest
#

obligatory message to convert this to a thread.

muted raft
#

You could use a dictionary to hold each state. private Dictionary<Type, State> states;. Whenever you want to know if a state contains reference to another states simply iterate the dictionary.

You could create a virtual function public bool HasState(Type type) and each state define if it contains or not that state with this type.

#

If you want to use Generic you could do,

private Dictionary<Type, State> references = new Dictionary<Type, State>();

public bool HasState<T>() 
{
  return references.ContainsKey(typeof(T));
}
#

Alternatively, if you really, really do not want to use any of those approach you can use Reflection to inspect the content of the class. Not really something I would suggest doing over and over, but it can be justify in a initialization scenario.

shut elbow
umbral crest
#

@muted raft Thanks for the suggestion. I think this will work for my purposes.

@Anikki This looks like a great resource. I appreciate the link!

umbral crest
# muted raft You could use a dictionary to hold each state. `private Dictionary<Type, State> ...

Hello Again Simferoce,
I tried to implement a dictionary using the line you created.

I have the core class:

public class Core : NetworkBehaviour
{
    public Dictionary<Type, State> childStatesDictionary = new Dictionary<Type, State>();

    //Everything else
}

I also have OperateCannon which derives from PlayerState which derives from State.

public class OperateCannon : PlayerState

Finally, I have class that tries to pull a reference from the childStatesDictionary with this line:

OperateCannon operateCannon = core.childStatesDictionary[OperateCannon];

When I write this line, I get an error under OperateCannon that says this:

#

Any thoughts on what I'm doing wrong?