Game Development Reference
In-Depth Information
Speed and Memory Costs
All of this power comes at a cost. Scripts are generally very slow when compared to
C++, and they take up more memory, which is one of the main disadvantages to
using a scripting language. When you load a script, the interpreter typically parses
the actual text right then and there and then executes the instructions found within.
Many languages give you a way to speed this up by allowing you to compile the
script into a binary form as an offline process, but a script
s execution is always
going to be slower. Crossing the boundary between C++ and script is also very
slow, especially if you
'
'
re marshalling a large amount of data. For these reasons, you
would never want to attempt to write a high performance graphics engine or physics
engine using a scripting language; these systems are best saved for C++.
Where
s the Line?
One of the pitfalls I see a lot of developers make is trying to do too much in the
scripting language. Scripting languages are extremely powerful, and it can be tempt-
ing to try to write huge, complex systems entirely in script. In the end, I
'
'
ve found
that this doesn
t really buy you anything. By their very nature, scripts tend to be
more difficult to maintain than a language like C++. For example, take a look at the
process system you saw in Chapter 7, Controlling the Main Loop. It would have
been possible to implement that entire system in Lua or Python much faster than
C++, but this is the kind of system that can potentially do a lot of work every single
frame. Furthermore, once it
'
'
s built, it
'
s pretty unlikely that you
'
ll need to iterate on it.
You don
t lose very much by having it in C++, and you gain a considerable amount
of performance. You could certainly prototype it in script, but this is the kind of sys-
tem that you
'
d eventually want to move out to C++.
As a counter example, let ' s suppose you want to make an RPG similar to Dragon Age or
Skyrim. You decide that you
'
ll need a quest system that can manage what quests the
player is on, the state of each quest, all the flags and items associated with them, etc.
This entire quest system can and should be implemented completely in script. This is
the kind of thing that you will be constantly iterating on and will want to have the abil-
ity to tweak on the fly. It
'
s also not a system that needs to be constantly updating every
frame because it will most likely just respond to events or potentially timers, so the per-
formance gain of having this type of system in C++ doesn
'
'
treallybuyyouanything.
So where
s the line between systems that should live in the script and systems that
should live in C++? The answer to this really depends on who you ask. I know devel-
opers who think only truly time-critical things should ever be in C++, and everything
else should be in script. I also know developers who think script should be more for
'
 
 
 
 
Search WWH ::




Custom Search