Want to use variable that was maked other function.

I have a question of Verse Programming. (I’m a very beginner…)

I want to substitute a value in a variable when push a button_device.

like bellow,

@editable
Button1s : button_device = button_device{}

OnInteractedButton1s(Inplayer:agent):float=
    var SET_SLEEP :float = 1.0
    return SET_SLEEP

OnBegin<override>()<suspends>:void=

    Button1s.InteractedWithEvent.Subscribe(OnInteractedButton1s)

    loop:
        Sleep(SET_SLEEP)

But it indicate “red wavy line on SET_SLEEP”. (Unknown Identifier)

Please someone teach me how to work well !!

Thanks,

Hi,
The error you’re receiving has to do with scope. It means that the SET_SLEEP variable isn’t accessible where you want to access it.
You either have to make the SET_SLEEP variable in the main scope and simply update it in your OnInteractedButton1s function or you can catch the return value of that function and store in a new variable to use. The second option I’m not really sure if it’s possible in a “Subscribe” though.

There are several weird things about your code also, what exactly are you trying to achieve so I can give a good example of code? Feel free to send me a DM so we can chat.

PS: Great that you want to be a Verse developer :wink:

1 Like

Also if you want to loop when an user interacts with your button, this is not gonna work, because the OnBegin function will start looping before your player has interacted with button. You’ll either need to

  • spawn{} a <suspends> function from OnInteractedButton1s if you want all your players to have a seperate loop
  • Use Button1s.InteractedWithEvent.Await() instead, thus blocking the execution of the next lines if you want the button to be interated once

These are simple examples, but they could lead you somewhere, it all depends on what you’re trying to achieve!

2 Likes

Thanks for all your help!

It works well on bellow script.


OnInteractedButton1s( Agent : agent ):void=
    var SET_SLEEP : float = 1.0
    spawn:
        Main(SET_SLEEP)

Main( SLTime : float )<suspends> : void =
    loop:
        SET_TIme := SLTime
        Sleep(SET_TIme)

It needs to call function with “spawn”, and function needs specifier…
I can’t get all these necessity, I have to study more!!

Thanks!

1 Like

Thanks for your help!

I could work well by your advice.
It is same reply to Mapshot_Agency
so omit on this reply.

Thanks for your kind!

1 Like