0
\$\begingroup\$

I'm trying to make a class selection system for learning and I've decided to use scriptable objects for the classes since it seems like scriptable objects are good for that(?) however I start to get confused when it comes to setting the class and using the values.

This is my scriptable object for a class.

[CreateAssetMenu(fileName = "New Class", menuName = "Classes/New Class")]
public class BaseClass : ScriptableObject
{
    public float health;
    public float walkSpeed;
    public float fallSpeed;
    public float jumpForce;
    
    public string displayName;

    public List<BaseAbility> abilites;
}

I currently have 2 instances one is called "No Class" which is just default values for when no class is selected (which I dragged into the Player component so it's there when the game starts) and then I have a Warrior one.

This is my player script which uses an event to set the players class when they press a button

public class Player : MonoBehaviour
{
    public BaseClass selectedClass;

    void Start()
    {
        ClassSelectButton.OnClassSelected += SetPlayerClass;
    }

    void SetPlayerClass(BaseClass newClass)
    {
        selectedClass = newClass;
    }
}

And here's where I start to get confused. So I set the players class to the new class in the Player script, but now I need to get the new values in the scripts that need them like my movement script so my solution was to just use the same event.

    public BaseClass playerClass;

    void Start()
    {
       playerClass = GetComponent<Player>().playerClass;
        ClassSelectButton.OnClassSelected += (BaseClass newClass) => { playerClass = newClass; };
    }

    void FixedUpdate()
    {
        rigidBody.MovePosition(transform.position + moveDirection * playerClass.walkSpeed * Time.fixedDeltaTime);   
    }

Although this works I feel like it's not a good approach (perhaps I'm wrong though) so I'm trying to figure out other methods I can do.

\$\endgroup\$
2
  • \$\begingroup\$ Have you tried to run the exact piece of code you posted without adding the listener in the movement script? Because it should get the same class over the reference you have to the player \$\endgroup\$
    – Zibelas
    May 27 at 17:17
  • \$\begingroup\$ I have done it without the listener, however, I think the issue I encountered was when it gets changed to a new SO (select a different class) it still uses the old one. \$\endgroup\$ May 27 at 18:04

0

You must log in to answer this question.

Browse other questions tagged .