void CreateChoiceNode(ref string line, ref Vector2 position, ref RPGTalkNode fatherNode, int choiceNum, ref StringReader sReader, ref int currentLine) { int initialBracket = line.IndexOf("[choice]"); //Ok! Let's isolate its string line = line.Substring(initialBracket + 8); RPGTalkChoiceNode choiceNode = Node.Create("rpgtalkChoiceNode", position) as RPGTalkChoiceNode; //Connect the node ConnectionPort myPort = null; foreach (ConnectionPort port in choiceNode.connectionPorts) { if (port.direction == Direction.In) { myPort = port; break; } } foreach (ConnectionPort port in fatherNode.connectionPorts) { if (port.direction == Direction.Out) { port.ApplyConnection(myPort); break; } } //Let's check if it has a follow up GetNewTalkTag(ref line, ref position, ref currentLine, ref sReader, choiceNode, choiceNum - 1); choiceNode.DialogLine = line; }
void CreateNode(ref string line, ref Vector2 position, ref int currentLine, string cutsceneTitle, ref StringReader sReader, ref RPGTalkNode fatherNode, bool startNode = false) { RPGTalkNode thisNode = null; int characterID = 0; int expressionID = 0; string dialoger; //Good. Let's create the node thisNode = Node.Create("rpgtalkNode", position) as RPGTalkNode; position.x += 400; //If it is a [title] line, we don't want to actually read it, but the line after it if (startNode) { thisNode.CutsceneTitle = cutsceneTitle; line = sReader.ReadLine(); currentLine++; } //dialoger GetDialoger(line, out line, out characterID, out expressionID, out dialoger); thisNode.characterID = characterID; thisNode.expressionID = expressionID; //questions string questionID; GetQuestion(line, out line, out questionID); thisNode.DialogLine = line; //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; } } } fatherNode = thisNode; if (questionID.Length > 0) { thisNode.questionID = questionID; line = sReader.ReadLine(); currentLine++; int choices = 0; float originalY = position.y; while (line != null && line.IndexOf("[choice]") != -1) { choices++; if (choices % 2 == 0) { position.y = originalY + 100; } else { position.y = originalY - 40; } CreateChoiceNode(ref line, ref position, ref fatherNode, choices, ref sReader, ref currentLine); line = sReader.ReadLine(); currentLine++; if (choices % 2 == 0 || line == null || line.IndexOf("[choice]") == -1) { position.x += 180; } continue; } position.y = originalY; } else { line = sReader.ReadLine(); currentLine++; } createdNodes.Add(thisNode); }
// 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; } } } }