Variable inside procedure [MEL]

I’m not sure why but I cannot seem to get this to work. I’m making a quick GUI for setting up props and when I select a button I have a symbol button with the props image show up, then i want a variable to change to its number: no props = 0, prop 1 =1, prop 2 = 2 and so on. however i’m setting it up like this.

$propN = 0;

proc createCenter(){
    if ($propN = 1){
        print "prop 1
";
    }
    else if ($propN = 2){
        print "prop 2
";
    }
}

For me the result is always “prop 1” no matter the value of $propN.

What am I missing?

Thanks!

your equality tests are wrong: they have one = , not ==. When put in brackets, they get evaluated: ($propN = 1) will always equal 1 and so thats what you get back.

If you had done it like this:


proc createCenter(){
    if ($propN == 1){
        print "prop 1
";
    }
    else if ($propN == 2){
        print "prop 2
";
    }
}

You’d see that $propN is not defined inside your proc at all: you’ll get an undeclared variable error.

Did you mean to stick the value into the argument of the function?


proc createCenter(int $propN){
    if ($propN == 1){
        print "prop 1
";
    }
    else if ($propN == 2){
        print "prop 2
";
    }
}

ahh thank you! I tried the == but i was getting an error but puting the (int $propN) worked like a charm! Thank you Theodox!

Hmm, now how can I change the variable from a procedure? This is what im using for the prop icon button:

proc stone(){
button -edit -vis 1 stoneMain;
button -edit -vis 0 grassMain;
global int $propN = 1;
}

This however will not change the $propN value and will remain the same. running “global int $propN = 1” by itself will. Why wont it while inside a procedure?

Thanks again.


global proc stone() { // etc }

It’s possible you don’t need to make the proc global. Try just declaring the variable as global within the scope of the proc before you set it.


proc stone()
{
    // Set buttons etc...
    global int $propN;
    $propN = 1;
}

Or, best of all – avoid global variables like the plague they are. Just return the updated incremented number from the procedure as it is called, then you don’t have to worry about some other script that also uses the same global name for some other purpose :slight_smile:

Yup, seconded. Even if this is data that you need to keep between separate executions of a script then I’d store it in a node in the scene (if it’s scene specific) or write it to a cfg/ini file rather than use globals.