#How to structure players in game development

1 messages · Page 1 of 1 (latest)

lone sky
#

Generally, how should i structure like my character in unity. I seperated out my player's combat and movement scripts but im struggling to see a good way to store my stamina and health since both my combat and movement scripts use these variables and im not sure if i should have a seperate script or maybe use an oobject or just store it in a movement/combat script.

carmine pollen
#

For things like health, stamina, armor, etc., I would make a Stats script, that only exists to hold those values (and perhaps allows modifying them). That would decouple them from other components that need to fulfil specific mechanics and therefor prevent one mechanic relying on another one existing, only for it to work.

lone sky
carmine pollen
#

Some stats (like the limits for health, stamina, ...) may be fixed. That totally depends on your game though.
And perhaps I should rather have phrased it "provides ways to modify those values". As in, the underlying variables cannot be modified directly but any changes are validated first (for example so health does not drop below 0).

lone sky
carmine pollen
#

You could just use GetComponent once and store its result in a variable. Or make said variable a serialized field and assign the reference manually in the inspector.

such as using a type of variable but im worried that might make an instance of that class at that specific time
I am not sure what you mean with that.

lone sky
#

sorry i understand instance is used in game dev but not sure the meaning hope that wasnt confusing. i meant if i created a variable of the script i was worried that if I changed the script in the combat script because you wouldnt actually be changing the original script but a duplicate. for example that it wouldnt change like a duplicated version of the script (so when you do variable = GetComponent<script>(), the variable would be a duplicate of the script at that time)

#

does that make anymore sense

carmine pollen
#

The explanation does make a bit more sense.
The assumption does not. Components are always classes. Classes are always passed by reference, they will never be copied (unless you specifically write/call a method to copy them).

#

The latter is actually what Unity's Instantiate does; it creates a deep copy of a given Unity object and all its serialized properties.

lone sky
#

ah sorry, im still getting used to what is passed by reference or passed by value. thank you very much that does help a lot. ill write my stats script and then just create a variable of it in the awake method

carmine pollen
#

still getting used to what is passed by reference or passed by value
classes, interfaces, records and delegates are passed by reference.
structs (and by extension enums) are passed by value.

thank you very much that does help a lot
You are welcome! 😄

ill write my stats script and then just create a variable of it in the awake method
Make sure you only assign the variable in Awake. It needs to be declared at class level, otherwise other methods won't have access to it (and its value will be lost when Awake exits).

lone sky
carmine pollen
#

You will need to find some way to determine whether any systems are blocking stamina regeneration.
A common approach for this would be to make a list of objects that you add any "blocking" mechanic to, and just don't regenerate while this list contains one or more entries.

lone sky