playMaker

Author Topic: Creating a .txt file? [SOLVED]  (Read 443 times)

evmo

  • Playmaker Newbie
  • *
  • Posts: 21
    • View Profile
Creating a .txt file? [SOLVED]
« on: January 31, 2018, 03:10:09 PM »
I'm looking for an action that will create a .txt file in a directory I specify.  I've got my fsm successfully writing strings to a txt file that I've created in windows but I don't want the user to have to manually make this file before running the tests.

This is a very interesting project...I'm using Unity and Playmaker to make a broadcast camera calibration tool for my company. it will spit out a text file that will be fed into an algorithm that one of our engineers will write. Pretty neat to be making a non-game in a game engine hehe! 
« Last Edit: February 05, 2018, 03:35:28 PM by djaydino »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 13001
  • Official Playmaker Support
    • View Profile
Re: Creating a .txt file?
« Reply #1 on: January 31, 2018, 10:39:54 PM »
Hi,

yes, you can find actions to deal with file on the Ecosystem, search for "WriteToFile"

 Bye,

 Jean

evmo

  • Playmaker Newbie
  • *
  • Posts: 21
    • View Profile
Re: Creating a .txt file?
« Reply #2 on: February 01, 2018, 04:39:56 PM »
Hi,

yes, you can find actions to deal with file on the Ecosystem, search for "WriteToFile"

 Bye,

 Jean


I'm currently using the "WriteToFile" action from the ecosystem to successfully write strings to a text file that I've already made.  I don't see anything in the action that will make the file for me though.  I define the path and file name but it won't create the file, only write to it if it finds that file where I specify it...am I missing something?

GonerGames

  • Playmaker Newbie
  • *
  • Posts: 24
    • View Profile
Re: Creating a .txt file?
« Reply #3 on: February 03, 2018, 11:10:28 AM »
The Write to File script does not allow for creating a file.
Edit the Writeto File script to the below in the OnEnter Function
Note: This is just specific for creating a .txt file if not found and is not universal
Code: [Select]
public override void OnEnter()
{
            if (System.IO.File.Exists(filePath.Value))
            {
                // Create an instance of StreamWriter to write text to a file.
                StreamWriter sw = new StreamWriter(filePath.Value, true);
                sw.Write(text.Value);
                sw.Close();
                Fsm.Event(successEvent);
                Finish();
            }
            else
            {
                //create a .txt file if file does not exist
                File.CreateText(filePath.Value);
                //Now add in your data
                StreamWriter sw = new StreamWriter(filePath.Value, true);
                sw.Write(text.Value);
                sw.Close();
                Fsm.Event(successEvent);
                Finish();
            }
        }
« Last Edit: February 03, 2018, 11:14:40 AM by GonerGames »

evmo

  • Playmaker Newbie
  • *
  • Posts: 21
    • View Profile
Re: Creating a .txt file?
« Reply #4 on: February 05, 2018, 10:46:04 AM »
The Write to File script does not allow for creating a file.
Edit the Writeto File script to the below in the OnEnter Function
Note: This is just specific for creating a .txt file if not found and is not universal
Code: [Select]
public override void OnEnter()
{
            if (System.IO.File.Exists(filePath.Value))
            {
                // Create an instance of StreamWriter to write text to a file.
                StreamWriter sw = new StreamWriter(filePath.Value, true);
                sw.Write(text.Value);
                sw.Close();
                Fsm.Event(successEvent);
                Finish();
            }
            else
            {
                //create a .txt file if file does not exist
                File.CreateText(filePath.Value);
                //Now add in your data
                StreamWriter sw = new StreamWriter(filePath.Value, true);
                sw.Write(text.Value);
                sw.Close();
                Fsm.Event(successEvent);
                Finish();
            }
        }

Thank you so much!  I will give this a try later today.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 13001
  • Official Playmaker Support
    • View Profile
Re: Creating a .txt file? [SOLVED]
« Reply #5 on: February 07, 2018, 01:49:22 AM »
Hi,

 ok, let me know if this works, I'll update the action.

 Bye,

 Jean

evmo

  • Playmaker Newbie
  • *
  • Posts: 21
    • View Profile
Re: Creating a .txt file? [SOLVED]
« Reply #6 on: February 09, 2018, 12:47:00 PM »
I haven't had much time to work with this in the last few days but a quick alteration of the action with the provided code has it making a text file but not in the directory specified.  It also won't write any text to the file if it has to create it first.  When I get more time I'll mess with it further.

GonerGames

  • Playmaker Newbie
  • *
  • Posts: 24
    • View Profile
Re: Creating a .txt file? [SOLVED]
« Reply #7 on: February 09, 2018, 02:13:45 PM »
After much testing I noticed that there was a long delay between when the file was created until it would actually be available. So i modified the script to remove this issue.

Here is the complete script including a check to see if the new file is created.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2014. All rights reserved.
/*--- __ECO__ __PLAYMAKER__ __ACTION__ ---*/

using UnityEngine;
using System.IO;
using UnityEditor; //<- added to make the AssetDatabase available.

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Files")]
[Tooltip("Write string to a File")]
public class WriteToFile : FsmStateAction
{
[RequiredField]
[Tooltip("Set the file path, for example : Assets/myfile.txt")]
public FsmString filePath;

[RequiredField]
        [UIHint(UIHint.TextArea)]
        [Tooltip("The text")]
public FsmString text;

        public FsmEvent successEvent;
        public FsmEvent failureEvent;


        public override void Reset()
{
filePath = null;
text = null;

}


public override void OnEnter()
{
            if (File.Exists(filePath.Value))
            {
                // Create an instance of StreamWriter to write text to a file.
                StreamWriter sw = new StreamWriter(filePath.Value, true);
                sw.Write(text.Value);
                sw.Close();
                Fsm.Event(successEvent);
                Finish();
            }
            else
            {
                //Stream Writer will create the file automatically
                StreamWriter sw = new StreamWriter(filePath.Value, true);
                sw.Write(text.Value);
                sw.Close();

                //re-import file and reference in editor
                AssetDatabase.ImportAsset(filePath.Value);

                //Check to see if file was created successfully
                if (File.Exists(filePath.Value))
                {
                    Debug.Log("New File Made");
                    Fsm.Event(successEvent);
                    Finish();
                }
                else
                {
                    Debug.Log("Error: File not created");
                    Fsm.Event(failureEvent);
                }
                               
            }
        }
}
}
« Last Edit: February 09, 2018, 02:15:39 PM by GonerGames »