Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started by: evmo 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!
-
Hi,
yes, you can find actions to deal with file on the Ecosystem, search for "WriteToFile"
Bye,
Jean
-
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?
-
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
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();
}
}
-
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
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.
-
Hi,
ok, let me know if this works, I'll update the action.
Bye,
Jean
-
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.
-
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.
// (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);
}
}
}
}
}
-
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.
// (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);
}
}
}
}
}
-
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.
// (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.
-
Bump please update ecosystem
-
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.
// (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 !