Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Downloading a script at runtime.

Discussion in 'Scripting' started by dylankarrdotcom, Aug 11, 2011.

  1. dylankarrdotcom

    dylankarrdotcom

    Joined:
    Aug 4, 2011
    Posts:
    2
    Hi,

    I'm working on a sort sandbox game right now based on little objects (called cells in this game) that do different things when the player 'programs' them to. Like the cell could change color when ever it's hit by another cell, or it could play a musical note when it receives a 'charge' from another cell (simulated electricity is also a big part of this game). And I was wondering if anyone could think of a way to allow the user to download more functionality for their cells during runtime.

    Basically, can anyone think of a way to download a script, specifically a unityscript, during runtime and add it to the assets without using asset bundles, since I don't have pro. Is there any way to do this using the WWW class?
     
  2. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    You can do this, but not on iOS, and this is a very advanced mechanic (akin to runtime game patching). It uses Reflection and storing/loading a pool of assemblies, as well as operating a large portion of your game entirely on reflection. There are a couple basic helper posts to get a single script able to be loaded at runtime, I tried to point someone in the right direction here: http://forum.unity3d.com/threads/92922-build-unity-game-from-external-app

    It's a ton of design and extensive work to load external script modules of gameplay, it would be easier to come up with as many cell behaviours as you can, and then add variations via XML, (maybe use the Animation system to toggle arbitrary values for each cell, then you upload different animations after the game starts, the limit being you can only modify things designed to be modified on release).

    Edit: looking a bit more, if it's strictly the cells, you may be able to use a lot of what I mentioned in that other link (about interfaces, and defining a Cell Interface, and loading all Cell types from the newly created Assembly, and running your Cells entirely through their Interface).
     
    Last edited: Aug 11, 2011
  3. dylankarrdotcom

    dylankarrdotcom

    Joined:
    Aug 4, 2011
    Posts:
    2
    Thanks, this is exactly what I need.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can use eval() in JS to execute arbitrary code at runtime (not on iOS), so you don't need to mess around with reflection (as long as you use JS; it's not available in C#). But that's a big potential security hole. You'd have to spend a lot of time writing code to clean all strings downloaded, to the extent that you're probably better off defining an interface and using that.

    --Eric
     
  5. BernieRoehl

    BernieRoehl

    Joined:
    Jun 24, 2010
    Posts:
    80
    Except that the eval() function doesn't seem to work properly in the web player. I've found several things that work fine in the editor and in standalone executables, but don't work in the webplayer.
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    for security reason realtime compilation and alike have limitations on other platforms than standalone

    eval though should work given you eval something that does not use something thats not present webplayer as such (run the same code as real code and see if it fails on compilation or in webplayer and you know for sure)

    on the webplayer you always also have the option to use ExternalEval and run real javascript code inside the webpage for example depending on what you want to do
     
  7. BernieRoehl

    BernieRoehl

    Joined:
    Jun 24, 2010
    Posts:
    80
    The code I'm eval'ing works fine in both the editor and the web player -- no compile errors, runs as I would expect. It's only when I eval() it that I run into problems.

    Simple example:

    eval(" 7 == 12" ) works in the editor, standalones and webplayer.

    eval(" \"seven\" == \"twelve\" ") works in the editor and standalone, but causes an internal compiler error in the webplayer:

    CompilationErrorsException: script(1,10): BCE0055: Boo.Lang.Compiler.CompilerError: Internal compiler error: Attempt to access a private/protected method failed
    .. ---> System.MethodAccessException: Attempt to access a private/protected method failed.

    I've run into similar problems with hashtables, calling functions, etc etc.
     
  8. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Take that example and report it through the bug reporter

    I think thats definitely a bug as reference comparision or "string.Equal(otherstring)" should never fail

    people aren't that often using eval for different reasons (like not fast, limited in general and being a hell not save as your users can eval whatever they want if you expose it and don't verify the input - which is a work that large that you could do the execution in your parser at the end too for maths)
     
  9. BernieRoehl

    BernieRoehl

    Joined:
    Jun 24, 2010
    Posts:
    80
    Okay, will use the bug reporter. Thanks!
     
  10. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    What he said. Think of small behaviours and effects that can be chained together or layered, then use XML or other data format to specify which behaviours and effects to use on a particular cell. Basically what you're asking for is a programmable particle system but as any competent game programmer will tell you, 99% of what you want to do with particles can be achieved just by adjusting a few parameters. The other 1% costs time and money, taking valuable resources away from the other features of the game. Figure out where your 99% of cells overlap, and do those, cut out the remaining 1%. I suggest you sit down and start designing your cells, maybe prototype a few types first to see what you want, before going full bore on a fully programmable, dynamically compilable system.