2

I want to set a variable in a Rebol 3 button handler (GUI) and use the value after the window is closed. How do I get the value outside of the view block? Here is an example code:

view [
    v_username:  field
    button "Submit" on-action [
        username: get-face v_username 
        close-window face
    ]
]

probe username

The result is "" regardless of the content of v_username.

Does it have to be 'declared' as a global variable? Should I get this value from a returned value of the view?

SoleSoul
  • 368
  • 2
  • 14
  • Did you set `username: ""` before the view? – kealist Jul 22 '13 at 20:58
  • I tried both with it and without it. The result is the same. – SoleSoul Jul 22 '13 at 21:00
  • If I don't have that, I get an error: `** Script error: username has no value ** Where: catch either either -apply- do ** Near: catch/quit either var [[do/next data var]] [data]` – kealist Jul 22 '13 at 21:01
  • Confirmed with another variable name. 'username' was probably used in a file I load before with 'do', that's why I didn't get the error. – SoleSoul Jul 22 '13 at 21:04

1 Answers1

2

The 'on-action block when invoked is wrapped in a function (Rebol function where set-words are assumed local to the function). You have a couple of options to work around this:

  1. Use an object to store the values in (set-paths are not bound within function):

    values: context [username: none]
    view [... on-action [values/username: get-face ...]]
    
  2. Use 'set to set the word. I find this a little less reliable as it's uncertain the context of the word you are setting:

    view [... on-action [set 'username get-face ...]]
    
  3. Though perhaps the best option is to keep in mind that the words you assign to styles are bound the context you're working in, so:

    view [username-field: field ...]
    username: get-face username-field
    
rgchris
  • 3,698
  • 19
  • 19
  • Thanks! I'm using the first option as you suggested and it works. I would love to know though, why does 1 work? because objects are being passed by reference? and what's the difference between "x: y" and "set 'x y"? – SoleSoul Jul 22 '13 at 21:20
  • I changed my code to use the third option. Who whould have thought that the face name is global and the variable is local. I assumed the opposite. – SoleSoul Jul 22 '13 at 21:28
  • 1
    When a `funct` is created, it collects `set-word!` types and creates a local context using them. When you reference a word within an object, you are using `set-path!` notation—this is not collected. – rgchris Jul 22 '13 at 21:34