Hi,
I’m recently trying to use optional parameter on a function in my project.
The function with optional parameter are:
UpdateNpcState(NewState:npc_state, ?ForceAction: logic = false): void = {
if(ForceAction = true){
Print("Force the action")
} else {
Print("Don't force the action")
}
if(NewState <> NpcState or ForceAction = true){
set NpcState = NewState
Print("Update the state of the NPC to " + NewState.ToString())
case(NpcState) {
npc_state.Attacking => {
spawn {
EnterInAttackingStateMode()
}
}
_ => {
Print("Other case")
}
}
}
(It the same behavior if I use if(ForceAction?)
)
It like, if we set a new state, execute corresponding code. In case if we trying to set a state who are the same as the current, do nothing, except if we use the optional param ForceAction to true.
EnterInAttackingStateMode :
EnterInAttackingStateMode()<suspends>:void= {
if(
CharacterToAttack := CurrentTarget.GetFortCharacter[],
){
if(CharacterToAttack.IsFortCharDead() = true){
Print("Target is dead")
UpdateNpcState(npc_state.Idle)
} else {
Print("Target is still alive")
Sleep(DelayBetweenAttack)
UpdateNpcState(npc_state.Attacking, ?ForceAction:= true)
}
} else {
Print("All need not found to attack")
UpdateNpcState(npc_state.Idle)
}
}
I had remove extra code to be easy to read. We check if the currentTarget are alive or not. If yes, we trying to re-set the same state, attacking, but using ForceAction to loop the attack state until it dead. If he dead, just go back in idle.
In my case, we are already in attackingState and trying to set it back
As you can see in the session log, it correctly reach the
UpdateNpcState(npc_state.Attacking, ?ForceAction := true)
but still found a false value for ForceAction, who are definitively set to true in the call.
It was working as well few days ago, and don’t know why it produce problem now.
Is someone know something about that? Maybe I’m missing some subtilities or using it wrong ?
If I set the param as no optional and set true/false at every call, it work as well.
(Sorry for some edit. I’ve currently set as non optional in my project so I had to set optional back in this text editor ahah )
Thank you, have a nice day