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.
#How to structure players in game development
1 messages · Page 1 of 1 (latest)
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.
alright thats great. you said perhaps might modify them, what situations would you not want them modifible?
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).
ah thats good idea actually to validate any changes in the script so that you don't have to do it elsewhere multiple times. finally, what would be the simplest way to access this script. i use GetComponent<>() but is there a way I can simplify this so I can just access the script such as using a type of variable but im worried that might make an instance of that class at that specific time
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.
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
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.
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
still getting used to what is passed by reference or passed by value
classes,interfaces,records anddelegates are passed by reference.
structs (and by extensionenums) 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 inAwake. It needs to be declared at class level, otherwise other methods won't have access to it (and its value will be lost whenAwakeexits).
sorry to come back to this a bit later. im working on my stamina system and when im moving i dont want the stamina to raise and when im doing attacks i dont want to raise the stamina. i understand how to not raise the stamina when doing attacks or moving but since they are in different scripts, im a bit confused how I can make sure that the stamina doesnt raise when either is going on
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.
what do you mean by objects? i plan on having this stamina regen script in the stats since it will be using stuff from both the movement and combat but if this wouldnt be the best method i will change it