View Full Version : global string problem:"requires a constant value"


shenmue
10 October 2007, 03:32 PM
Hi
I am trying to declare a global string that is quering the textfield,so I can use it in procedures,but I am having the following error message:
-->Initialization of global variable "$charName" requires a constant value.

Lets say that I have that code


window;
columnLayout;
textField charName_txtFld;
global string $charName = `textField -q -text "charName_txtFld"`;
showWindow;

proc $yuhuu()
{
print $charName
}



I though that this was the way to get in procs,the name entered on the textField.
What am I doing wrong?

oh,and how could I delete global variables that I declared by mistake?the command "env"
gets all global variables,but how can I delete one?To keep clear maya memory.

thx

Flaye
10 October 2007, 07:49 PM
get rid of the "$" in front of the $yuhuu ()

It should read global proc yuhuu ()

mhovland
10 October 2007, 07:56 PM
This should do it:


window;
columnLayout;
textField charName_txtFld;
global string $charName;
$charName = `textField -q -text "charName_txtFld"`;
showWindow;

proc yuhuu()
{
print $charName
}


If you are declaring a global variable, you need to initialize it as a global first, then put the data in the variable.

shenmue
10 October 2007, 08:18 PM
Man,I īve been 2 hours trying to figure out the problem...I was going crazy,thanks guys.

But I still donīt know how to delete the global variables I did by mistake.About that,should I worry about storing a lot of global variables in memory?

trancor
10 October 2007, 05:19 PM
Once you set the variable, you'd need to restart Maya to get rid of the variable set. Plus, yeah, I'd be wary of using globals all over the place. I would say, assign the variable in the process rather than assigning the variable in the creation of the window, I don't think that would even work how you have it written. Plus it would be safer in the long run, not using any globals, just incase you don't name them distincly enough.


string $textName="charName_txtFld";
window;
columnLayout;
textField $textName;
button -command "yuhuu($textName);" -label "Run Command";
showWindow;

proc yuhuu(string $textName)
{
string $var=`textField -query -text $textName`;
print $var;
}


Putting the variable in the proc like I did means that when you call the process --
yuhuu($textName);
the variable $textName is taken by the process and turned into what ever variable it is that you assign in the beginning of the proc-
proc yuhuu(string $textName)

So if you used that variable name inside the proc, it remembers what the variable is that you assign when you call the function.

Norb
10 October 2007, 06:25 PM
Just to follow up on trancor's comments..

If you have a gui, just use an empty textField. As long as the gui is open, you can query that textField with any other procedure without the need to use global strings. You can also edit the value of the textField in other procedures.

Like so:



global proc myGUI()
{
window;
columnLayout;
button -l "Value A" -command "proc_A";
button -l "Value B" -command "proc_B";

//Create an empty textField that is invisible to the user
textField -visible 0 -text "" textFieldName;

//I use the setParent out of habit, when you start nesting layouts, you'll need it.
setParent..;
}

global proc proc_A()
{
string $name = "Procedure A";
//This edits the text value in the textField
textField -edit -text ($name) textFieldName;
//This queries the value
string $getValue = `textField -q -text textFieldName`;
print ("Selected is "+$getValue);
}

global proc proc_B()
{
string $name = "Procedure B";
textField -edit -text ($name) textFieldName;
string $getValue = `textField -q -text textFieldName`;
print ("Selected is "+$getValue);
}

shenmue
10 October 2007, 08:48 PM
:thumbsup:

Understood.I am starting get it.Thx.

But about the globals & memory,Iīve restarted Maya and excuted "env" to see all global variables (is that rigth?),and the globals I declared in other maya sessions, disapeared...I thought once you declare a global var,it would be there forever.:shrug: I am confused.

trancor
10 October 2007, 09:10 PM
Globals are there for ever, just like every other variable to set, until you close Maya. Then when you reopen, all variables are reset.

strarup
10 October 2007, 10:54 AM
hi,

well unless you use optionVar's... if you do that you can save it between Maya sessions... and have the same thingies when you are starting maya again

(however sometimes if Maya crashes it doesn't) but otherwise it does... :)

but working with optionVar can be pretty anoying and tricky....

if you use optionVar you will be able to keep your global variables until you change them...

regards

Strarup

CGTalk Moderation
10 October 2007, 10:54 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.


1