- Home /
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 :)
Answer by DaveA · Mar 29, 2011 at 12:19 AM
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.
Not to be rude, but you kinda did (I am not very clear when I ask my questions). I am wondering, once I hav e the Firemonster sub class, how can I have the values in that subclass fill up public variables in another script (so it would be as if I had types out the variables in my script inside the editor).
Do you mean setting vars in other scripts like at the bottom of this page: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Components.html
$$anonymous$$inda...I want to use one script to change the variables in another, but using classes. The class and the script I want to change the variables in (lets just call it script B) have exact the same variables in them. So, I am wondering how I would say "spawn an object that has the monster template script attached to it and fill up its data using the fire monster variant of the monster class".
Answer by swquinn · Aug 26, 2011 at 08:39 PM
I'm not sure if I understand your question correctly do you mean to ask...:
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?
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.
Your answer
Follow this Question
Related Questions
Override a serialized variable from a parent class? 3 Answers
hot to make classes and why ? 1 Answer
Audio Mixer Class is empty. 0 Answers
Trying to change a subclasses' variable 1 Answer
How would I access a Class Variable from another script? 1 Answer