I am checking to see if there is a way to convert a public string to a call method of a type. For my questing system I have created a Database System that incorporates all of the information for the particular quest, but I am lacking the capability of adding it through an easy to use script. The problems I am running into is the inability to convert a string to a call method, that is of the “Quest” type.
using UnityEngine;
using System.Collections;
public static class QuestDatabase
{
public static Quest Apple()
{
Quest quest = new Quest();
quest.Name = "Apple";
quest.Description = "I really could go for an apple, mind acquiring one for me?";
quest.Value = 5;
quest.ExpValue = 10;
return quest;
}
}
using UnityEngine;
using System.Collections;
public class QuestBehavior : MonoBehaviour
{
private Quest quest;
public string questName = "Apple";
void Start ()
{
quest = QuestDatabase.Apple();
}
void OnGUI()
{
if(GUI.Button(new Rect( windowWidth * .7f, windowHeight * .8f, windowWidth * .25f, windowHeight * .15f), "Accept"))
{PC.Instance.Quest.Add(quest);}
}
If I understand correctly, you want to do something like:
questName ="Apple";
//something to call QuestDatabase.Apple();
I think about the only option is to do a if/else or switch statement on the questname, either in the QuestBehavior or the QuestDatabase script:
QuestBehavior.Start(){
switch(questName)
{
case "apple":
QuestDatabase.Apple();
break;
}
}
OR
QuestDatabase.AddQuest(string questName){
Quest quest = new Quest();
switch(questName)
{
case "apple":
quest.Name = "Apple";
quest.Description = "I really could go for an apple, mind acquiring one for me?";
quest.Value = 5;
quest.ExpValue = 10;
break;
default:
break;
}
return quest
}
(Although it’d probably be better to use a list or an enum instead of switching on a string)
You could use Invoke and set the call time to zero.
For this example-
Invoke(quest.Name, 0);
The problem I was running into was that the quests are stored in a database. What I ended up doing was creating enums for the quest names for easy calls and what not and if ever I needed to convert the enum to string or string to enums I would use:
QuestNames parsed_enum = (QuestNames)System.Enum.Parse( typeof( QuestNames ),questName[cnt] );
Quest quest = QuestDatabase.Quest(parsed_enum);
This converts the string to the enum equivalent and made it easy to save and load the data whenever needed.