- Home /
How to make a class selection system?
I'm trying to make a class selecting system (warrior, archer, mage, etc) where each class has 2-3 abilites and some stats, but I'm a bit stuck.
So I decided to start with the button that you need to press which will set you to the class.
public class SelectClassButton : MonoBehaviour
{
public string classToGive; //string is temporary
void Awake()
{
var button = GetComponent<Button>();
if (button)
button.onClick.AddListener(() => OnButtonPressed());
}
void OnButtonPressed()
{
Debug.Log("UIButtonBase::OnButtonPressed()");
}
}
Now I have 2 scripts for the player one for the camera and the other for all the movement stuff. So since the classes give abilites that need to be activated through input it made sense to handle the input in the movement code, but to do this I needed a reference to the class they selected and so I made a variable called playerClass and a method SetClass and used events to set everything up, but now my player movement code also handles class stuff which IMO doesn't make sense so I probably wanna break things up, but that's where I get stuck because I can't think of any good methods to do it I probably wanna use interfaces or an abstract class, but can't seem to figure out how.
Sorry if this was a bit lengthy hopefully it made sense.
Answer by landings · May 19 at 03:19 AM
Class selection is just an initialization process. Even if you want class switching, it's just a re-initialization.
A character doesn't have to hold a reference to its class initialization code all the time. He just needs to preserve the initialization results like class name, hp, inventory and ability list.
You need a Player
script that holds your numeric values, inventory list and ability list. These can be get or set with an information pack PlayerData
.
Now you just need a static function that reads current player data (optional) and returns new player data:
PlayerData GetPlayerClassInitializationData(string className, PlayerData current = null)
.
This function is just giving information. The information is set to Player
by Input
.
Input and movement should be separated. Because AI, network or even abilities can also cause movements.
Your answer
![](https://koobas.hobune.stream/wayback/20220613060727im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUITexture not corresponding with position 1 Answer
manipulate fields of class 1 Answer
Show Image when button is highlighted 1 Answer
Some JavaScript questions 1 Answer
Accessing interface in a derived class through the parent class 0 Answers