Search Unity

Is it possible to edit/write script code from another script

Discussion in 'Getting Started' started by Bbpv, Feb 11, 2015.

  1. Bbpv

    Bbpv

    Joined:
    Feb 11, 2015
    Posts:
    5
    Hey guys, first time on the forums and in need of some help. I'll start with some context, I'm in uni and creating a game in unity, the game has a 'Spell-book' like script in which the behaviors of a 'spell' are defined, I need to create a tool so that I can have a panel for that script that allows you to edit current information about 'spells' such as cost of the spell and other variables, but i also need to have it so that the tool can add a new 'spell' and in doing so add new variables to the script. So if there a way in which i can edit/add to a normal C# scripts code with an editor script?

    Sorry for the length of the post.
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    You can not declare new variables at runtime. What you CAN do, though, is add items to an ArrayList, or something like that.
     
  3. Bbpv

    Bbpv

    Joined:
    Feb 11, 2015
    Posts:
    5
    I don't mean at runtime of the actual game but just whilst unity is running, or is that what you meant?
     
  4. Bbpv

    Bbpv

    Joined:
    Feb 11, 2015
    Posts:
    5
    Also not sure if i should post a new thread for this, but can i treat a script like a text file and use the Windows class to write to a script and treat it like a .txt file? would it still work and compile in unity? Not at runtime through just as im using unity, if that makes any sense :)
     
  5. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    Totally doable. I have built a couple of editor tools that exactly that. As mentioned, you can't do it runtime, but since you don't need to, its not a problem.
    Yes, that's how you do it.
     
    Bbpv likes this.
  6. Bbpv

    Bbpv

    Joined:
    Feb 11, 2015
    Posts:
    5
    Thanks for the help man :) can get on with work now :)
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,190
    Last edited: Feb 11, 2015
    Bbpv and R-Lindsay like this.
  8. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    OK you need to build a spell system, the spell book will hold a list of spells, and those spells will have 'effects'.

    So you just need to make a Spell class, with spell names, and probably best to have an Effects class that will trigger the effects of the spell, for instance a spell could shrink the player but double their hit points. So a Mighty Mouse spell would have a shrink effect and a mighty effect.

    This looks like a comprehensive RPG tutorial -> http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial

    And if you go down to item 278 he goes over the spell system he uses. http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial

    Note just googled for the tutorial so not sure how good it is, also you might find RPG toolkits on the asset store with spell systems.
     
    Bbpv likes this.
  9. Bbpv

    Bbpv

    Joined:
    Feb 11, 2015
    Posts:
    5
    Yeah I have a set-up like that, the 'spell-book' well it's called 'Spell Repository' is the effects class that you mentioned and I have a seperate script called 'Casting System' to handle the casting of spells and such but I'll take a look at the links provided, same with Ryiah, thanks for all the help guys :)
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You'd be surprised how much flexibility you can achieve with regular delegates and variables.
     
  11. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    Well sure... But writing code to code is just cool!
     
    Kiwasi likes this.
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Totally. But I don't think it meets the use case. Not unless you plan to allow moding on a heavy basis.
     
    zombiegorilla likes this.
  13. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    Yea, you are probably correct. We do in a few cases, but they are all for pipeline and asset management, not the actual game.