Home   Archive   Permalink



Variables not working in View(R3)

I'm very confused... After all I've learned about R3, it is clear that dealing with variables should be very simple. Certainly no more complicated than in other languages. Yet I'm finding that even a simple incrementation such as:
    
cnt: cnt + 1
    
does not work, at least not within a view anyway. I have to assume that there's some secret that I've not come across yet... So I set up a file specifically to test this out, with nothing else going on... And I found something strange. In my original program where I first encountered the issue, I was getting an error which said 'cannot use add on none! value'
    
But now, with the new file I'm getting 'Cannot parse the GUI dialect at: cnt + 1'... Here's the code:
    
R E B O L
[
Title: "Variable Experiment"
]
load-gui
    
cnt: 0
alert to-string cnt
    
view layout [
    cnt: cnt + 1
    alert to-string cnt
]
    
This code does display the first alert with the value of zero, but then after closing that alert, I get the error.
    
I don't understand why this could possibly be happening. If anyone could shed some light on this, I'd appreciate it. Thanks!

posted by:   mothdragon       21-Aug-2014/21:06:30-7:00



Hi--the LAYOUT function interprets its block argument as a GUI dialect and does not evaluate as with other code. It parses this dialect and returns a FACE object that is in turn displayed by VIEW. There are mechanisms for evaluating code within the GUI dialect, but in your example above it's not clear that the GUI dialect is what you're after as you have not defined any GUI elements.
    
You could try the following:
    
view layout [
     do [
         cnt: cnt + 1
         alert to string! cnt
     ]
]
    
Or
    
view layout [
     title (join "Count: " cnt: cnt + 1)
]

posted by:   Chris       25-Aug-2014/17:41:44-7:00



on-action blocks act as closures, so when you do
    
cnt: cnt + 1
    
cnt is automatically initialised to `none as all set-words are in a closure or function body.
    
The cnt in the on-action block is different from the one you first initialised.
    
If you want to refer to the one outside the block you can do this. First, grab the context as in this line
    
https://github.com/gchiu/RSOChat/blob/master/rsochat.r3#L87
    
and then refer to that context like here
https://github.com/gchiu/RSOChat/blob/master/rsochat.r3#L839
    
So you would do
    
u/cnt: u/cnt + 1
    


posted by:   Graham       25-Aug-2014/21:31:27-7:00



And finally, if you don't use a set-word inside the on-action body, you'll be able to access variables defined in the user context like this.
    
R E B O L []
cnt: 0
    
view [
    button "Click me" on-action [ print ++ cnt ]
]
    
Since you're not using a set-word, the r3-gui dialect parser won't make it local to the on-action block.

posted by:   Graham       25-Aug-2014/21:51:46-7:00