Strange behavior of function optional parameter

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

I worry there could be another issue happening as I believe your optional logic param should work as written.

I made this test to confirm, BTW I did not know we could use curly brace formatting! And I am all for it! Thank you for showing me that :slight_smile:

    Testing():void={
        Logger.Print("Testing Start")
        OptionArgFunc()
        Logger.Print("Testing 2")
        OptionArgFunc(?MaybeForce:=true)
        Logger.Print("Testing 3")
        OptionArgFunc(?MaybeForce:=false)
    }

    OptionArgFunc(?MaybeForce:logic=false):void={
        if (MaybeForce?):
            Logger.Print("1: Force")
        else:
            Logger.Print("1: No Force")

        if (MaybeForce=true):
            Logger.Print("2: True")
        else:
            Logger.Print("2: False")

        if (MaybeForce=true)
        {
            Logger.Print("3: True")
        }
        else
        {
            Logger.Print("3: False")
        }
    }

The log output ends up being as we expect for all test cases (1,2,3).

You might want to confirm some of your base assumptions, restart your applications if you haven’t already etc.
I didn’t try to test your exact code, but I was curious about using an optional logic param.
Good luck figuring it out!