Importing data from classes?

I have a question about classes. I am working on an RPG, and rather than have a separate script for each enemy monster, I am just going to have a "monster" class that serves as the template for each monster, then have each monster be a variant on that class. Then, I am going to have a template "monster" script, so when I want to spawn three "fire monsters" I was just going to have the variables in the monster script be filled with the data from the fire monster class variant. My question is, how can I have the variables in a script automatically be filled with the data from a class? Also, how can I use that in conjunction with instantiate (so I could spawn the monster template prefab and then control what class variant it is filled with).

Here is an example of what I want: The Monster template class

class Monster{
    var name;
    var HP;
}

Here is the Fire Monster Sub class

var fire_mon = Monster();
fire_mon.name = "Charmander";
fire_mon.HP = 20;

Here is what the monster script (which would be attached to the monster prefab).

var name = String;
var HP   = Int;
//other code

and here is what I would want that instance of the monster script to look like after I have applied the fire_mon class variant:

var name = Charmander;
var HP   = 20;
//other code

I know that was a long and complex question, thanks :)

This is how subclassing works.

so for example:

class Firemonster extends Monster { ... things specific to Firemonster... }

It automatically has the Monster variables, and setting them, well, sets them.

Unless I completely misunderstood.

I’m not sure if I understand your question correctly do you mean to ask…:

  1. Given the class MonsterA, how do you create a new instance of a Monster class (e.g. MonsterB) using the values of MonsterA as the defaults?
  2. Given a script for use with a prefab, you want to have a reference to the Monster class (or its attributes) when you assign MonsterA or MonsterB to the script?

As a heads up, I mostly code in C# since it gives me a bit more flexibility, so my answer(s) will be using C# syntax, it should be possible to migrate the concepts presented by this code over to JavaScript and Boo without too much trouble though.

For (1), you can achieve this by cloning the object, done by either using a copy constructor or an explicit clone method.

For (2) you have a couple of options, you can A) copy the values off of the monster object into the script exposed variables or B) you can use properties and expose them to the UnityEditor through attributes.

For (A):

public class MyMonsterScript {
  [HideInInspector]
  private Monster monster;

  private int monsterHp;
  private string monsterName;
  ...
  public void setMonster(Monster monster) {
    this.monster = monster;
    this.monsterHp = monster.getHP();
    this.monsterName = monster.getName();
  }
}

For (B):

public class MyMonsterScript {
  [HideInInspector]
  private Monster monster;

  [ExposeProperty]
  public int HP {
    get { return monster.getHP(); }
    set { monster.setHP(value); }
  }

  [ExposeProperty]
  public int Name {
    get { return monster.getName(); }
    set { monster.setName(value); }
  }

  public void setMonster(Monster monster) {
    this.monster = monster;
  }
}

Solution 2-B requires the usage of the following, found on the Unify Community site: Expose Properties in Inspector.

Regardless of your approach, you can now call: MyMonsterScript.setMonster() with any class that inherits its behavior from the base Monster class, and either attributes of the Monster will be exposed for modification in the Editor Inspector via reference or directly as field values. This is obviously a bit over-simplified, but hopefully you get the idea. Just make sure that they are inheriting from the Monster class, otherwise you’ll receive ClassCastExceptions and invalid argument exceptions.

If you want to expose the actual Monster-reference member, so that you can change that through the inspector, that becomes a little bit more tricky.

Sorry if this is a bit late, and you’ve already figured out the solution to your problem. Hopefully it helps, I tried to base my answer on your initial question and the clarification of it in the earlier answer’s comments.