Search Unity

Dump an array to a file, then load it back up

Discussion in 'Scripting' started by darkgriffin, Apr 1, 2010.

  1. darkgriffin

    darkgriffin

    Joined:
    Nov 2, 2009
    Posts:
    113
    I'm working on a level editor in Unity(not for use in unity itself, but for use as a tool in the finished game, to build new levels with).

    I have all my level data worked out and stored in an array object.

    I need to simply check if a file exists, if not then create a new one. Then I need to make a raw dump of the array object to the file. I also need a reliable method of dumping back into the array, of course. Security is not an issue(I'm personally very fond of level formats that are text readable/editable outside of the level editor itself, it's fun to see what users make with such power over the game data.) Filesize is an issue however, since the users will most likely be sharing their levels over the internet on forums/websites, thus my goal of simply spilling the editor array to a file and back.

    I've tried looking over the documentation, but I keep finding a sea of Unity Editor commands instead of some simple way to dump data to files and manage them. :oops: Can someone point me in the right direction?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  3. darkgriffin

    darkgriffin

    Joined:
    Nov 2, 2009
    Posts:
    113
    Ah, forgot to say I'm using Unity Script(I think it's Java, or something). I tried searching in the documentation for Unity Scripting for any IO type stuff, but it's not there.

    I have no idea how to use that code you linked to. When I type the command, Unity isn't even highlighting the code, so I don't think it's valid code for Unity Engine. :?
     
  4. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    For the sake of this topic not getting turned into what Unityscript is, I will clear it up now. It is a form of Javascript.
     
  5. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's fine, so am I. All the info there is still completely relevant; you can switch the syntax tab to JScript.

    Right, because all the IO stuff, along with the rest of .NET, is already documented on the MSDN site. You can use the Mono docs for System.IO here, but personally I don't find those to be very useful. The MSDN docs are more readable if you switch to lightweight view (upper right).

    Yes, it is; Unity scripting is entirely built on .NET. As long as you stick to .NET 2.0 or earlier (the MSDN docs have a switch for this), almost all of it is valid.

    --Eric
     
  7. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,553
    System.File.IO is part of .NET, which is also available to Unity via Mono

    Code (csharp):
    1.  
    2. var text : String;
    3.  
    4. function OnGUI()
    5. {
    6.     GUILayout.Label(text);
    7. }
    8.  
    9. function Start()
    10. {
    11.     text = System.IO.File.ReadAllText("C:\\file.txt");
    12. }
    13.  
     
  8. darkgriffin

    darkgriffin

    Joined:
    Nov 2, 2009
    Posts:
    113
    Ok, thanks all!

    You'll have to forgive me, I'm not a programmer by nature, and even most of what I do in Unity Script is trial and error. :D

    Let me run by what I think I've learned, so you can correct me if I'm totally wrong. This whole able to use .net thing just blew my mind up. :wink:

    1 - Even though when I type System.IO.File it's not highlighting the words or coming up with the autocomplete in the Unity Code Editor, it still works because Unity contains .net's commands, right?

    2 - Everything on that documentation at http://msdn.microsoft.com/en-us/library works in Unity? Or only certain commands? I'm really lost and never understood all the versions of .net, only that it's some sort of programming kit. .net is sort of a big black box to me, I've never learned to use it.

    3 - The .net documentation has a lot of commands that say "Overloaded." in their description. What on earth does that mean? Do I need to know if I'm using it in Unity's Javascript?

    Sorry for all the questions, I just really don't know what I'm doing anymore. :oops:

    EDIT: Ah, indeed it does work. Ok, no need to answer number 1. I couldn't get it to work because I wasn't telling IO to actually do anything, I had just typed "System.IO()" and was trying to pass variables through that. Shows you how silly I am at this whole coding thing. :D :oops:

    Still curious about 2 and 3, and I got another one too.

    4 - I am getting this error in Unity's Console with my test code.

    "IOException: Sharing violation on path C:\test\Bob.txt"

    I am guessing it has to do with making the file but not setting any permissions, in other words, the OS(Windows XP) is probably yelling at me for making new files that don't belong to any users. Should I just ignore this error, or is there something I am doing wrong?

    My code is just this, quick and dirty:

    Code (csharp):
    1. System.IO.File.Create("C:/test/Bob.txt");
     
  9. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,553
    Probably not all. Mostly the System commands. You can also use the mono documentation (http://go-mono.com/docs/index.aspx) as unity really uses mono, not .NET, although basically they're the same.



    It simply means you can pass different variations of arguments to that function

    Code (csharp):
    1.  
    2. // for example lets say we have an imaginary function that takes in a Vector3:
    3. someFunction(Vector3(4,2,5))
    4.  
    5. // if it was overloaded to take in 3 numbers, you can also do this instead:
    6. someFunction(4,2,5)
    7.  
    That was just an example.

    Its basically for ease of use.

    From here I can see you lack a background in programming. I suggest you learn some basics, you can learn about Javascript here: http://w3schools.com/js/default.asp.

    Just learn the basics, don't bother with the advanced stuff since that's for html-related javascript (which unity doesn't use).

    Then you can learn the difference between normal Javascript and Unityscript here: http://www.unifycommunity.com/wiki/index.php?title=Head_First_into_Unity_with_JavaScript



    Hmm, I don't experience any problem when creating a text file from the code you used

    Try using this instead and see what happens:
    Code (csharp):
    1.  
    2. System.IO.File.Create(Application.dataPath + "/Bob.txt");
    3.  
    The text file should end up in your assets folder.
     
  10. darkgriffin

    darkgriffin

    Joined:
    Nov 2, 2009
    Posts:
    113
    Thanks for all the links. I'm not completly lost in programming, I've got a background scripting movies and mock websites in Flash Actionscript, but Unity and .net are a whole other beast. In other words, I know enough coding methods to get myself into trouble, but not enough to get back out. :D I'll probably spend next week studying up on java programming so I understand what's going on better. :)

    Code (csharp):
    1. System.IO.File.Create(Application.dataPath + "/Bob.txt");
    That still results in the "Sharing Violation" error. I'm the administrator(and only user of the PC). The files are being made, the console is just tossing red errors at me every time it happens.

    It is most likely because my Unity project is located in my Shared Documents instead of somewhere on my normal folders. I'm working with my brother(he does some of the modeling, Unity's autoupdate of files is amazing for working like this :) ), so we'll keep this setup for now. I'll test if the error exists if I run it in my local C:/UnityProjects directory next week and post the results of the test here for future reference. For today, I'm more concerned with finishing up some of the data handling for the editor. And of course I've got lots of homework to read over now too. 8)

    Thanks again for all your help!
     
  11. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,553
    I see, sorry for assuming. The links I gave should still help though.
     
  12. darkgriffin

    darkgriffin

    Joined:
    Nov 2, 2009
    Posts:
    113
    That javascript tutorial won't open in IE 8. I get a blank page background. It's really weird, cause usually the stuff from w3 works over a lot of browsers. :eek: No big deal though, I've noticed Javascript works very much like how Actionscript 3 was. There are lots of technical differences of course, but the basic concept of line based commands, end of line symbols, functions attached to objects, and Object-Dosomething structures are the same enough where I can understand it.

    The second link, Unity with Javascript, is very helpful. I've been reading it all morning, and slowly regrasping all the programming terms. Thanks for the links!

    Update on the file error above:

    It was indeed caused by me running the program in the Shared Documents folder. Things in the Shared Documents are handled differently when it comes to permissions, so the .net was tossing an error that basically boiled down to "This exe is owned by everyone, but is writing to a directory only you own, and this is a violation of safety and security for the end user".

    To solve this, I'm simply ignoring the error, because I can't work with my brother developing the program if it's not shared between our computers. Once the game itself is installed or moved to a proper C: location(say, C:/Games/mygame), the error goes away because the folder is actually owned by the user the program is being run by(the admin account in this case).

    It is interesting to note that while Unity/.net was happy to tell me the error, nothing in windows XP was actually stopping the write to file. Guess that's what security software is for. :p