Search Unity

Load arbitrary script at runtime?

Discussion in 'Scripting' started by chrischris, Dec 12, 2007.

  1. chrischris

    chrischris

    Joined:
    Oct 18, 2005
    Posts:
    19
    Is there a way to load an arbitrary script at runtime and attach it to an object? The script source would, for example, be read from a file at runtime, or even entered through a GUI.

    Thanks!
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    'fraid not. All of the scripts are compiled to bytecode before the game is run in the editor, and when the game is built, so there is no way to dynamically load script files into the system. Mono isn't an interpretive scripting system.

    You can, however, write your own script system if you can handle the headache and the overhead, but I don't recommend it.

    Perhaps use data files for a happy medium.

    -Jeremy
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Javascript does have eval() now in Unity 2.0, which can run arbitrary strings as code during run-time. I haven't played with it much, but I wasn't able to get it to work outside the editor (works fine in the editor), which doesn't surprise me. But then I might have been doing something wrong.

    --Eric
     
  4. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    If all you want is very very basic commands, it's not too hard to just parse a file yourself.

    You can even use XML if your commands can be defined in such a structure.

    -Jeremy
     
  5. chrischris

    chrischris

    Joined:
    Oct 18, 2005
    Posts:
    19
    Cool, thanks for the infos.