State Configuration at runtime using CharacterBuilder

Ace

Member
I'm using CharacterBuilder.BuildCharacter() to setup a character at runtime. This is working great but I would like to use a State Configuration( like the ThirdPersonControllerDemoStateConfiguration) to setup the character to be preconfigured with already defined values, (this is possible using the Character Manager/ editor window). Is there a way to do the same using the CharacterBuilder? or is there a way to add a Configuration once your character is setup and then apply the defined values?
 
StateConfiguration has a method "AddStatesToGameObject", which you can use to add all the states from the configuration to the character gameobject once it's been created.
 
Thanks that worked but I'm still struggling with one aspect of the state configuration. I was under the impression that it would allow me to configure abilities. But it doesn't look like it does that. I could try copping the component values from the Nolan character in the demo scene but I would need to do that in code and I'm not sure if that is a good solution.

Does opsive have any good solutions for coping over abilities or component values?
 
Yeah the state system is separate from the abilities unfortunately - you could have "default" states for each of the abilities that sets their parameters to your desired values, but that seems a tad convoluted to me. Unfortunately there's no direct way to just copy over all properties from one character's abilities to another as far as I know, so you'd need to copy them over through code.

What's the functionality of this for, by the way? Prefab variants might be of interest depending on what you're trying to achieve.
 
I was able to figure it out. For anyone trying to add abilities at runtime using a StateConfiguration here is how you do it.

You can copy over ability values using a StateConfiguration in this example I'm using the UltimateCharacterControllerDemoStateConfiguration which can be found here Opsive\UltimateCharacterController\Demo\Presets\

if you click on the state configuration you will see all of the abilities that are associated with it
1608430321603.png

you can loop through the stateConfiguration and get the presets and then create the ability and set its values using the preset.
sample code:
C#:
UltimateCharacterLocomotion locomotion = avatar.GetComponent<UltimateCharacterLocomotion>();

foreach (var profiles in stateConfiguration.Profiles)
{
    foreach (var state in profiles.StateElements)
    {
        // Each preset will be setup for some type of ability, you will need to figure out
        // which ability the preset is, you can do this using state.Preset.Data.ObjectType
        
        // When you know the ability you can create it and then set its values.
        // I used a switch statement to handle the different types of abilities
        Ability ability = AbilityBuilder.AddAbility(locomotion, typeof(Drive));
        state.Preset.Initialize(ability, MemberVisibility.Public);
        state.Preset.ApplyValues();
    }
}

Hope this helps, if anyone else runs into this problem and need more info just let me know
 
@Ace can you show the part of the code that shows what is "stateConfiguration" plz? I'm trying to add States to my Locomotion runtime, since the character is not being animated at all (when I create the character via CharacterBuilder). Thanks
 
Top