ImplementingGameplayElementsUsingGamebryo
Chapter1:ASimpleFrameworkforaGameApplication1
Chapter1–ASimpleFrameworkforaGameApplication
Motivation
Novicegamedevelopers,inspiredbythelatestgamedevelopedbyCrytekorLionheadorEA,may
wanttobeginprogramminggameplaywithoutthinkingaboutthedesignandarchitectureofthe
game application. They want to work on code that makes the game fun! You might be able to
create a simple
game without considering the software design; however, if you try to build a
complex game by adding features to ad‐hoc code, you will find that the cost to develop a new
featureincreases overtime
1
.Further, thecode willlikelybecomemore fragile,whichcanleadto
costoverruns andprojectfailure. Wewanttoteachyouhowtouse theGamebryosystem,while
atthesametimeillustratingsomeofthedesignelementsthatyoushouldconsiderwhenbuilding
yourgame.
You may be
interested in constructing gameplay prototypes, developing a game as part of an
academicproject,ordevelopinghighqualitycommercialgames.Inanyofthesecases,whenyou
choosetobuildyourgameusingGamebryo,youarenotchoosingtomodanexistingshrink‐wrap
gamebyaddingyourowncontent
andtweakingAI.Youwillbebuildingyourowncustomgame
enginefromhighquality,robustcomponentsthathavebeendesignedtoallowyoutheflexibility
thatthemodcommunitydoesnotenjoy.Thegoalofthistutorialseriesistoteachyoutodevelop
gameplay features using Gamebryo, the centerpiece
of your custom game engine. Each chapter
will illustrate some Gamebryo feature, showing you how to integrate it with third‐party open‐
sourceprojectsandyourowncode.
Chapter 1 will begin to walk you through the development of some of the software needed to
assemble a simple game engine
using Gamebryo and other available components. The text will
describeconceptsanddocumentthedevelopmentofasmallfragmentofagamethatimplements
the concepts. The code developed throughout this series is meant only to be illustra tive, fairly
easy‐to‐readandcomprehend.Itisnotintendedtorepresentthe
currentbest‐of‐breedpractices
inthegameindustry.Wewillprovidewitheachchapterasetofrecommendedreferenceswhere
you can explore other ideas. The game development community is quite open, and it is easy to
1
Note that the gameplay‐first approach to game development can play an important role in the
successofalargeproject, forexamplewhen usedtobuildprototypesasa partofpreproduction.
SporedeveloperMaxis hasincorporat edgameplayprototypingintotheirdevelopmentprocessfor
manyyears,especiallyinthe
earlyphasesofdevelopment,andthepopularityoftheirpriorgames
isevidencethattheirapproachissuccessful.Themodcommunitiesthatgrowout ofthefanbase
ofmanyfirst‐personshootersarealsoabletoquicklybeginworkingongameplaywithoutconcern
for software architecture, In this case, somebody
else created the architecture for them, and
thoughthisenablesgameproductiontobeginquickly,theabilitytoaddnewfeaturesislimited.
ImplementingGameplayElementsUsingGamebryo
Chapter1:ASimpleFrameworkforaGameApplication2
learn from other developers by reading the proceedings of the Game Developers’ Conference or
otherindustryconferencesthatoccureachyeararoundtheworld.So,withthat,letusbegin!
GameasaStateMachine
Theconceptofafinitestatemachineappearsinmosteverygamedesign.Itisperhapsmostoften
associated with artificial intelligence (AI), though developers do not implement AI using state
machines exclusively and often use state machines for algorithms other than AI. The most
fundamentaluseofthestatemachine
inagamehasnothingto dowithgameAI.TheWikipedia
pageforafinitestatemachine
statesthat“Afinitestatemachine(FSM)…isamodelofbehavior
composed of a finite number of states, transitions between those states, and actions.” This
definitionmightseemratherabstract,butitiseasytomakesenseofthis.Considerthefollowing
characteristics,gamescreensinatypical
game:
• Theopeningsplashscreenthatappearswhenyoufirstputthe gameCDintotheconsole
andturntheconsoleon:thegame’s“boot”screen(StartupSplash)
• The main game user interface screen that lets you select a number of options such as
begina newgame,load
asavedgame,visitanonline gaminglobby, browseaninventory
ofmagicspellsgatheredwhileplayingthegame,etc.(Home)
• Thescreentobeginanewgamethatletsyouselectacharactertoplay,aleveltoload,and
thedifficultylevel(StartNewGame)
• An
“attract mode” in which the game plays back a cinematic sequence, originally
developedforarcadegamestoattractplayerswhomightbestandingnearby(Attract)
• The “loading level…please wait…” screen that appears after you have selected a level to
play(Loading)
• Apopupwindowthatdisplayswhen
thegameispaused(GamePaused)
• Themaingameplaywindow, which displaysa3Dgame world,theplayer’s character and
NPCs, perha ps a small inset overhead map of the level, player statistics, inventory, and
selectedweaponry,etc.(MainGamePlay)
Allofthesegamescreensdisplaysomethingdifferenttothe
player.Eachisindependent,andonly
one can be in control at any instant in time (though more than one might be actively doing
something).Each ofthese gamescreensisagamestate,andsoit beginstobe easytoimaginea
finite state machine that represents the overall
flow of the major game screens of a game. Let’s
lookatadiagramofthestatesdescribedabove.
ImplementingGameplayElementsUsingGamebryo
Chapter1:ASimpleFrameworkforaGameApplication3
StartupSplash
MainGamePlay GamePaused
Loading
Attract
StartNewGame
Home
Here,anarrowhasbeendrawnbetweentheStartupSplash statetotheHomestate.Thisindicates
aratherobvioustransition.TheStartupSplashstate isactiveonlyuntilsufficientgameassetsare
loadedtodisplaytheHomestate,themainopeninggameuserinterface.TheStartupSplashstate
isthefaceof
thegameasitboots.Let’saddanothersetoftransitions.Ifnoplayerinteractswith
thegameforsomeperiodoftime,say60seconds,theHomestatewillinitiateatransitiontothe
Attract state. After some time, or if the game detects that a player is doing something
with a
gamepad,the Attractstate willinitiate atransition backto theHome state.Thefollowingfigure
showsthisnewpairoftransitions.
StartupSplash
MainGamePlay GamePaused
Loading
Attract
StartNewGame
Home
Without much difficulty, if you are familiar with games, you should be able to fill in the
remainingbasictransitions.Thefollowingfigureillustratesa completedstatemachinetransition
diagram. (Though actually, there are other transitions that make sense here. For example, the
player might be able to cancel out
of the StartNewGame state, causing that state to transition
backtotheHomestate.)
ImplementingGameplayElementsUsingGamebryo
Chapter1:ASimpleFrameworkforaGameApplication4
StartupSplash
MainGamePlay GamePaused
Loading
Attract
StartNewGame
Home
Thisvery basicintroductiontotheconcept ofstate machinesshould convinceyouthata typical
game‐really,anygamethatgivestheplayer anyoptions‐canbebuiltaroundahighlevel finite
statemachine.Withalittlebitofplanning,itisn’tdifficulttobuildthesetypesof
statesintoeven
thesmallestofgames,fromthebeginning.
Implementing a Simple Game Application using Gamebryo and a Simple Finite State
Machine
Theremainderofthischapterwillassembleasimplegameapplicationframework,implementing
asubsetofthestatesdescribedabove.
StartupSplash
MainGamePlay GamePaused
StartNewGame
The way our initial set of 4 states will work is extremely simple. It resembles a traditional
application that displays a main window, but can change the way the user interacts by display
popupdialogboxes,addingandremovingpanesandcontrols,etc.Onekeydifferencehereisthat,
though
onlyonestatecanbeincontrolofthegameapplication,anynumberofsuspendedstates
has the opportunity to perform actions interactively in time. These suspended states are not
respondingtocontrollerinput,butareplayinganimationsorotherspecialeffects,decidingwhen
toauto‐savealevel in
play,etc.The simplicity of thisinitialdemo will make iteasierfor you to
seehowthisworks,how thingsfittogether,and howyou can exploitcertainGamebryo features