Call a function from a text file?

I’m creating an event/dialogue system and am wondering if there is a way to load a text file that just calls a function with some arguments, such as:

public TextAsset textFile;
string[] lines;

void Start()
{
	if(textFile != null){
		dialogueLines = (textFile.text.Split ( '

’ ));
}

	for(int i = 0; i < dialogueLines.Length; i++){
		//run each line of code
		//in the example the lines in the text file would be
		//set up like this:
		//method(false, "Speaker Name", "A string of text.");
	}
}

You can use [StartCoroutine][1] to call a method by it’s name as a string, but it normally only accepts one parameter that you can pass to a method. This isn’t a problem because you can pass it arrays and use an [object][2] array to handle multiple data types, it doesn’t look nice but it works.

I’m not sure if this is the best way to do a dialogue system, what I’ve written will execute all the functions sequentially with no delay, though perhaps you can set up the text file reading in a Coroutine and wait to read & execute the next line until user input.

The format of the text file in this is “MethodName,param1,param2” and so on, so you can split each line by the comma and have the method name always at index 0 and parameters following. You’ll want to pick a different character or string like “||” so you can use commas in the text though. Now you must determine what data type each parameter is to convert it and then pass it to the method. There may be a more efficient way, but here it attempts to convert the string to common data types and if it is successful it will return that type (thanks [callisto][3]). There may be some issues with integers and floats, 10 may convert to an integer as well as a float (also with 10.0?) depending on the order of the conversion if statements but I’m not sure. Then based on the type it returns, convert to that type and throw it into an object array which you then pass to the method, and cast from object to your desired type and use it.

using UnityEngine;
using System;
using System.Collections;

public class stringmethods : MonoBehaviour {
    enum DataType
    {
        Boolean,
        Int32,
        Int64,
        Double,
        Single,
        String
    }
	
    DataType GetDataType(string input){
        bool boolValue;
        Int32 intValue;
        Int64 bigintValue;
        double doubleValue;
        float singleValue;
		
        // Place checks higher to give higher priority to type.
        if (bool.TryParse(input, out boolValue))
            return DataType.Boolean;
        else if (Int32.TryParse(input, out intValue))
            return DataType.Int32;
        else if (Single.TryParse(input, out singleValue))
            return DataType.Single;
        else if (double.TryParse(input, out doubleValue))
            return DataType.Double;
        else if (Int64.TryParse(input, out bigintValue))
            return DataType.Int64;
        //If no conversions succeed it is still a string
        else return DataType.String;
    }

    object[] ConvertTypes(string[] arguments){
        object[] output = new object[arguments.Length];
        for(int i=0; i<arguments.Length; i++){
            switch(GetDataType(arguments*)){*

case DataType.Boolean:
output = Convert.ToBoolean(arguments*);*
break;
case DataType.Double:
output = Convert.ToDouble(arguments*);*
break;
case DataType.Int32:
output = Convert.ToInt32(arguments*);*
break;
case DataType.Int64:
output = Convert.ToInt64(arguments*);*
break;
case DataType.Single:
output = Convert.ToSingle(arguments*);*
break;
case DataType.String:
output = arguments*;*
break;
}
}
return output;
}
*//Methods called from text file *
void NoParameters(){
Debug.Log(“No parameters!”);
}
//Parameters: string, float
void SomeMethod(object[] parameters){
string text = (string)parameters[0];
float number = (float)parameters[1];
*Debug.Log(text + " - " + number); *
}
//Parameter: float
void AnotherMethod(object[] parameter){
float number = (float)parameter[0];
Debug.Log((number * number).ToString());
}

void Start () {
//Dummy input
string inputLine1 = “NoParameters”;
string inputLine2 = “SomeMethod,stringArgument,15.0”;
string inputLine3 = “AnotherMethod,10.0”;

//First line in text file
string[] inputs = inputLine1.Split(new char[]{‘,’});
if(inputs.Length == 1){
//No parameters, simply call a function
StartCoroutine(inputs[0]);
}
else if(inputs.Length > 1){
//Multiple parameters, must convert from string to data types
//First index is the method name, so remove it.
string[] parameters = new string[inputs.Length - 1];
for(int i=0; i<inputs.Length - 1; i++){
parameters = inputs[i + 1];
}
object[] paramsForMethod = ConvertTypes(parameters);
StartCoroutine(inputs[0], paramsForMethod);

}

//2nd line
inputs = inputLine2.Split(new char[]{‘,’});
if(inputs.Length == 1){
StartCoroutine(inputs[0]);
}
else if(inputs.Length > 1){
string[] parameters = new string[inputs.Length - 1];
for(int i=0; i<inputs.Length - 1; i++){
parameters = inputs[i + 1];
}
object[] paramsForMethod = ConvertTypes(parameters);
StartCoroutine(inputs[0], paramsForMethod);
}

//3rd line
inputs = inputLine3.Split(new char[]{‘,’});
if(inputs.Length == 1){
StartCoroutine(inputs[0]);
}
else if(inputs.Length > 1){
string[] parameters = new string[inputs.Length - 1];
for(int i=0; i<inputs.Length - 1; i++){
parameters = inputs[i + 1];
}
object[] paramsForMethod = ConvertTypes(parameters);
StartCoroutine(inputs[0], paramsForMethod);
}
}
}

So rather than writing it out like that you would be looping through the text files lines and replace inputLine# with the current line. With those 3 methods & dummy text input the output is:
![alt text][4]
After rereading this I think you can optimize it, rather than using TryParse and returning a DataType enum element you could return the successful conversion instead and put that directly into the output array. I’m too lazy to test it but I think if a method’s return type is object you can return any value but must ensure whatever value is receiving the casted object is of the same type.
*[1]: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html*_
[2]: Microsoft Learn: Build skills that open doors in your career
_[3]: http://stackoverflow.com/a/5325687*_

_*[4]: http://i.imgur.com/Jz0QK8X.jpg*_