Home Coding, Scripting, Shaders

Mel script: Is there a way to declare a procedure with variable without declaring the variable?

polycounter lvl 2
Offline / Send Message
Fran127 polycounter lvl 2
Sorry if the title is a little confusing,
I am trying to have a procedure that containts a proxy text that will be replaced after, but the only way I can create the procedure is if I declare the variable inside the procedure and that ruins my intention

// Create variable
string $vartest  = "test";

// Check print
print $vartest;
// Rename the text inside the variable
$vartest = `substituteAllString $vartest "test" "test_2"`;
// Check print again
print $vartest;

// Declare a procedure / DOESN'T WORK /
proc procTest()
{
print $vartest;
}
// Test proc
procTest();

//////////////////////////////////

I need to not declare the variable inside the procedure because I want to have just one big code with all my procedures with a "proxy test" and then for each buttom I'll change the text inside the variable. Is there a clean way to do that?

Replies

  • sprunghunt
    Offline / Send Message
    sprunghunt polycounter
    I'm not entirely sure if this is actually what you want but maybe try passing the variable as an input:

    proc procTest(string $varTest)<br>{<br>print $varTest;<br>}<br><br>procTest($varTest);
  • Tekoppar
    Offline / Send Message
    Tekoppar polycounter lvl 10
    The reason this is happening is because Maya's variable scope is a bit weird in that variables inside functions are separate from everything else. You need to declare it as global inside the function like
    global string thisIsGloba;
    Here's a short example of how the scope works.
    string $scope = "This is scoped";<br>print($scope); //prints "This is scoped";<br>{<br>    print($scope); //prints "This is scoped";<br>}<br><br>proc printVar() {<br>    print($scope); //gives undeclared variable because there is no variable declared as $scope inside printVar()<br>    string $scope = "This is scoped locally";<br>    print($scope); //We can use the name scope for a variable because it's not used in the function scope<br>    string $scopeTwo = "This is scoped locally";<br>}<br>printVar();<br><br>print($scopeTwo); //gives undeclared variable because there is no variable declared as $scopeTwo in the global scope<br>global string $scopeTwo; //even with global we just declared a new $scopeTwo string<br>print($scopeTwo); //this will print nothing as it's an empty string<br><br>proc printVarAgain() {<br>    global string $scope;<br>    print($scope); //prints "This is scoped";<br>}<br>printVarAgain();
    But even then you should avoid global variables whenever you can as you can never truly rely on their values and they pollute the namespace.
  • Fran127
    Offline / Send Message
    Fran127 polycounter lvl 2
    I'm not entirely sure if this is actually what you want but maybe try passing the variable as an input:

    proc procTest(string $varTest)<br>{<br>print $varTest;<br>}<br><br>procTest($varTest);
    If I try this doesn't work, " "$varTest" is an undeclared variable. "

  • Fran127
    Offline / Send Message
    Fran127 polycounter lvl 2
    Tekoppar said:
    The reason this is happening is because Maya's variable scope is a bit weird in that variables inside functions are separate from everything else. You need to declare it as global inside the function like
    global string thisIsGloba;
    Here's a short example of how the scope works.
    string $scope = "This is scoped";<br>print($scope); //prints "This is scoped";<br>{<br>    print($scope); //prints "This is scoped";<br>}<br><br>proc printVar() {<br>    print($scope); //gives undeclared variable because there is no variable declared as $scope inside printVar()<br>    string $scope = "This is scoped locally";<br>    print($scope); //We can use the name scope for a variable because it's not used in the function scope<br>    string $scopeTwo = "This is scoped locally";<br>}<br>printVar();<br><br>print($scopeTwo); //gives undeclared variable because there is no variable declared as $scopeTwo in the global scope<br>global string $scopeTwo; //even with global we just declared a new $scopeTwo string<br>print($scopeTwo); //this will print nothing as it's an empty string<br><br>proc printVarAgain() {<br>    global string $scope;<br>    print($scope); //prints "This is scoped";<br>}<br>printVarAgain();
    But even then you should avoid global variables whenever you can as you can never truly rely on their values and they pollute the namespace.
    I understand, thank you for taking your time with so many examples Tekoppar!
    As I understood you can't declare a variable outside a procedure, right?
  • Tekoppar
    Offline / Send Message
    Tekoppar polycounter lvl 10
    Fran127 said:
    I understand, thank you for taking your time with so many examples Tekoppar!
    As I understood you can't declare a variable outside a procedure, right?
    You can, as in the example above if you want to use a variable declared outside a function(procedure) you need to declare the variable inside the function as global. So if you have a variable declared outside a function like
    string $variable = "Hello Polycount";
    inside the function you would type
    global string $variable;
    and you could then use it inside the function.
  • Fran127
    Offline / Send Message
    Fran127 polycounter lvl 2
    Oh yes!! There we go! Thank you so much Tekoppar!!
    Sorry if it was a little dificult to make me understand how it works
Sign In or Register to comment.