+1
Answered

Modifying stats on player init

Mitch Treece 5 months ago in Stats updated by Marti (Lead Developer) 5 months ago 6

Hey Guys,

Just started diving into GameCreator (+ Stats), seems like a good foundation to build on so far! Was wondering what the best way would be to modify a player's stats on initialization.

I have "base" player object that gets fed data when spawned. This data dictates the player appearance, speed, and other attributes. I want to also add various buffs (or-debuffs) to this data and modify the player object's stat component when data gets fed in. For example, imagine the player data contains a hero class-archetype. I would want to modify base health, speed, etc based on that type.

Assuming I have a list of unique ID's (status effects / modifiers) on the data object, how can I load the effect assets and apply them to the player via code? Also, is there a better way to reference these in the editor besides using the unique IDs? Ideally, I'd be able to have a serialized array field on the data object that I can add the effect scriptable objects directly to.

Cheers,

Mitch

Unity version:
2019.3.7f1
Game Creator version:
1.1.6
Answered

I wouldn't overcomplicate things. The easiest way to do this would be to have an Actions object with all the modifications made based on the class. For example, let's say our game has 2 classes: Warrior and Mage. Warrior adds +2 to strength and -1 to intelligence and Mage adds +2 to intelligence and -1 to strength.

So, we create two Actions objects in the scene. We'll call the first one WarriorClass and will have the following actions:

- Change Stat => Add +2 STR to Player

- Change Stat => Add -1 INT to Player

And we do the same for the MageClass object, but setting -1 to STR and +2 to INT.


Now here comes the trick. You create a prefab out of these two objects. 


If you store the selected class in a Global Variable, all you have to do is use a Trigger "On Start" that calls a Condition object that checks which is the class of the player.


If it's a mage, then use the Action "Call Actions" and drop in the prefab MageClass. This will execute the previous actions and add +2 to INT and -1 to strength. If the player is a warrior, do the same, but pass in the WarriorClass prefab.


This is a very good example, and I'm taking note for a future tutorial. Cheers!

Hey Marti,

Thanks for the reply! I get what you’re saying, and I think I could definitely get this to work. My project is networked however, and I need to tightly control some of the stat transactions. Even if it is more complicated, is there anyway to achieve the same results from a script.

I want to leverage Actions and all of GCs systems as much as possible. But in some situations (like player spawning) is like to handle this manually.


Glad my question might help inspire some tutorials! I’m sure other people have wondered something similar :)


Cheers,
Mitch

Also, I definitely want these stat changes to be tied to a stat modifier / status effect if possible (think class buff). You're suggesting just applying the changes to the stats directly via actions. This works, but I would like to do something like I mentioned above if I can.

Thanks again!

I think it should work just fine. Stat's "on change" triggers will be fired every time a stat changes its value. Because a stat can be tied to other stat values, every time you modify a stat, all others will update and see if their value has changed due to to that. If that's the case, they will fire the "on change" event.

That includes using Stat Modifiers (which is another action).

Gotcha,

But can any of this be invoked via script, instead of action object in the scene?

Sure. The easiest wway is to copy the code from Actions/Triggers. Check out the IgniterAttrChange.cs Trigger script to know how Triggers do listen to attribute changes.