void GetNewTalkTag(ref string line, ref Vector2 position, ref int currentLine, ref StringReader sReader, Node fatherNode, int choiceNum = -1) { //check if the user have some newtalk and the line asks for one if (line.IndexOf("[newtalk") != -1 && line.IndexOf("]") != -1) { //We do have one! int initialBracket = line.IndexOf("[newtalk"); int finalBracket = -1; if (initialBracket != -1) { finalBracket = line.IndexOf("]", initialBracket); } //There still are any '[newtalk' and it is before a ']'? if (initialBracket < finalBracket) { //Everything fine until now. Now let's check the start and break variables int indexOfStart = line.IndexOf("start=", initialBracket + 8); int endOfStart = line.IndexOf(" ", indexOfStart); if (endOfStart == -1) { endOfStart = line.IndexOf("]", indexOfStart); } int indexOfBreak = line.IndexOf("break=", initialBracket + 8); int endOfBreak = line.IndexOf(" ", indexOfBreak); if (endOfBreak == -1) { endOfBreak = line.IndexOf("]", indexOfBreak); } if (indexOfStart != -1 && indexOfBreak != -1 && endOfBreak != -1 && endOfStart != -1) { string newLineToStart = line.Substring(indexOfStart + 6, endOfStart - (indexOfStart + 6)); string newLineToBreak = line.Substring(indexOfBreak + 6, endOfBreak - (indexOfBreak + 6)); if (newLineToStart.Length > 0 && newLineToBreak.Length > 0) { line = line.Substring(0, initialBracket) + line.Substring(finalBracket + 1); //Lets keep this node to move and connect later FollowUpNode followUp = new FollowUpNode(); followUp.node = fatherNode; followUp.followUpTitle = newLineToStart; followUp.identifier = choiceNum; followUpNodes.Add(followUp); } else { Debug.LogWarning("There was a problem in your start=x or break=y. Check The spelling"); } } else { Debug.LogWarning("Found a [newtalk] variable in the text but it didn't had start=x or break=y. Check The spelling"); } } } }
// RPGTalk added to load the TXT! void LoadTXTFile() { //Get the actual selected path string selectedPath = "Assets"; foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets)) { selectedPath = AssetDatabase.GetAssetPath(obj); if (File.Exists(selectedPath)) { selectedPath = Path.GetDirectoryName(selectedPath); } break; } string path = EditorUtility.OpenFilePanel("Load dialog TXT", selectedPath, "txt"); //first, clear the canvas. canvasCache.NewNodeCanvas(typeof(RPGTalkNodeCanvas)); // read the TXT file into the elements list StreamReader reader = new StreamReader(path); string txtFile = reader.ReadToEnd(); reader.Close(); StringReader sReader = new StringReader(txtFile); string line = sReader.ReadLine(); int currentLine = 0; if (line == null) { Debug.LogError("There was an error reading your file! Check your encoding settings."); return; } RPGTalkNode fatherNode = null; Vector2 position = Vector2.zero; followUpNodes = new List <FollowUpNode>(); createdNodes = new List <RPGTalkNode>(); while (line != null) { //If the line is empty, we will just ignore it if (string.IsNullOrEmpty(line)) { line = sReader.ReadLine(); currentLine++; continue; } //Lets check with this is an oppening node int titleLine = line.IndexOf("[title="); if (titleLine != -1) { string title = ""; int titleEnd = line.IndexOf("]", titleLine); if (titleEnd != -1) { title = line.Substring(titleLine + 7, titleEnd - (titleLine + 7)); } else { Debug.LogError("Error reading title"); } if (title.Length > 0) { //Good. We got a title. Let's find out if it is an oppening or a closing one if (title.IndexOf("_begin") != -1) { title = title.Substring(0, title.IndexOf("_begin")); CreateNode(ref line, ref position, ref currentLine, title, ref sReader, ref fatherNode, true); continue; } else if (title.IndexOf("_end") != -1) { //end last title node position.y += 250; position.x = 0; fatherNode = null; line = sReader.ReadLine(); currentLine++; continue; } else { Debug.LogError("Right now, Node Editor only reads TXT made with node editor. You can change your TXT title to have _begin and _end tags"); } } }// end if title //Let's check if this line is a save if (line.IndexOf("[save") != -1 && line.IndexOf("]") != -1) { //We do have one! int initialBracket = line.IndexOf("[save"); int finalBracket = -1; if (initialBracket != -1) { finalBracket = line.IndexOf("]", initialBracket); } //There still are any '[save' and it is before a ']'? if (initialBracket < finalBracket) { //Everything fine until now. Now let's check the start and break variables int indexOfStart = line.IndexOf("start=", initialBracket + 5); int endOfStart = line.IndexOf(" ", indexOfStart); if (endOfStart == -1) { endOfStart = line.IndexOf("]", indexOfStart); } int indexOfBreak = line.IndexOf("break=", initialBracket + 5); int endOfBreak = line.IndexOf(" ", indexOfBreak); if (endOfBreak == -1) { endOfBreak = line.IndexOf("]", indexOfBreak); } int indexOfSavedData = line.IndexOf("data=", initialBracket + 5); int endOfSavedData = line.IndexOf(" ", indexOfSavedData); if (endOfSavedData == -1) { endOfSavedData = line.IndexOf("]", indexOfSavedData); } int indexOfModifier = line.IndexOf("mod=", initialBracket + 5); int endOfModifier = line.IndexOf(" ", indexOfModifier); if (endOfModifier == -1) { endOfModifier = line.IndexOf("]", indexOfModifier); } if (indexOfStart != -1 && indexOfBreak != -1 && endOfBreak != -1 && endOfStart != -1 && indexOfSavedData != -1 && endOfSavedData != -1 && indexOfModifier != -1 && endOfModifier != -1) { string newLineToStart = line.Substring(indexOfStart + 6, endOfStart - (indexOfStart + 6)); string newLineToBreak = line.Substring(indexOfBreak + 6, endOfBreak - (indexOfBreak + 6)); string newSavedData = line.Substring(indexOfSavedData + 5, endOfSavedData - (indexOfSavedData + 5)); string newModfier = line.Substring(indexOfModifier + 4, endOfModifier - (indexOfModifier + 4)); int intModifier; if (newLineToStart.Length > 0 && newLineToBreak.Length > 0 && newSavedData.Length > 0 && int.TryParse(newModfier, out intModifier)) { //Good, this a valid save. Let's create the node RPGTalkSaveNode thisNode = Node.Create("rpgtalkSaveNode", position) as RPGTalkSaveNode; position.x += 300; thisNode.modifier = intModifier; thisNode.savedData = newSavedData; //Lets keep this node to move and connect later FollowUpNode followUp = new FollowUpNode(); followUp.node = thisNode; followUp.followUpTitle = newLineToStart; //followUp.identifier = choiceNum; line = sReader.ReadLine(); currentLine++; followUpNodes.Add(followUp); //Connect the node if (fatherNode != null) { ConnectionPort myPort = null; foreach (ConnectionPort port in thisNode.connectionPorts) { if (port.direction == Direction.In) { myPort = port; break; } } foreach (ConnectionPort port in fatherNode.connectionPorts) { if (port.direction == Direction.Out) { port.ApplyConnection(myPort); break; } } } continue; } else { Debug.LogWarning("There was a problem in your save variables. Check The spelling"); } } else { Debug.LogWarning("Found a [save] variable in the text but it didn't had a variable. Check The spelling"); } } } //This is none of the special lines. So it must be a common node. CreateNode(ref line, ref position, ref currentLine, "", ref sReader, ref fatherNode); } //Everything created. Everthing fine. Let's now add connections to the follow up nodes (choices and saves) foreach (FollowUpNode follow in followUpNodes) { //the follow up node is exclusive to the prior node? if it isn't, we don't want to move it. Just add the connection if (follow.followUpTitle.IndexOf("FollowUp_") != -1 && follow.followUpTitle.IndexOf("Choice_" + follow.identifier.ToString()) != -1) { //TODO: Move the followups } //Find out what node should if be connected to foreach (RPGTalkNode talkNode in createdNodes) { string removeBegin = follow.followUpTitle.Substring(0, follow.followUpTitle.LastIndexOf("_begin")); if (talkNode.CutsceneTitle == removeBegin) { //Connect the nodes ConnectionPort myPort = null; foreach (ConnectionPort port in follow.node.connectionPorts) { if (port.direction == Direction.Out) { myPort = port; break; } } foreach (ConnectionPort port in talkNode.connectionPorts) { if (port.direction == Direction.In) { port.ApplyConnection(myPort); break; } } break; } } } }