GUI.Window - How do I access data from within!?

I’m trying to access data from within the GUI.Window func delegate function in my Editor code. This is all located in function OnGUI(). Here’s my code:

var currentOption:DialogueOption; //My custom object, declared globally in the class

//In OnGUI()
for each (var o:DialogueOption in n.options) {
	currentOption = o;
	windowIndex++;
	currentOption .myWindow = GUILayout.Window(windowIndex, currentOption .myWindow, DrawOptionWindow, "Option Node", GUILayout.Width (200), GUILayout.Height (80));
	DrawNodeConnect (currentNode.myWindow, o.myWindow);
}

//Here's the window drawing function
function DrawOptionWindow(windowID:int) {
	GUILayout.Label ("Option Text:", EditorStyles.boldLabel);				
	currentOption.myText = GUILayout.TextArea(currentOption.myText, GUILayout.Height(50));				
	GUILayout.Space(20);			
		
	GUI.DragWindow();		
}	

The problem is that all the windows display the same text. If I change one, they ALL change. In the data, the last object in the List. is the one being changed. Help!

The window callback is executed later after all other GUI stuff is done. That way the windows are drawn on top of everything else. If you need to use custom data in your window there are several ways.

First one is to use the windowID which should be unique. That ID is passed to the callback. You might want to store the relevant data in an array or Dictionary and use the ID as index / key.

Another way (which is the more easier solution and since you already have a custom class the way that makes most sense) is to move your DrawOptionWindow method into your DialogueOption class. Furthermore you probably want to move the GUILayout.Window line also into the class itself.

Example:

public class DialogueOption
{
    private static var nextWinID = 1000;
    public var myText = "";
    public var position : Rect;
    private var winID : int = nextWinID++;
    
    function DrawOptionWindow(windowID:int) {
        GUILayout.Label ("Option Text:", EditorStyles.boldLabel);                
        myText = GUILayout.TextArea(myText, GUILayout.Height(50));                
        GUILayout.Space(20);
        GUI.DragWindow();        
    }
    function Draw()
    {
        position = GUILayout.Window(winID, position, DrawOptionWindow, "Option Node", GUILayout.Width (200), GUILayout.Height (80));
    }
}

In this example i also added a “winID” variable to the class. That ID should be unique. I simply added a static variable which is automatically incremented for each new instance.

In your loop you only have to execute the “Draw” method.