views:

93

answers:

2

I am currently developing a C# .net XNA game engine.

I had a question about the design of the engine. First of all I would like to have my engine be able to incorporate my own scripting language. To do so I was thinking I would have to be able to access to all the properties of all the objects in the game, or just the ones I would like to change.

The first thing I started doing is trying to be able to access by strings. To do so I made every "Entity", something that can be drawn to the screen, have a dictionary of all the properties in it. The adding of the properties was done manually in the Entity class. I have since read that when you have a lot of dictionaries but not very many things in them it can slow down the GC. Which I believe is what's happening in my game right now. I can only get up to about 1000 Entities with basic properties, i.e. Color, Position, Texture, before it starts to lose fps.

I was wondering if there is any other way to be able to access an object's properties dynamically on the fly other than what I'm doing or using reflection, because that is even slower than the dictionaries. Or should I just rethink the entire design?

+1  A: 

I did something similar to this, although with Java, using JavaScript as the scripting language.

The way I had it working was basically exposing the objects to the scripting language directly. You should be able to call the methods on the classes directly from the scripting language, so I don't really see the reason to implement some odd dynamic property hack-thing.

Jani Hartikainen
what do you mean by exposing the objects to the scripting language?
Chris Watts
Most embeddable scripting languages allow exposing certain objects to the scripts. Basically you say "Make this C# object available to scripts with name X". Then, you can use X in the scripts, access any of X's methods etc., just like you'd do in C#.
Jani Hartikainen
+2  A: 

You could look into runtime compilation and simply use C# to write scripts. If you define a standard interface to your scripts, you can read, compile and run them when your engine is running, similar to this.

DrDeth
Here is one good scripting system: http://www.csscript.net/
Dan Bryant
I'm the author of the link in this reply. We've had great success using C# as our scripting language. It gives you the full power of C#/Visual Studio while letting you have specific control over what the script can access. I give the scripts access to my game objects through specific interfaces so I have control over what can and can't be changed.
Doolwind