Stats in Unity - How I do it!

preview_player
Показать описание
Every video game needs stats for players, enemies, npcs, or weapons and armor. How you structure stats for your Unity project can make things easier or harder... So I thought I'd share how I'm doing it.

Video Links
************

Support the Channel!
**********************

Video Gear List (Amazon Affiliate)
***********************************

Other Links
************

Timestamps
*************
0:00 Intro

#GameDevelopment #Unity3D #indiedeveloper
Рекомендации по теме
Комментарии
Автор

I had a thought to basically have a stat modifier stack, which is a list of stat modifier commands. You have your base stats, set in stone and serialised in the inspector. Then as buffs/debuffs are added the object it they're added to that stats modifier stack. Each time a stat is read it's pulled through the modifier stack to produce the actual value. Added benefit is it also stores which modifications have been applied to the state. Of course you would have a quick read buffer for the calculated stat when no recalc is required.

simonnordon
Автор

Your videos are so valuable to me. I'm a decent coder, but I don't spend that much time researching coding solutions. I'll generally just use the knowledge I have to hack together a solution (that isn't necessarily the best). Every time my hacked together solution starts to buckle, I'll scroll through your videos to see if there is a better way, and usually there is. Your coding patterns videos and tutorials are some of the most valuable programming content on YouTube. Looking forward to your future content.

richardrothkugel
Автор

How I ended up here? Just like how you imagined, looking up how other people do it - though I'm pleasantly surprised you came to the same conclusion I did: Dictionaries. Great stuff, looking forward to the next video you teased here that'll include the problems you had with the ScriptableObjects and your upgrade system!

EEEdoman
Автор

Hey, just writing a comment here on a more recent video, I was checking the observer pattern video and it was really clear. I might check this one later because I'm interested about it

Thanks for explaining everything so clearly !

Daeniak
Автор

What I have also added to my statvalues:
- unit (KG, HP, XP)
- label for display
- value limit [0..100] - but yes, what to do if I upgrade my hero's shield, how to raise the upper limit? needs a solution
- valuechanged event - useful to change display scores/stats or check for ... death or level up events.

halivudestevez
Автор

Good info on SOs, thanks. Looking fwd on how you tackled the "CodeMonkey" problem.

siklus
Автор

I use a combination of scriptableobjects and systems that consume them to sort of achieve instance-based entity-component-system architecture. each instance of unit (say, enemies) start with same stats, then different systems consume them (health, enemyMovement, targetDetection etc.) to monitor and upkeep instance-based values for each of these. It's not perfect but it's working.

windwalkerrangerdm
Автор

i like the way you cut the video with a slight "One Wheel" pause.

troyna
Автор

Good job and thank for sharing! I am writing a stat system for a game now just like you I am doing a little research to see what is our there and that is how I found your video.
If I may say, the one thing I did not like is when you used an ENUM for the Stat Types. I even saw that you added a couple of "Unused slots" in the enum in case you needed yo squeeze in more stats in the future. Personally I do not like to use ENUMs for things that may change in the future. When that is the case specifically in Unity I either set a arbitrary value to the enum items like 100, 200, 300, this way is very easy to squeeze in other values in the middle without others losing their prior values. Or even better, since you are already using Scriptable Objects here, you could also create a StatTypeSO that just defines the different stat types. This way you also get the benefit of being able to create new stat types entirely in the editor without having to refactor any code and add items to an ENUM.

Either way, good video! Subscribed!

thepolyglotprogrammer
Автор

In get method is best to throw an error. Not gently show message - it will be ignored with time. This thing need to blow up early. Also it can be written in four lines 'if (Try) \n return ...; \n return 0;' else are bad. In our project we name method with "Get" - that will throw error if it can't find a thing. And we call method with "Find" if it can't find and silently return default value.

Also there are a problem, that you need to diffrentiate a static data and real data. You made a point in the end of the video about it, but it need to be more explicitly. You have two version of data - static - that never changes between run, and real data - that may change in run. Static data is your database, real data is your gameplay data. Before use, you may wan't to convert static data to real data. Point is that static data can be in other format - like in yours it is a some kind of dictionary in scriptable object, but real data can be anything realy. May be fields in class, objects from database (sprites/prefabs etc). So it will need to be initialized.

We store our data in components, because we use ECS pattern with Entitas and this is just a convinient way.

Nice video. Subscribbed :)

WeslomPo
Автор

Scriptable object can be used as a short save option, like i make pick a character and i save this choice on a scriptable object, in every scene they share this scriptable object, no need to save this data if it's for one run. or temporary

Ryöken
Автор

I love scriptable objects also. For a Genetic AI unity project I have been making I couldn't figure out how to use it. I needed new stats that is a random value of two other game objects stats then a random chance of changing + or -. I was not able to figure out how to make a new scriptable object while in game. Only know in editor. So used stats that were part of the movement of the AI. Your videos are great. Just found your channel and used your RTS camera for this project.

neilfosteronly
Автор

there is no SerializedScriptableObject in unity, my Dictionary is not displayed in inspector

USSR-Lenin-Stalin-Forever
Автор

I don't really agree with you on the "SO's should not be used as a save system" statement. Serializing class based data as a save system is basically the same as serializing SO data, but SO's give you the option to give access to said data to a bunch of different places simultaneously like you mentioned.

I've been using SO's as a framework for tying together systems using shared data and even events/signals for a while now in various client projects and I love the flexibility it gives you.

With some special parsing I even use it for practically seamless streaming and storing said data to the cloud.

I'd love to hear your thought though

LuukDomhof
Автор

Hey, around 3:25 onward you talk about if you upgrade your tower, all towers can be affected easily because they all use the same scriptable object. My question here is how? Are you changing your scriptable object's data at runtime (which I hear you typically don't want to do?) and polling it from the towers? Or what are you doing here to propagate the upgrades to all of your towers?

MalikenGD
Автор

Hi, I have a question. Where I see the source code of this video to be able to follow along better?. Im unable to find the SerializedScriptableObject part

sebastianecheverry
Автор

The upgrade system at 1:24 looks very cool, is there a video where it is being built?

kucirulz
Автор

I was using SO's for upgrading my stats & so that i can show them easily in UI just by referencing the data. But this caused me requirement to reset values every time i start the game meaning setting it to the default values too. But that caused to have "a lot of" values for defaults & currents.

TheKrckeR
Автор

SerializedScriptableObject is a class provided by Odin inspector? I wonder how did you provided an enum in the Inspector

LuRybz
Автор

i am having an issue where I used your code for the stats class, I copied your code exactly so that I could edit and alter it where needed but decided to just run a test(with all the upgrade information that is used in the next video mostly commented out!) and for some reason I am getting an error I cant seem to figure out where its not finding the namespace Stat which i find

RK-djwf