Game Development Reference
In-Depth Information
child = TestScriptProcess:Create({frequency = 500});
parent:AttachChild(child);
AttachProcess(parent);
First, the TestScriptProcess class is created with the class() function. It
defines three methods: OnInit(), OnUpdate() , and OnSuccess() . The OnUp-
date() method counts to five and then calls Succeed()
to end
the process. The test code for this class creates two objects, one with an update fre-
quency of 1,000 and the other with an update frequency of 500. It then attaches
the child to the parent and attaches the parent to the Process Manager. You
a C++ method
'
ll see
the count from 1
-
5, each taking 1 second in between, followed by another count of
1
5, taking half a second each, before terminating.
As you can see, this class looks very much like any other class that inherits from
Process , which is the whole idea. Remember at the beginning of the chapter
when I said that one of the biggest reasons for using a scripting language is for
fast iteration? This is exactly how you achieve it. You can write processes very
quickly in Lua, test them out, and iterate very quickly. In many cases, you can
update a process without even restarting the game. You just edit the code, reload
the script (probably by calling ExecuteFile() , which is exposed to Lua), and
then trigger the process again.
If you decide that you need to move the process to C++ for performance reasons,
theLuaclassisalreadylaidoutliketheC++version.Youjustcreatethesame
class in C++, port the code from Lua to C++, and set up the triggering calls.
This is an extremely powerful and flexible system, and you should use it wherever
you can.
-
Inheriting from C++ Classes
The ScriptProcess class gives us one more thing that
s extremely
powerful: the ability to inherit from C++ classes in Lua. Without too much
trouble, you should be able to create a system that allows you to expose any
arbitrary class to Lua and enable Lua classes to inherit from them. I chose not
to do this here because such a system tends to make the details much more
obscure and harder to understand. My goal here is not to give you the best
engine I possibly can, but to teach you how you can build it yourself. I leave
this as an exercise to the reader. If you get stuck, you can always post in the
forums, and I
'
'
ll be happy to help.
By the way, you should always be on the lookout for these types of
abstractions. They can make the difference between an okay engine and an
amazing one.
 
Search WWH ::




Custom Search