- Home /
Variable won't change, any suggestions?
Hi!
I am using a custom tooltip function (credits to AngryAnt). But I have a problem when trying to make it possible to have multiple tooltips. In the post as far as I understand, "tooltip" is the string to be replaced by the tooltip text, so I changed it to:
void StringToolTip (Rect rect, string toReplace, string lastToolTip)
{
if (rect.Contains (Event.current.mousePosition))
{
toReplace = lastToolTip;
}
}
void TooltipLastLayout(string toReplace, string lastToolTip)
{
StringToolTip (GUILayoutUtility.GetLastRect (), toReplace, lastToolTip);
}
And call it like this in OnGUI:
TooltipLastLayout (stringToDisplay, "This is a tooltip");
Where "stringToDisplay" is used by the label where the tooltip are to be displayed.
The problem is that "toReplace" won't change to "lastToolTip". I think it has something to do with the fact that you can't assign a value that is "layered" like "toReplace" (?) Does anyone know why this is happening?
Answer by jacksmash2012 · Oct 04, 2013 at 06:48 PM
toReplace is a local variable within the StringToolTip function. You can assign in whatever you want within that function, but that toReplace is a completely different memory location that the toReplace variable in your ToolTipLastLayout function, even though you have named them the same thing.
What I would suggest is that you change the signature of StringToolTip to this:
string StringToolTip (...) {...}
and then you can return the updated value. So your code could be something like:
string StringToolTip (Rect rect, string toReplace, string lastToolTip)
{
if (rect.Contains (Event.current.mousePosition))
{
return lastToolTip;
}
return toReplace;
}
string TooltipLastLayout(string toReplace, string lastToolTip)
{
toReplace = StringToolTip (GUILayoutUtility.GetLastRect (), toReplace, lastToolTip);
return toReplace;
}
Your StringToolTip method has a return type of void, so you cannot return anything from it. You need to make the return type string.
Thanks for the quick reply!
I tried your code, but still the same results (I changed the return type from void to string)
Any ideas?
stupid me... sorry for the confusion with the return value. i'll correct it.
Answer by whydoidoit · Oct 04, 2013 at 09:34 PM
You need to put a ref in front of the string parameters that are to change - then you can change the value that is being passed in. Or you could return strings from both of the functions.
Wow, thanks! I put ref infront of every reference (?) and changed "This is a tooltip" to a variable, and now it works perfectly.
Just one question, what is the ref keyword(I think I went a little overboard with them)? Do you have any links or explanation?
Final code:
string StringToolTip (Rect rect, ref string oldString, ref string lastToolTip)
{
if (rect.Contains (Event.current.mousePosition))
{
return lastToolTip;
}
return oldString;
}
void TooltipLastLayout(ref string toReplace, ref string lastToolTip)
{
toReplace = StringToolTip (GUILayoutUtility.GetLastRect (), ref toReplace, ref lastToolTip);
}
and calling it like this:
TooltipLastLayout (ref stringToDisplay, ref tooltipText);
ref simply implies that you are passing in the memory location of the actual parameter, and so the string that is referenced by "oldString" is being updated, whereas previously you were assigning "oldString" to a new string completely.
So a string is an immutable reference, changing it inside the function just changes the local copy. If you want to update the passed in reference then that's what ref is for. You only need it on the first parameter.
Ahhh, that's very handy. Both of you; thanks for the help! ;)