#Trying to pass a derived class to gameobject and access its props

1 messages · Page 1 of 1 (latest)

small zodiac
#

Hello everyone, I have a question that's been troubling me :

In Unity I have 2 scripts : Char1Specs and Char2Specs that both derive from an empty class CharacterSpecs.

He are the 3 scripts :

public class CharacterSpecs : MonoBehaviour {
}
public class Char1Specs : CharacterSpecs {
  public const string NAME = "char1";
  public const float GROUND_ACCEL = 2.0f;
}
public class Char2Specs : CharacterSpecs {
  public const string NAME = "char2";
  public const float MAX_SPEED = 10.0f;
}

I have 2 GameObjects called Char1 and Char2 which respectively have the Char1Specs and Char2Specs scripts attached to them. They also both have a PlayerManager script attached to them.

The PlayerManager script has a public variable of type CharacterSpecs to which I attach either Char1 or Char2 depending on which GameObject the PlayerManager script is attached to.

Here is the PlayerManager script :

public class PlayerManager : MonoBehaviour {
  public CharacterSpecs character;
}

And then there's another script called PlayerPhysics :

public class PlayerPhysics : MonoBehaviour {
  public PlayerManager PM;
  private void Move() { Debug.Log(PM.character.NAME) } // Doesn't work
}

How can I make it so that if I pass Char1Specs to the PlayerManager I can have access to his props in PM.character, and same for Char2Specs ?

My goal is to have something super clean like that

Thanks !

civic oar
#

Please use codeblocks!

#

[]cb

misty folioBOT
#

Use codeblocks to send code in a message!

To make a codeblock, surround your code with ```
To use C# syntax highlighting add cs after the three back ticks.

For example:
```cs
Console.WriteLine("Hello World");
```

Produces:

Console.WriteLine("Hello World");

To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.

small zodiac
icy sun
#

You can't directly get access to a child class's properties by referencing the base class.
Inheritance only works the other way around there.

Though you have two options:

  1. Declare the property in question in the base class (so it will be shared across all child classes and can potentially be overridden)
  2. Cast the base class reference you have to the desired child class, so you can access all properties declared in the child class. Note that this may fail if the referenced instance is not of the expected child class. Though this can easily be checked.
small zodiac
#

So what I want to do is impossible ?

icy sun
#

Did you read my entire message?
Because I listed two ways you can achieve what you are trying to do. It just won't work like you are doing it right now.

small zodiac
icy sun
#

Ah
Sorry, didn't see that your members were constant. In that case, inheritance won't really work, as you can't override constants and they are tied to the type.
But if you want different values for different child classes to be returned by the same property, that's the exact case I mentioned in 1.

But with constants, it pretty much is impossible.

small zodiac
#

How can 1 work ? I'm sorry I don't understand

#

I would need to override it, but when ?

civic oar
#

Wait why don't you just move the fields to the base class, make them a property and mark as protected set?

small zodiac
#

That wouldn't do what I want

#

Which I'm starting to believe is impossible

#

:/

#

Basically, I want PlayerPhysics to know which Specs were passed to PlayerManager

#

If it's Char1Specs, "character" will have the NAME and GROUND_ACCEL props, if it's Char2Specs, it will have NAME and MAX_SPEED

civic oar
#

I mean super convulted but you could just make each one of them their own "component" and then just iterate through all components

small zodiac
#

That's pretty heavy, which is what I wanted to avoid

#

^^

civic oar
#

Super not efficient especially as you'd essentially just be turning a bunch of primitives into objects but yeah I really don't see a need for PlayerManager to need to know what props are available

small zodiac
#

So that I can use the right props in PlayerPhysics

#

If it's Char1 ? Then NAME should be "char1" etc

civic oar
#

But why do the physics need to change? Just use a statemachine and change the behaviour of PlayerPhysics based on the PlayerManagers current "state"

small zodiac
#

I have several characters with their own physics

civic oar
#

What you're doing is possible but extremely anti-pattern and it's going to lead to a bunch of bloat

small zodiac
#

I want the move method to accelerate the characters based on their own acceleration etc

civic oar
#

So why not just let the move method decide how to move that player? Again a state machine would work here with some abstraction

desert python
#

This sound like a job for... reflection

civic oar
#

Just have different MoveStates which, based on the current character, handle the movement differently
Then you can just use one class that contains the properties you need per player

#

They said they wanted performance nt 😂

desert python
#

LOL