Search Unity

Newbie - which path to take?

Discussion in 'Scripting' started by d@mmi, Nov 30, 2007.

  1. d@mmi

    d@mmi

    Joined:
    Nov 30, 2007
    Posts:
    19
    I'm starting out on a project where I want to be able to model injury or damage which is location specific, eg the closer you get to a fire, the quicker you'll be injured. The fire (and smoke) can be distributed throughout the building.

    I see doing this by generating heat intensity values for my domain (outside of Unity) and storing the values in a 3d array, probably just in an ASCII file.

    I'd like to be able to open this file, allocate the values to an array and associate each vale to a cell in my domain.

    My question is - which script should I use? I see that Javascript is 'the preferred' language but have had issues opening files with this in the past.

    I know a bit of Python but am nervous about using Boo due to how well documented and supported this is.

    As to C#, well I really have no experience in this language so it would be a very steeep learning curve.

    Any advice most gratefully received.

    Cheers

    Dave
     
  2. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    What sorts of issues, and was this while using Unity? In my experience with Unity, the differences in what I can do with Javascript versus C# or Boo comes largely down to personal preference, as all the file access tools in Unity can be used equally well by all three languages.
     
  3. d@mmi

    d@mmi

    Joined:
    Nov 30, 2007
    Posts:
    19
    Thanks for this- I think you've answered my question in that Javascript within Unity can read from (and write to?) external files.

    Problems encountered in the past involved using Javascript to access html files.

    Thanks again.

    D
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    For reading/writing files you use .net (which is the same for all 3 languages), or for just reading, you could use the TextAsset type that's new in 2.0, if you want the files to be included in your project and not external. From what you described, I'd probably use TextAsset in that case.

    I'm not clear if you're saying that you've had problems with Javascript in the past, outside of Unity. Javascript in Unity is actually quite different in a lot of ways.

    --Eric
     
  5. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Here's some JavaScript for loading and saving to text files. I've clobbered some code from my project; excuse me if I've screwed it up...

    That said, I just discovered about the new TextAsset from this thread, so if you don't need to write to the file, that's probably the way to go.

    Code (csharp):
    1. import System;
    2. import System.IO;
    3.  
    4. function SaveText( fileName : String, theText : String ){
    5.     var sw : StreamWriter = new StreamWriter ( Application.dataPath + "/" + fileName );
    6.     sw.WriteLine( theText );
    7.     sw.Close ();
    8. }
    9.  
    10. function LoadData( fileName : String ){
    11.     var line : String = "";
    12.     var content : String = "";
    13.     var offset : int;
    14.     try {
    15.         var sr : StreamReader = new StreamReader ( Application.dataPath + "/" + fileName );
    16.         line = sr.ReadLine();
    17.       while (line != null) {
    18.         line = sr.ReadLine();
    19.             content = content + line + "\n";
    20.       }
    21.         sr.Close();
    22.         return content;
    23.     }
    24.     catch (e) {
    25.         print ( "Error: " + Application.dataPath + "/" + fileName );
    26.     }
    27. }
     
  6. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    For fires, loading text files sounds like an unnecessary complication. Instead I would attach a trigger collider to your fire with a script that sends a message to whatever it touches, then the message is received by the player which causes him to lose health.

    Code (csharp):
    1.  
    2. var heat : float = 5.00;
    3.  
    4. function OnTriggerStay (other : Collider)
    5. {
    6.     other.gameObject.SendMessage("Burn", heat * Time.fixedDeltaTime, SendMessageOptions.DontRequireReceiver);
    7. }
    8.  

    Code (csharp):
    1.  
    2. var health = 10.00;
    3.  
    4. function Burn (amount : float)
    5. {
    6.     health -= amount;
    7. }
    8.  
    9. function Update ()
    10. {
    11.     if(health < 0)
    12.     {
    13.         print("I'm dead!");
    14.         Destroy(gameObject);
    15.     }
    16. }
    17.  
     
  7. d@mmi

    d@mmi

    Joined:
    Nov 30, 2007
    Posts:
    19
    Thanks for all your posts which are really helpful.

    In answer to some points - yes my problems with JS were outside Unity (I've never used this before)

    With respect to the fires - we are doing a simulator for evacuation from fire and hence need accurate info.

    Cheers

    Dave