- Home /
Call function from variable scripts
I really need a hand-holding answer for this. The direction people have pointed me are just not clicking for some reason. Usually I can grasp new concepts but for some reason, I'm just not comprehending what needs to be done. Something to do with interfaces I guess? Static class? I just don't understand, I blame the Christmas booze. I digress, the following works but as I understand it, it is not desirable.
Problem: I have a prefab NPC. I give it an activity master script. I want to drag and drop a new activity script on to the game object without having to modify the master script.
Existing Code: Master:
public class NPC_Choose_Activity : MonoBehaviour {
Dictionary<string, int> myActivities = new Dictionary<string, int>();
public void AddNewActivity(string activityName, int activityImportance){
myActivities.Add(activityName, activityImportance);
}
private void Update() {
if (!npcMaster.isBusy) {
ChooseActivity();
}
}
private void ChooseActivity() {
npcMaster.isBusy = true;
int highestImportance = 0;
string activityKey = "";
foreach (KeyValuePair<string, int> aKey in myActivities) {
if (aKey.Value > highestImportance) {
highestImportance = aKey.Value;
activityKey = aKey.Key;
}
}
SendMessage("IsThisMe", activityKey); // This seems bad. Is it bad? Sure seems like it
}
}
Drag and Drop:
public class NPC_TestActivityOne : MonoBehaviour
{
NPC_Choose_Activity npcMaster;
void OnEnable() {
npcMaster = GetComponent<NPC_Choose_Activity>();
npcMaster.AddNewActivity(this.GetType().Name, 500);
}
public void IsThisMe(string myName) {
if (this.GetType().Name == myName) {
GoDoTheThing();
}
}
void GoDoTheThing(){
// Every drag and drop script will have this function, that's why I think interface? I don't understand the implementation though
// Just do it
}
}
Second verse same as the first:
public class NPC_TestActivityTwo : MonoBehaviour
{
NPC_Choose_Activity npcMaster;
void OnEnable() {
npcMaster = GetComponent<NPC_Choose_Activity>();
npcMaster.AddNewActivity(this.GetType().Name, 1000);
}
public void IsThisMe(string myName) {
if (this.GetType().Name == myName) {
GoDoTheThing();
}
}
void GoDoTheThing(){
// And now for something completely different
}
void MoreFunctionsThatNoOtherScriptHas(){
// Wheeee
}
}
Questions. Those activities are on gameObjects already in your scene? Or are they being instantiated by something later on?
I'm currently building the prefab so all those scripts are being put on to a game object already in my scene. When I'm done, the whole thing will be a prefab so that when new NPCs are "born" they'll instantiate from this game object.
Hmm. Seems really rough when you have 20+ different activities.
Answer by SpaceManDan · Dec 28, 2018 at 12:13 AM
First if there is only one npcMaster ever in your game, go to your npcMaster script and do this
public static NPC_Master Instance;
private void Awake ()
{
Instance = this;
}
Then, instead of doing
NPC_Choose_Activity npcMaster;
private void Start()
{
npcMaster = GetComponent<NPC_Choose_Activity>();
}
like you have been, you can instead access the npcMaster without storing a reference, like this:
NPC_Master.Instance.anyPublicVariableOrFunction
Keep in mind you still need to define private from public variables in the master for them to be available.
Now, you need to make a standard activity class that uses inheritance.
public class NPC_TestActivity : MonoBehaviour
{
//NOTICE the keyword virtual here...
public virtual void OnEnable()
{
NPC_Master.Instance.AddNewActivity(this.GetType().Name, 500);
}
//NOTICE the keyword virtual here...
public virtual void IsThisMe(string myName)
{
if (this.GetType().Name == myName)
{
GoDoTheThing();
}
}
//NOTICE the virtual keyword here...
public virtual void GoDoTheThing()
{
IDoThings();
}
}
Be of the mind that what you put into NPC_TestActivity is only things you want ALL of the activities to do. But when you have one that needs some customs stuff you'll Inherit from it like so...
//Take out MonoBehavior and inherit from your own class instead
public class NPC_TestActivity_SpecialFunction : NPC_TestActivity
{
//NOTICE the override keyword here...
public override void OnEnable()
{
base.OnEnable(); //Runs the base code set inside of NPC_TestActivity OnEnable() function.
//You can just run the base code if you need it to behave the same as all the others or add more like so...
string moreStuff = "MAKE MORE STUFF IF YOU LIKE...";
string soMuchMoreStuff = "MAKE MORE AND MORE STUFF!!! WOOOO!!";
}
//NOTICE the override keyword here...
public override void IsThisMe(string myName)
{
//same here, just the base or add more if you like
base.IsThisMe(myName);
}
//NOTICE the override keyword here...
public override void GoDoTheThing()
{
//same here, just the base or add more if you like
base.GoDoTheThing();
}
//NOTICE no override necessary because there is nothing in the base class to override.
public void MySpecialTasksNOTForOtherNPCActivities()
{
DoStuff();
}
}
NOWWWWWwwww, you need to go to your npcMaster script and add a public List that you can store these activities.
public List<NPC_TestActivity> nPC_TestActivities = new List<NPC_TestActivity>();
Now, open the inspector and put the NPC_TestActivity_SpecialFunction script on a gameObject that stores it. For example NPC_GameObject. Now move over to the npcMaster script in the inspector and expand the new list we just created and change the number of elements you want to have in the list to reflect how many activities you want it to hold. Then drag NPC_GameObject(with the specialFunction script on it) into one of the elements you now have available... You just set up inheritance. CLAPPING!!!
Now in script you'll have access to your activities like this
nPC_TestActivities[0].MySpecialTasksNOTForOtherNPCActivities();
//or do this too
nPC_TestActivities[0].GoDoThing();
//You can access downward, but it doesn't work upward... watch the video linked at the bottom to get that explained I don'wanna right now.
Make sure that zero up there is a 1 or 2 or a 3 or a 4 or what ever number corresponds with the correct activity you set in the inspector. Remember that 0 is the first element, and 1 is the second and so on.
BAM!!!! DONE... Mic drop.
Answer by toddisarockstar · Dec 28, 2018 at 11:39 AM
you can just add the new script in runtime when you want your activity to happen. your triggered function would be the start function of the activity script. in the master script you just need to store the string name of the activity script for lookup.
public class masterscript : MonoBehaviour {
//type the name of your activity script here
//in the inspector
public string nameofscript;
public void DoActivity(){
gameObject.AddComponent (nameofscript);
// new script should be doing some stuff
}
}