playMaker

Author Topic: Actions to save/load text files  (Read 9056 times)

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Actions to save/load text files
« on: April 15, 2013, 11:57:49 AM »
Hi, I am trying to make a couple of PM actions, one to put all the content of a text  file into a string variable, the other to write a string value into a text file.

Didn't manage to set them up, because I don't know all the damn C# syntax... so if anyone would tell me what's wrong here, I would be delighted.

Code: [Select]
// (c) Copyright Marco E.G. Maltese, No rights reserved.

using UnityEngine;
using System.IO;
using System.Text;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.ScriptControl)]
[Tooltip("Read a determined file into a string.")]
public class TextFileRead : FsmStateAction
{
public FsmOwnerDefault gameObject;

public FsmString pathName;
public FsmString fileText;

public override void Reset()
{
pathName = "";
fileText = "";
}

void readFile ()
{
fileText = System.IO.File.ReadAllText(pathName);
}
}
}

Code: [Select]
// (c) Copyright Marco E.G. Maltese, No rights reserved.

using UnityEngine;
using System.IO;
using System.Text;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.ScriptControl)]
[Tooltip("Saves a text string to a determined file.")]
public class TextFileWrite : FsmStateAction
{
public FsmOwnerDefault gameObject;

public FsmString pathName;
public FsmString fileText;

// public override void Reset()
// {
// pathName = null;
// fileText = null;
// }

void writeFile ()
{
System.IO.File.WriteAllText(pathName, fileText);
}
}
}

I didn't have chance to even try these actions because I have a couple of compiler errors that I can't fix.

Assets/PlayMaker Custom Actions/TextFileRead.cs(26,51): error CS1502: The best overloaded method match for `System.IO.File.ReadAllText(string)' has some invalid arguments

Assets/PlayMaker Custom Actions/TextFileRead.cs(26,51): error CS1503: Argument `#1' cannot convert `HutongGames.PlayMaker.FsmString' expression to type `string'




By the way: what is that part PUBLIC OVERRIDE VOID RESET() for???
I don't know if I need to keep it or it can be deleted.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Actions to save/load text files
« Reply #1 on: April 16, 2013, 01:18:10 AM »
Hi,

 ok, few things:

 a Fsm Variables MUST be get and set using .Value so in your case, you need to write:

Code: [Select]
System.IO.File.WriteAllText(pathName.Value, fileText.Value);

a FsmString is not a String, it's a wrapper for a string and therefore the .Value


Reset() is called when you select the action from the state stack and select from the menu "reset", it defaults to the values you want, this is a good practice to keep this and set the public values of your action to the defaults.

bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Actions to save/load text files
« Reply #2 on: April 17, 2013, 08:10:17 AM »
Hey, thank you for your help!

So, the action for writing on this is like this now:

Code: [Select]
// (c) Copyright Marco E.G. Maltese, No rights reserved.

using UnityEngine;
using System.IO;
using System.Text;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.ScriptControl)]
[Tooltip("Saves a text string to a determined file.")]
public class TextFileWrite : FsmStateAction
{
public FsmOwnerDefault gameObject;

public FsmString pathName;
public FsmString fileText;

public override void Reset()
{
pathName = "";
fileText = "";
}

void writeFile()
{
System.IO.File.WriteAllText(pathName.Value, fileText.Value);
}
}
}

But it's still not working.

I managed to use SEND MESSAGE and write a text file with a Javascript on an object but the problem is that I can only set up a single variable for a function, and I need 2: filename string and text string.

This is why I tried to make this PM action (which could come useful to many users I think).
But I bought PlayMaker exactly because I have no wish to learn C# syntax or .NET hell of documentation, so I really can't go through and find why the hell the file is not being written.



I have seen your example here where you bridge scripts but this requires 2 scripts to work and looks a bit too cumbersome imho.

On this, why not just read the FSM variables from inside the script? Is it possible?

