mercredi 24 juillet 2019

How to call projectwide properties using Reflection?

I am building a newsfeed system for a game, and I have a file which contains various string messages that the system reads in to show them on screen.

Strings like that are not the problem: "Welcome to Trash Island"

But I would like to add some dynamic text that includes some specific variables from classes or non-void functions that return their result to the defined position of the message:

Example: "Time: [SHOW CURRENT TIME]" should give "Time: 10:23pm"

I think it could be good enough to use tags that mark the code. To implement the format of the example above I have come up with this:

"Time: < code=UIManager.ToTime(DayManager.instance.dayTime)/>"

//Note: daytime is a float value and ToTime returns a time formatted string

This is my approach, but of course not working:

string msg = messages [_index];
string prefix = "";
string subfix = "";
string code = "";
if (msg.Contains ("<code=")) {
    int start = msg.IndexOf ("<code=")
    prefix = msg.Substring (0, start);
    start += 6;
    end = msg.IndexOf ("/>");
    code = msg.Substring (start, end - start);
    sufix = msg.Substring (end+2, msg.Length-1);
    Debug.Log ("Function found: " + code);
    string codeReturnValue = //DO MAGIC REFLECTION WITH "code" 
    text_message.text = prefix + codeReturnValue + subfix;
} else { 
    text_message.text = msg;
}

But how would I turn "code" to an actual call using reflection?

I think this is not possible, but how would I manage something like this without having to create tons of hard coded functions and instead gather all information based of the message-string?





Aucun commentaire:

Enregistrer un commentaire