I wasn’t sure whether to but this in scripting or here, as it’s one tied to the other. What I’m trying to do is make a node-based editor to write my XML files for me. The actual writing of the XML file is easy. What I’m having trouble with is having an array of nodes based on a class, and being able to create nodes (and therefore add to the array), and have the properties for each assigned to each member in the node class array. What I’ve got gives me an index out of range error, because it’s trying to get an array that’s not there. How can I sort this, so that the array is actually based on what windows I have, and created by that, instead of how they are now?
Here’s the code I have.
import System;
import System.IO;
class DialogueGraph extends EditorWindow {
@MenuItem ("RPG/Write Dialogue XML")
static function ShowWindow() {
EditorWindow.GetWindow(DialogueGraph);
}
var fileName = "MyFile.xml";
var numberOfNodes : int;
var nodes : node[];
var windowRect = Rect(100, 100, 100, 150);
var windowRect2 = Rect(250, 250, 100, 150);
class node {
var text : String;
var hasReplies : boolean;
var replies : String[];
var links : int[];
var giveItem : boolean;
var itemToGive : int;
var numToGive : int;
var startsBattle :boolean;
var enemyIndex : int;
var eLevel : int;
var eName : String;
var endpoint : boolean;
var newStartingPoint : int;
}
function Write() {
if (File.Exists(fileName))
{
Debug.Log(fileName+" already exists.");
return;
}
var sr = File.CreateText(fileName);
sr.WriteLine ('<?xml version="1.0" encoding="utf-8" ?>');
sr.WriteLine ('<dialogue>');
sr.WriteLine (" ");
for (var i : int = 0; i < numberOfNodes; i++) {
//start writing a cnode with the ID (as all cnodes a required to have an ID)
sr.WriteLine ('<cnode id="'+i+'" ');
//check if node is an endpoint, and if so, write to it.
//if (nodes[i].endpoint) {
sr.Write('endpoint="'+nodes[i].endpoint+'" ');// }
//check if node has replies, and write it.
sr.Write('hasReplies="'+nodes[i].hasReplies+'" ');
//check if nodes gives an Item and write.
sr.Write('giveItem="'+nodes[i].giveItem+'" ');
//check if node starts a battle and write.
sr.Write('startsBattle="'+nodes[i].startsBattle+'" ');
//check if gives item, then give the item index and amount to give
if (nodes[i].giveItem) {
sr.Write('itemToGive="'+nodes[i].itemToGive+'" ');
sr.Write('numToGive="'+nodes[i].numToGive+'" ');
}
//check if starts battle, then give enemyindex, level, and name
if (nodes[i].startsBattle) {
sr.Write('enemyName="'+nodes[i].eName+'" ');
sr.Write('enemyLevel="'+nodes[i].eLevel+'" ');
sr.Write('enemyIndex="'+nodes[i].enemyIndex+'" ');
}
//if it's an endpoint, give it the new start point for the next convo. (can be 0 for repeating the conversation)
if(nodes[i].endpoint){
sr.Write('multiNode="'+nodes[i].newStartingPoint+'" ');
}
//close the cnode
sr.Write('>');
//write the text prompt tag
sr.WriteLine('text prompt="'+nodes[i].text+'"/>');
//if the node has replies then list both.
if (nodes[i].hasReplies) {
sr.WriteLine('reply link="'+nodes[i].links[0]+'" text="'+nodes[0].replies[0]+'" />"');
}
//close the node
sr.WriteLine("</cnode>");
sr.WriteLine(" ");
}
sr.WriteLine ('</dialogue>');
sr.Close();
}
function OnGUI() {
//numberOfNodes = nodes.length;
GUILayout.Label("Use this to create dialogue readable by the DialogueHandler script");
//for (var n : int = 0; i < nodes.length
//}
Handles.BeginGUI();
Handles.DrawBezier(windowRect.center, windowRect2.center,
Vector2(windowRect.xMax + 50f,windowRect.center.y),
Vector2(windowRect2.xMin - 50f,windowRect2.center.y), Color.red, null, 5);
Handles.EndGUI();
BeginWindows();
windowRect = GUI.Window (0, windowRect, WindowFunction, "Node 1");
windowRect2 = GUI.Window (1, windowRect2, WindowFunction, "Node 2");
EndWindows();
}
function WindowFunction (windowID : int) {
GUI.DragWindow();
GUILayout.Button("Button");
GUILayout.Label(windowID.ToString());
nodes[windowID].hasReplies = EditorGUILayout.BeginToggleGroup("Has Replies", nodes[windowID].hasReplies);
}
}
The Write() function can be ignored at the moment, because that’s what’s writing my files and works as is.
Any help would be appreciated.