Changing Ability State on runtime

Sem

New member
Hello, i've been trying to trigger a state change on one of the abilities on my character during runtime thru a script, but so far it doesn't change to the specified state.

I have configured the states as follows:
1669106718319.png


on my script, i tried changing the state this way:
1669107216258.png

unfortunately the state stuck in the 'Normal' state during gameplay.
1669107353573.png

May i know, how do i properly change the ability state?
 

eg. : StateManager.SetState(m_Character, "MyState", true);

So if you are on a script outside of the ability system you will want a reference to the GameObject on which the state is to be set - likely the player (m_Character) in this case.

your script will need to be using Opsive.UltimateCharacterController.StateSystem
 
  • Like
Reactions: Sem

eg. : StateManager.SetState(m_Character, "MyState", true);

So if you are on a script outside of the ability system you will want a reference to the GameObject on which the state is to be set - likely the player (m_Character) in this case.

your script will need to be using Opsive.UltimateCharacterController.StateSystem
Hello DankP3, the script that accesses the jumpAbility.SetState() were also inheriting from Ability and attached to the character

1669113187289.png

here's the full snippet of the code:

C#:
using System;
using System.Collections.Generic;
using ControlFreak2;
using Opsive.Shared.Game;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Character.Abilities;
using UnityEngine;

public class SkillDoping : Ability
{
    public float dopingDuration = 5f;
    public float cooldownDuration = 7f;

    private UltimateCharacterLocomotion characterLocomotion;
    public ScheduledEventBase scheduledCooldownEvent;
    public ScheduledEventBase scheduledDopingEvent;
    private Jump jumpAbility;
    private bool isCoolingDown;

    private List<Action<bool>> OnSkillActive = new List<Action<bool>>();

    // Start is called before the first frame update
    public override void Start()
    {
        Debug.Log("(SkillDoping) Start");
        
        characterLocomotion = m_GameObject.GetComponent<UltimateCharacterLocomotion>();
        jumpAbility = characterLocomotion.GetAbility<Jump>();
        isCoolingDown = false;

        Debug.Log("(SkillDoping) jump ability has these states: ");
        foreach (var state in jumpAbility.States)
            Debug.Log(" - " + state.Name);
        
        base.Start();
    }

    public override bool CanStartAbility()
    {
        return !isCoolingDown;
    }

    public override bool AbilityWillStart()
    {
        if (ReferenceEquals(characterLocomotion, null))
        {
            Debug.Log("(SkillDoping) characterLocomotion is null");
            return false;
        }
        
        if (ReferenceEquals(jumpAbility, null))
        {
            Debug.Log("(SkillDoping) jumpAbility is null");
            return false;
        }
        
        return true;
    }

    protected override void AbilityStarted()
    {
        Debug.Log("(SkillDoping) AbilityStarted");
        
        isCoolingDown = true;
        jumpAbility.SetState("Doping", true);
        jumpAbility.SetState("Normal", false);

        Debug.Log("(SkillDoping) jumpAbility state = " + jumpAbility.State);

        scheduledDopingEvent = SchedulerBase.Schedule(dopingDuration, () =>
        {
            jumpAbility.SetState("Normal", true);
            jumpAbility.SetState("Doping", false);
            
            Debug.Log("(SkillDoping) skill disabled, state is now = " + jumpAbility.State);
        });
        
        scheduledCooldownEvent = SchedulerBase.Schedule(cooldownDuration, () =>
        {
            isCoolingDown = false;
        });
        
        base.AbilityStarted();
    }
    
    public void AddOnSkillActiveListener(Action<bool> listener)
    {
        OnSkillActive.Add(listener);
    }
    
    public void RemoveOnSkillActiveListener(Action<bool> listener)
    {
        OnSkillActive.Remove(listener);
    }

    // // Update is called once per frame
    // void Update()
    // {
    //     if(CF2Input.GetButton("Doping Skill"))
    //         Debug.Log("Doping Skill Button Pressed");
    // }
}

all it does is it reference the Jump ability that's plugged onto the character and change the state.
I've made sure to change the other state value's to a bit extreme number so it would be noticable when the state changes
1669113522803.png

but unfortunately it doesn't seems to change it
 
As Dank mentioned you should use the StateManager to change the state instead of calling SetState on the ability.
 
  • Like
Reactions: Sem
Hello Justin, i've tried using the StateManager.SetState and at first it doesn't appear to have fixed it.

However, i tried messing around with the Blocked By setting on one of the state
1669115606002.png

and it appears to be working now ?, it now seems to work on either
jumpAbility.SetState() and StateManager.SetState().

thanks :)
 
Top