Jump to content

  • Log In with Google      Sign In   
  • Create Account


Execute piece of text


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Arthur Souza   Members   -  Reputation: 1417

Like
0Likes
Like

Posted 15 August 2012 - 02:23 AM

So, I'm writing a game in XNA. If I want to script actions and Npcs using C# itself, is it possible to run just a small piece of code from a string?

I mean, having a string that contains nothing but one or a few lines of code, calling methods and passing parameters using members accessible from the calling context, is that possible?

Thanks.

/feeling stupid

A.

Lotus - Action RPG In development http://www.gamedev.n...die-rpg-engine/ |
Personal blog In Portuguese: lotuzgames.wordpress.com |


Sponsor:

#2 Andy474   Members   -  Reputation: 669

Like
1Likes
Like

Posted 15 August 2012 - 03:22 AM

you can

for instance lets say you have this function

public void Speak(string Text) {
	 //NPC Says the text ...
}
public void Dance() {
	 //Does an animated dance;
}


you can store this as a Delegate in a Dictionary (Dictionary<string, Delegate>) where the key is the function name and delegate is the function name.
you add a function to this dict like so

Dictionary<string, Delegate> myFunctions;
myFunctions.Add("Speak", new Func<string>(Speak)); //Adds the speak function (Func = Function with params)
myFunctions.Add("Dance", new Action(Dance)); //Adds the dance function (action = no Params)

//Read Speak("Hello World"); from file npcscript.txt
string scriptLine = readfromaFile();
//scriptline = "Speak("Hello World")"
// do some stuff to split the string into th following array
string splitted = {"Speak", "Hello World" }
string args = splitted[1..n]; //all the other things are params

myFunctions[splitted[0]].DynamicInvoke(args);

N.B. i dont have the code i used, but i used it for an ingame commandline system and i seemed to work ok (there is probally a better way). there may be some errors as i am only doing this from memory, but just google stuff and it'll turn up somthing.

#3 Arthur Souza   Members   -  Reputation: 1417

Like
0Likes
Like

Posted 15 August 2012 - 03:30 AM

Thats the kind of thing I was thinking that would most likely be possible, by invoking the methods dinamically, will put some thought into it, thanks for the support

A.

Lotus - Action RPG In development http://www.gamedev.n...die-rpg-engine/ |
Personal blog In Portuguese: lotuzgames.wordpress.com |





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS