public InputObject DefineObject(string name, string className, object[] initArgs) { // first, try to find this class className = className.ToLower(); InputClass c = GetClass(className); if (c == null) { throw new Exception("Class '" + className + "' not found when constructing object '" + name + "'."); } name = name.ToLower(); InputObject o = c.PrintObject(name); if (name != "anon") { Objects[name] = o; } // if it has an initializer, run it now if (o.Implementation.Funcs.TryGetValue("f:init", out Exline init)) { init.Execute(initArgs); } return(o); }
public void ManualObjectTest() { InputContext context = new InputContext(); Exline e1 = new Exline("v:hp := 100; f:damage := {v:hp -= a:0};"); context.CompileLine(e1); e1.Execute(new object[] { 0.0 }); InputClass testClass = new InputClass("test", context); InputObject testObj = testClass.PrintObject("a"); // see that obj has its own var and func implementations and scoping works Exline e2 = new Exline("f:damage(15)"); testObj.Implementation.CompileLine(e2); Assert.AreEqual(new InputVar("v:hp", 85.0), e2.Execute(new object[] { 0.0 })); Assert.AreEqual(new InputVar("v:hp", 70.0), e2.Execute(new object[] { 0.0 })); // but also see that the original class is unaffected by the lines we just executed Exline e3 = new Exline("f:damage(15)"); testClass.Definition.CompileLine(e3); Assert.AreEqual(new InputVar("v:hp", 85.0), e3.Execute(new object[] { 0.0 })); }