This way I could use my Javascript reading the FSM variables from inside the script.
But I can't find any documentation about this in PlayMaker.

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Actions to save/load text files
« Reply #3 on: April 17, 2013, 09:03:47 AM »
I tried to read the variables from the FSM and keep using my Javascript, reading the documentation, but no success.

Here is the code:

Code: [Select]
import System.IO;
import HutongGames.PlayMaker;

function writeTextFileJS() {

var writeTextFileFSM : PlayMakerFSM;
var pathName = writeTextFileFSM.FsmVariables.GetFsmString("pathName").Value;
var textFile = writeTextFileFSM.FsmVariables.GetFsmString("textFile").Value;

    // Create an instance of StreamWriter to write text to a file.
    sw = new StreamWriter("Assets/MARCO/MAPS/TXT/testfile.txt");
sw.Write(textFile);

    sw.Close();

}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Actions to save/load text files
« Reply #4 on: April 18, 2013, 02:06:37 AM »
Hi,

 the way I approach a new action is the following, first I find a workign script on the web and when I know it works, I then do the playmaker action, if you work straight off an action with a feature you are not sure is working, you will waiste time. So first find a solution that works on the unity wiki or forums.

http://answers.unity3d.com/questions/23578/how-do-i-write-a-line-of-text-to-a-file.html

http://forum.unity3d.com/threads/8864-How-to-write-a-file

Bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Actions to save/load text files
« Reply #5 on: April 18, 2013, 02:10:18 AM »
Ye but that's what I do too  ;D

In fact, none of that stuff is from me, I found it around, in Javascript and in C#, and I tried to integrate with PM actions.

In fact, the JS action is working by itself, but what I need to know is how to take variables from a FSM.
That would be the easiest of all: read a couple of variables in the FSM and use them in the script.
I also read the PM docs and tried it but I'm missing something.
The examples in the manual are not easy (remember: people buying PM are NOT coders and probably they don't even want to become coders) and so I can't understand what is wrong.
I only need to know how to read a variable in an FSM from a JS, then I'm done.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Actions to save/load text files
« Reply #6 on: April 18, 2013, 02:11:02 AM »
hi,

 do you have the original post of script that works for you? can you share it?

bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Actions to save/load text files
« Reply #7 on: April 18, 2013, 04:37:01 AM »
Code: [Select]
import System.IO;
import HutongGames.PlayMaker;

function writeTextFileJS() {

[color=red] var writeTextFileFSM : PlayMakerFSM;
var pathName = writeTextFileFSM.FsmVariables.GetFsmString("pathName").Value;
var textFile = writeTextFileFSM.FsmVariables.GetFsmString("textFile").Value;[/color]

    // Create an instance of StreamWriter to write text to a file.
    sw = new StreamWriter("testfile.txt");
sw.Write("textFile string text goes in here");

    sw.Close();

}

This one is working, if you take off the FSM stuff (in red), it will just write a text file.

I also managed to set the text inside the file by passing the string value to the function, but, as I said, I need at least to set the path and filename too...

And, I may be wrong on this, but maybe it's better to stay on JS so that the thing will work without problems also on Unix and Mac?
Or because of Mono it's no problem if we use the C# .NET version?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Actions to save/load text files
« Reply #8 on: April 22, 2013, 01:17:05 AM »
Hi,

Allow me few days, I'll work it out. Bump me towards the end of the week if you don't hear from me,

bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Actions to save/load text files
« Reply #9 on: April 22, 2013, 01:43:14 AM »
Thank you very much, man, you really are a Hero Member  ;D
I hope I can help you one day too... I make modeling, so feel free to tell me if you need something!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Actions to save/load text files
« Reply #10 on: June 05, 2013, 06:09:53 AM »
Hi,

 Sorry it took longer than expected, catching up now :)

 Please find an action attached write to files.

 I would however recommend you use something like EasySave2, it's a lot more powerful and covers way more ground.

bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Actions to save/load text files
« Reply #11 on: June 08, 2013, 12:28:25 PM »
Thank you very much for the action and info, man!  :-*  ;D