(Solved) Basic Question - How do I call Sleep() inside of a player function like a button interacted? "Suspends effect not allowed by its context."

11/28/22 - Solved!

Fixed! Thanks to @AxelCapek for the advice! Axel’s comment told me I can’t suspend a player function, but I can call a function inside of that player function. The function that I call can use the suspended effect, so Sleep() is ok there.

So below is my new fixed version of my color picker script, which works. The ChoseHue functions are the player functions triggered by the button press, and the StopAtHue functions are the suspends functions that are spawned.

    OnBegin<override>()<suspends>:void=
        # TODO: Replace this with your code
        Logger.Print("Device Started")
        ButtonHue1.InteractedWithEvent.Subscribe(ChoseHue1)
        ButtonHue2.InteractedWithEvent.Subscribe(ChoseHue2)
        ButtonHue3.InteractedWithEvent.Subscribe(ChoseHue3)

    ChoseHue1(Player:player):void=
        CinSeqHue.Play()
        spawn {StopAtHue1()}

    ChoseHue2(Player:player):void=
        CinSeqHue.Play()
        spawn {StopAtHue2()}

    ChoseHue3(Player:player):void=
        CinSeqHue.Play()
        spawn {StopAtHue3()}

    StopAtHue1()<suspends>:void=
        Sleep(1.0)
        CinSeqHue.Stop()

    StopAtHue2()<suspends>:void=
        Sleep(3.0)
        CinSeqHue.Stop()

    StopAtHue3()<suspends>:void=
        Sleep(4.0)
        CinSeqHue.Stop()

And for fun, here’s a video of the color picker mechanic in action. You can see how the Hue starts changing when I press the buttons, and stops after 1 second (landing on yellow), 3 seconds (blue), or 4 seconds (red). Obvi I’ve got a lot more tweaking to do but I’m happy with the proof of concept!

11/27/22 - Original Question
Hey all,

This is super basic, hopefully shouldn’t be hard to answer. :pray: :slightly_smiling_face:

How do I make it so that when a player presses a button, there is a pause (using Sleep) before the interacted with event begins? I know Sleep requires a suspends context, but it appears I can’t use : after (Player:player) ? I’m not great with terminology yet.

Error: “This invocation calls a function that has the ‘suspends’ effect, which is not allowed by its context.”

Here is the part of my script I have a problem with:

    # Function - OnBegin =========================================================
    OnBegin<override>()<suspends>:void=
        ButtonHueGreen.InteractedWithEvent.Subscribe(ChoseGreen)
        ButtonHueBlue.InteractedWithEvent.Subscribe(ChoseBlue)
        ButtonHuePurple.InteractedWithEvent.Subscribe(ChosePurple)

# Functions - Player Presses Button, Cinematic Sequence Starts, Pauses, and Stops on Chosen Color ====================================================
    ChoseGreen(Player:player):void=
        CinSeqHue.Play
        Sleep(0.2)
        CinSeqHue.Stop

    ChoseBlue(Player:player):void=
        CinSeqHue.Play
        Sleep(0.3)
        CinSeqHue.Stop

    ChosePurple(Player:player):void=
        CinSeqHue.Play
        Sleep(0.4)
        CinSeqHue.Stop

This is what the script is for:

In a flower-arranging minigame, I want players to be able to change the color of a flower mesh, by pressing buttons that correspond with their chosen color. So I set up a Level Sequence that cycles the hue parameter on my flower’s material throughout the rainbow until stopped. So, for example, when the player presses the Green button, I want to start the sequence, and stop it after .2 seconds - that’s when the material’s Hue parameter will have reached green.

I know there are other ways to do this but this is my first experiment. I’m trying to keep the number of sequencers low, and use parameters instead of providing a ton of different materials. Ultimately I want players to change the flower’s Hue, Saturation, Brightness, Patterns, etc. Lol I’m so cursed, all of my dream game ideas require me to code sandbox art tools for players. :laughing:

I’d really appreciate some advice! Thanks :pray: :smiling_face:

2 Likes

You cannot suspend a player function, instead call another function to stop the sequence with the suspended effect.

Example:

MyPlayerFunction(Player:player)void=
CineHue.Play
spawn {MySuspendedFunction()}

MySuspendedFunction()<suspends>:void=
Sleep(0.4)
CineHue.Stop
1 Like

Thank you so much for the super quick response, Axel! Trying this asap when I get a chance. Looks simple!