Questions about global variable editing in a shape rule

If you're having trouble using Context Free or don't understand the language, ask for help here.

Moderators: MtnViewJohn, chris, mtnviewmark

Post Reply
phoebus16
Posts: 2
Joined: Wed Jan 04, 2012 5:58 pm

Questions about global variable editing in a shape rule

Post by phoebus16 »

Hi,

I am pretty new at using contextfree and I have a question:

I am using the following code:
startshape one

var=5

shape one{
var=var - 1
asquare[]
if (var) one[x 0.1 s 0.95]
}

shape asquare{
SQUARE
SQUARE[s 0.95 b 1 ]
}


I want to draw only 5 squares.
I use "var" variable to restrict further drawing of squares.
However the code draws limitless squares.
If I define "var=1" then only one square will be drawn, which is what is expected. That means that the expression "var=var - 1" works once but not again (like in the case of var=5).
Why this is happening? It seems like the variable is only temporalily edited in the "shape one" and then reset to its original value.

ps:(I suspect I am confused about the way the code is executed)

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Re: Questions about global variable editing in a shape rule

Post by MtnViewJohn »

Oh, I should make that clear. Context Free variables are not modifiable state. When you say

Code: Select all

var = var - 1
You are declaring a new var variable that is distinct from the global var variable. The local var variable will always be 4. Redeclaring a variable with the same name should probably be an error because it doesn't do what you think it does and what it actually does is not particularly useful.

You could use a natural parameter:

Code: Select all

shape one(natural count){
asquare[]
if (count) one(count -- 1)[x 0.1 s 0.95]
}

phoebus16
Posts: 2
Joined: Wed Jan 04, 2012 5:58 pm

Re: Questions about global variable editing in a shape rule

Post by phoebus16 »

Cool, now I understand how to do it!
Thank you :D

Post Reply