- Home /
Show variable name from a class as String
So i have this class:
class ActorKnowledge {
var state : boolean;
function ActorKnowledge (state : boolean) {
this.state = state;
}
function ActorKnowledge (state : boolean, selectedList : List.<ActorKnowledge>) {
this.state = state;
selectedList.Add(this);
}
}
And its used in several Lists. I need to have them display in the editor from reading the Lists that contain them but I can't convert the class name to string. This is what i have so far:
for (var playerKnowledge : ActorKnowledge in scriptPlayer.playerKnowledgeList){
var knowledgeName = playerKnowledge.ToString();
if (!playerKnowledge.state){
if ( GUI.Button (Rect (leftPadding, row01PositionY, position.width - buttonPaddingRight, buttonHeigth), (knowledgeName)) ){
if(EditorApplication.isPlaying){
knowledgeManager.GetKnowledge (playerKnowledge);
}
}
row01PositionY += buttonPadding;
}
else {
EditorGUI.LabelField(Rect(entryLeftPadding,row01PositionY,position.width,buttonHeigth), knowledgeName);
row01PositionY += buttonPadding;
}
}
What I really want to figure out is the variable knowledgeName! In the current example where I do a ToString() I just end up getting the button/label called ActorKnowledge.
Any help would be amazing!
UPDATE:
SO I think I phrased this incorrectly. What I meant is, I want the name of the variable that was created using this class, so for example I have these:
knowsAboutPocketWatch = new ActorKnowledge(false, playerKnowledgeList);
knowsAboutJournal = new ActorKnowledge(false, playerKnowledgeList);
canProveSarahInnocence = new ActorKnowledge(false, playerKnowledgeList);
and I would like them to show in the editor with their names rather than as "ActorKnowledge"
I don't understand. You say you want the class name. The class is Actor$$anonymous$$nowledge. ToString give you "Actor$$anonymous$$nowledge" What were you hoping for?
You are right, I didn't phrase it correctly. What I meant is that I want the name of the variable that was made using that class.
Can you say why you want this? Normally you just have a var at the top of your script and it shows in the editor with the name of that var. You can set that via Inspector or programatically.
Each one of those variables can be changed by the player during gameplay and I need a Custom ui where i can turn them on and off for fast debugging. Right now it ends up looking like this:
$$anonymous$$aking it impossible to know what each actor$$anonymous$$nowledge actually is. The only solution I can think of is to add another var to the class (e.g. name : String) and whenever I instantiate a new Actor$$anonymous$$nowledge I would also add the name to it, something like this:
class Actor$$anonymous$$nowledge {
var name : String;
var state : boolean;
function Actor$$anonymous$$nowledge (state : boolean, name : String, selectedList : List.<Actor$$anonymous$$nowledge>) {
this.name = name
this.state = state;
selectedList.Add(this);
}
}
knowsAboutPocketWatch = new Actor$$anonymous$$nowledge(false, "knowsAboutPocketWatch" player$$anonymous$$nowledgeList);
and then use the player$$anonymous$$nowledge.name for display in the editor
Answer by flamy · Aug 19, 2013 at 05:31 AM
one simple solution is using
this.GetType().Name;
objectName.GetType().Name;
But remember that it wont work if it is a static class.
Answer by MasterLG · Aug 19, 2013 at 05:23 AM
I'm not sure if JavaScript has typeof, but have you tried typeof(ClassInQuestion).ToString()
Yeah, both those options dont work. ToString still shows the type of object.
Your answer
Follow this Question
Related Questions
Naming array elements in editor 3 Answers
[JS] [Editor] SerializedObject from a custom class. 0 Answers
Converting String to Variable Name 3 Answers
Adding To Array From Editor 3 Answers
Update List<> in editor 0 Answers