playMaker

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

evmo

  • Playmaker Newbie
  • *
  • Posts: 21
Creating a .txt file? [SOLVED]
« on: January 31, 2018, 05: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, 05:35:28 PM by djaydino »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Creating a .txt file?
« Reply #1 on: February 01, 2018, 12:39:54 AM »
Hi,

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

 Bye,

 Jean

evmo

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Creating a .txt file?
« Reply #2 on: February 01, 2018, 06: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

  • Junior Playmaker
  • **
  • Posts: 53
Re: Creating a .txt file?
« Reply #3 on: February 03, 2018, 01:10:28 PM »
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, 01:14:40 PM by GonerGames »

evmo

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Creating a .txt file?
« Reply #4 on: February 05, 2018, 12:46:04 PM »
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: 15500
  • Official Playmaker Support
Re: Creating a .txt file? [SOLVED]
« Reply #5 on: February 07, 2018, 03:49:22 AM »
Hi,

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

 Bye,

 Jean

evmo

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Creating a .txt file? [SOLVED]
« Reply #6 on: February 09, 2018, 02: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

  • Junior Playmaker
  • **
  • Posts: 53
Re: Creating a .txt file? [SOLVED]
« Reply #7 on: February 09, 2018, 04: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, 04:15:39 PM by GonerGames »

Plancksize

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 75
Re: Creating a .txt file? [SOLVED]
« Reply #8 on: July 25, 2018, 09:30:15 AM »
So, I was using this modification until I found out it would break on build.
I've added a couple of "#if UNITY_EDITOR"'s in it to make it work for builds (as atm it will only work on the editor).
I'm actually pretty clueless about coding but this seems to work.

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

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

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();


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

                //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: July 25, 2018, 09:33:17 AM by Plancksize »

nuFF3

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 74
  • Are we even real?
    • One Month Studio
Re: Creating a .txt file? [SOLVED]
« Reply #9 on: July 11, 2019, 09:54:53 AM »
So, I was using this modification until I found out it would break on build.
I've added a couple of "#if UNITY_EDITOR"'s in it to make it work for builds (as atm it will only work on the editor).
I'm actually pretty clueless about coding but this seems to work.

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

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

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();


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

                //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);
                }
                               
            }
        }
}
}

Thank you so much for this Plancksize. I've been looking for this for a while, and it seems it was never added to the WriteToFile in ecosystem.

Maart

  • Junior Playmaker
  • **
  • Posts: 88
Re: Creating a .txt file?
« Reply #10 on: November 29, 2020, 09:10:11 AM »
Bump please update ecosystem

blackant

  • Hero Member
  • *****
  • Posts: 521
  • http://blackantmaster.com
    • blackantmaster.com
Re: Creating a .txt file? [SOLVED]
« Reply #11 on: September 22, 2021, 03:48:52 AM »
So, I was using this modification until I found out it would break on build.
I've added a couple of "#if UNITY_EDITOR"'s in it to make it work for builds (as atm it will only work on the editor).
I'm actually pretty clueless about coding but this seems to work.

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

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

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();


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

                //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);
                }
                               
            }
        }
}
}

i was looking for this solution, it works perfectly ! thanks !