954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dynamic methods

Hello,


I'm creating a game editor and I'm at the current stage that I would like the ability to attach scripts to objects (by selecting the string of the method name to be called). That would be saved into the object's scripts to load / run array. That shouldn't be too difficult, the part I'm having trouble with is finding a method to dynamically call functions at runtime based on what script they need to run. Keep in mind this "script" is just C# code.

I need to run the scripts efficiently at game runtime and I can't have complicated lookups to the functions. Is there some way I can do this? I'm thinking delegates / reflection but I'm not sure how to implement any of it.

There's not really any code yet for this specifically but I'll provide whatever is deemed necessary for a reply.

BigTito89
Newbie Poster
14 posts since Dec 2009
Reputation Points: 33
Solved Threads: 1
 

Are the methods predefined or are they created at runtime?

Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

Although it would be cool if they are generated at runtime, I'm not going to bother with that yet. The are going to be predefined in a scripts folder.

BigTito89
Newbie Poster
14 posts since Dec 2009
Reputation Points: 33
Solved Threads: 1
 

The easiest way is to create a delegate. Then use this delegate to create an array of your 'scripts' and use that to call the methods.

You can also use reflection to find the methods and call them, but that takes more work :)

Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: