How do I deal with an optional parameter?

I am attempting to use the AnimationController.setAnimation() function but I am getting an error on the second argument.

ERROR: This function parameter expects a value of type tuple([]keyframe_delta,?Mode:animation_mode), but this argument is an incompatible value of type tuple([]keyframe_delta,animation_mode).

  • tuple([]keyframe_delta,?Mode:animation_mode)
  • tuple([]keyframe_delta,animation_mode)

How do I deal with an (what I assume) optional parameter?

This is how i am calling it.
AnimationController.SetAnimation(Keyframe, animation_mode.OneShot)

You need to put your value inside option{}. for example consider the function:
Foo(Val: ?float): void = {}
In that case, when you call it you’ll write it this way:
Foo(option{5.7}) and not Foo(5.7)

Thank you, now it likes the optional, but still doesn’t like the “Mode:”

  • Expects tuple([]keyframe_delta,?Mode:animation_mode)
  • Given tuple([]keyframe_delta,?animation_mode)

AnimationController.SetAnimation(Keyframe, option{animation_mode.OneShot})

    animation_mode<native><public> := enum:
        
        OneShot
      
        PingPong
        
        Loop
1 Like

It seems that the second parameter of this function is an optional parameter. To fill an an optional parameter in a function, you’ll need to write it this way:
?Mode:= option{animation_mode.OneShot}

A brief example is this:
Print(“Hello, world!”, ?Duration:= 0.35)
Optional parameters in functions indicates that this parameter has an already assigned value and can be overridden by user whenever needed unlike required parameters

2 Likes

Just to expand on that, you need to use the optional parameters full exact name (with a ‘?’ before it) as written in the function before the default value.
I wrote some of these myself and struggled hard before I figured that out. Just figured I’d mention it in case you wanted to implement this yourself.

Example:

AnimPlayerState(Agent:agent, ?SleepTime:float=0.0, ?Turning:logic=false, ?Falling:logic=false, ?Vulnerability:logic=true)<suspends>:void=
        if (Fort:=Agent.GetFortCharacter[]):
            Fort.SetVulnerability(Vulnerability)
            Fort.Hide()
            Fort.PutInStasis(stasis_args{AllowTurning := Turning, AllowFalling := Falling, AllowEmotes := false})
        Sleep(SleepTime)