Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not sure you've got the right end of the stick with the dynamic keyword. It doesn't by itself let you interpret new code at runtime. All it does it let you bypass static type checking by delaying the resolution of operations until runtime.</p> <p>If you're looking to "script" your game, you probably want to take a look at integrating Lua, IronPython, or one of the other DLR languages:-</p> <ul> <li><a href="https://stackoverflow.com/questions/1067784/c-net-scripting-library">C#/.NET scripting library</a></li> <li><a href="https://stackoverflow.com/questions/1821866/ironruby-and-handling-xaml-ui-events">IronRuby and Handling XAML UI Events</a></li> </ul> <p>Otherwise, the usual thing to do is have something along the lines of:-</p> <pre><code>interface IBehavior { // Binds whatever events this behaviour needs, and optionally adds // itself to a collection of behaviours on the entity. void Register(Entity entity); } // Just an example public abstract class TurnEndingDoSomethingBehavior { public void Register(Entity entity) { entity.TurnEnding += (s, e) =&gt; DoSomething(); } private abstract void DoSomething(); } </code></pre> <p>The question is, do you want to be able to add entirely new behaviours after compile-time? If so you'll need to expose some or all of your game-state to a scripting language.</p> <p>Or is it sufficient to be able to compose existing behaviours at runtime?</p> <hr> <p><strong>After your edit</strong></p> <p>I'm still unsure, to be honest, about your requirement for the dynamic keyword and the DLR. Your game's launcher can download a class library full of behaviours just as easily as it can pull down a set of scripts! (That's what Minecraft's launcher does if memory serves)</p> <p>If you absolutely must use the DLR then take a look at the links I posted. You'll have to expose as much of your game state as necessary to one of the DLR languages. Events get exposed as first-order-function properties. You shouldn't even need the "dynamic" keyword for basic stuff.</p> <p>Quick example in IronPython:-</p> <pre><code>def DoSomethingWhenDamageTaken(*args): #do whatever... player.DamageTaken += DoSomethingWhenDamageTaken </code></pre> <p>The player class:-</p> <pre><code>public class Player { public event EventHandler DamageTaken; // ... } </code></pre> <p>You set it up like:-</p> <pre><code>ScriptEngine engine = Python.CreateEngine(); ScriptRuntime runtime = engine.Runtime; ScriptScope scope = runtime.CreateScope(); // In an actual application you might even be // parsing the script from user input. ScriptSource source = engine.CreateScriptSourceFromFile(...); Player p = new Player(); scope.SetVariable("player", p); source.Execute(scope); </code></pre> <p>Some links to get you started:-</p> <ul> <li>IronPython: <a href="http://ironpython.net/" rel="nofollow noreferrer">http://ironpython.net/</a></li> <li>IronRuby: <a href="http://www.ironruby.net/" rel="nofollow noreferrer">http://www.ironruby.net/</a></li> <li>Lua: <a href="http://www.lua.inf.puc-rio.br/post/9" rel="nofollow noreferrer">http://www.lua.inf.puc-rio.br/post/9</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload