2011/02/19 (Villainmad): Yes It Matters
I don't like to ask for help, but for some reason I don't mind complaining and seeing if anyone offers any.
So, I'm almost where I was in December, except that I haven't added the code for the player. I haven't gotten around to adding the collision-detection code yet, because I've run into (har har) another issue: there is a difference between doing something in Lua and in C#.
For the record, here's the entire "if the object's LuaTable has an Update function, execute it every frame" code:
// curObj and curEnt are respectively the "object-ID" and "game-entity" structs I described in the last entry.1
if (curEnt.Table["Update"] != null)
// There does not seem to be any other way of doing this besides creating a new "LuaFunction" object, casting the element from the LuaTable, AND THEN doing the .Call() method from that.
LuaFunction caller = (LuaFunction)curEnt.Table["Update"];
I noticed this when I tried to run the "if the object is out of the game-field, delete it" code in the Update() function, and discovered that the game was gradually slowing to a crawl. Well ... not "slow to a crawl," exactly, it was skipping a bunch of frames.2 After enough time had gone by, it was starting to run in seconds-per-frame instead of the other way around. With the aid of SlimTune, I determined that the primary resource-hog was the "LuaFunction.Call()" method. When I moved the "if it's out of the field, delete it" to the C# and got rid of the Update() function, the game ran perfectly fine.
Now, this happened even though the current test-script never has more than about 60 or so bullets at once, and even when the Update() function consisted solely of "return false." It also happens with both Update() and Draw() (when I tested it with that). Now, this isn't that much of a problem for bullets, which you're generally going to be handling in bulk and using Sequence-objects, but more complicated things like enemies and suchlike, or even things like Kogasa's "fade from one color to another" bullets ... yeah.
So, in lieu of any idea of how to make LuaFunction.Call()-from-C# take up less processing-time, after some thinking3 I've come to the conclusion that the way to do this is to send the list-array of objects back into Lua, and have it iterate through and update all the objects, on the basis that two LuaFunction.Call()s (one for "update" and one for "draw") is easier on the CPU than 100. The problem with that, of course, is how to get an array and/or a list into Lua. Not sure how well it would work with Lua's luanet.import_type(), either. (Random thought-which-just-occurred-to-me for how to speed up the LuaFunction.Call(): Is it because I'm passing two C# structs as the arguments?) I must do SCIENCE4 to figure this out!
1LuaInterface lets you use the functions luanet.load_assembly() and luanet.import_type() to use C# (by which I mean .Net) object-types within Lua.
2I'd rather figure out how to get XNA to slow down instead, but ...
3And false starts! For instance, while I was typing "handle it in bulk" above, I thought: "Hmm ... now there's an idea! Instead of calling
Update() once for every object, have it call
Update() only once, and have it iterate through every object instead! ... mmm ... no, wouldn't work. Too hard on the logistics, and it'll unerringly deal with things in the wrong order."
4By which I mean, methodical experimentation.
Previous: 2011/02/03 (Villainmad): Object-Disoriented Design
Next: 2011/02/28 (Villainmad): Changes!
Back to Villainmad
Back to My Games
Back to Main Page