Академический Документы
Профессиональный Документы
Культура Документы
Language
Note:
Remembertheseareexcerptsfromtheroughdraft
editionofthebook.TherewillbemistakesandIwillvery
likelychangetheorderof,andrewrite,entiresectionsof
thebook.Ivelookedthroughthestructure,grammar,and
codebutImnotaneditorsoImsureIvemissedthings.If
youseeanymistakesorhaveideasonsomethingyou
wouldliketoseedonedifferentlyshootmeanemailat
heartbeast.studios@gmail.com .Ireallyappreciatethe
feedbackyouguysprovide.Forthefinaleditionofthe
book,Iwillpayafeweditorstogothroughthebookand
fixanymistakesthatImighthavemissed.
Thanksagainforyoursupport!EnjoythesamplePDF!
SettingupInheritance
YoucanmakeoneobjectinheritfromanotherwithGameMakersparentsystem.
GameMakerusesthetermsparentandchildtodescribetheinheritancerelationship
betweenobjects.Thechildobjectwillinheriteventsfromitsparentobject.Illdescribe
thespecificsofhowthatworksinjustasecondbutletslookathowyoucansetupthis
relationship.
Whenyouopenuponeofyourobjectsfromtheresourcetreeyouwillseea
propertynexttothemaskpropertycalledparent.Clickonthatpropertyandselectthe
objectyouwanttobethisobjectsparent.Youcannotselectthesameobjectasthis
wouldcreatealoop.
Oncetheparent/childrelationshiphasbeensetthechildobjectwillinheritevents
fromtheparentobject.Therearesomeimportantrulesthatyoushouldbemadeaware
ofinregardstohowtheinheritanceactuallyworks.
1. Childobjects willnotinheriteventstheypossessthemselves.
2. Childobjects will
inheriteventsfromgrandparents,greatgrandparents,andso
on(followingthefirstruleofcourse).
3. Itispossibleforchildeventstohavetheirowneventandstillruntheparents
eventusingtheevents_inherited()function.
Letstalkaboutrulethreealittlebecauseitisextraimportant.Therewillbe
caseswhereyouwanttocreateabaseparentobjectandhavechildrenexecutethe
baseeventsoftheparentbutalsocontaintheirownuniqueadditionstotheevent.
GenerallywhenIstartcreatinganewgame,Itryandthinkaboutthemain
objectsinmygameandwhatactionstheywillbeperforming.AfterIfigurethesetwo
thingsoutIstartseparatingthoseactionsintotwocategories, actionsthatwillbethe
same betweenobjectsand actionsthatwillbedifferent .Letmegiveyouaquick
example.
Youarebuildingaplatformgame.Youhavetwomaintypesofobjects.Youhave
yourplayerobjectandyouhaveyourenemyobjects.Youwanttheplayerobjectand
thenenemyobjectstobeabletointeractwiththeworldinanormalplatformertypeway
(gravity,jumping,andsuch).Youwantyourplayerobjecttobeabletoshootarrowsat
theenemiesandyouwanttheenemiestoshootfireballsattheplayer.
Steponeistoseparatetheactionsthatwillbethesameandtheactionsthatwill
bedifferent(therearemanygoodwaystosetthisup,Imonlygoingtoshowyouoneof
them).
Actionsthatwillbethesame
1. Gravity
2. Jumping
3. Collisions
Actionsthatwillbedifferent
1. Input(Usercontrolsplayer,A.I.controlsenemy)
2. Attacking(Enemyshootsfire,Playershootsarrows)
Oncesyouhavetheactionsseparated,itistimetocreateyourobjects.Youwill
createaparent objectthatcontrolsthe
actionsthatarethesame andthentwo
children objectsthatwilladdtotheparentsactionsandcontroltheactionsthatare
different.
Ifyouarenewtoprogrammingyoumightbeaskingyourselfwhyyouwoulddo
this.Theansweristhatitwillmakeyourlifeeasierlater.Letssaythatinsteadofusing
aparentobjectyoujustcodedthe actionsthatwillbethesame twiceintoeachchild
object.Thecodewouldworkexactlythesame,right?Andyouwouldnthavetoworry
aboutparentobjectlogicandsuch.Youwouldberighttomaketheseassumptions.But
whatifinsteadofhaving2childrenobjectsyouhave20?Thenyouwouldhavetowrite
thesamecode20times.Whatifyoustilldecidedtojustpushthroughandwritethe
code20timesbutthenlateryouwantedtochangeit?Youwouldhavetochangeyour
codein20differentplaces.Thiscouldcauseerrorsanddiscrepancies.Ifyouusea
parentsystemthenyouneedonlywritethecodeonceandthen,ifneedbe,changeitin
oneplace.
GettingusedtotheinheritancesysteminGameMakercanbetrickysometimes,
buttheeffortiswellworththebenefitsandonceyougetitdownitwillbecomeoneof
yourfavoritesfeaturesofGameMakerLanguage.
IdentifyingInstances
Wevealreadytalkedaboutthedifferencesbetweenobjectsandinstances.
Therearetimesinyourlogicwhereyouwillwanttoworkwithaspecificinstance.One
waytodothisistocreateareferencetotheinstanceatthetimeofcreation.Acommon
placeforthisiswhenanenemyobjectfiresabulletattheplayer.
Theinstance_createfunctionreturnstheidoftheinstancethatiscreated.
Becauseofthis,weareabletocreateareferencetotheinstancelikethis.
Afterwehavecreatedtheinstanceandwehaveareferencetoit,wecansetits
propertiesandhaveitcallfunctions.
Thisisallmadepossiblebecausewehaveareferencetotheinstance.
Iftheinstancehasalreadybeencreated,itisstillpossibletogetareferencetoit
byusingits
idproperty.
UserInput
InGameMakerStudio,therearemanydifferentwaysyoucangetinputfromthe
user.Inthischapter,Iwillbecoveringthethreemostcommonmethods:MouseInput,
KeyboardInput,andGamePad(Controller)Input.
MouseInput
KeyboardandMouseinputarearguablythemostcommonformofinputon
computers.MouseinputinGameMakerStudioissimpleandeasytolearn.Youcanuse
thebuiltinobjectevents,butImalsogoingtoshowyouhowtocheckthoseeventsin
code.
Letsbeginbyshowinghowtocheck,incode,ifamousebuttonisbeing
pressed.
if
(
mouse_check_button
(
mb_left
)){
// The left mouse button is being pressed
}
mb_leftisaconstantinGameMakerLanguagethatreferstothenumber
associatedwiththeleftmousebutton.Therearethreethatyoucanuse.
mouse_check_button()isafunctionthatreturnstrueifthebuttonisbeing
pressedandfalseifitisnot.AtthispointIshouldclarifythedifferencebetweenifthe
mousebuttonis being
pressedandifthemousebutton is
pressed.Thefirstimpliesthat
theusercouldbeholdingthebuttondownanditwouldstillreturntrue.Thesecond
impliesthatitwillonlyreturntrueinthestepwherethemousebuttonispresseddown.
Afterthat,itwillreturnfalseeveniftheusercontinuestoholdthebuttondown.Lets
lookathowyoucanchecktoseeifthemouseispressed.
if
(
mouse_check_button_pressed
( )){
mb_left
// The left mouse button was pressed.
}
MousePosition
InGameMakerLanguagetherearetwovariablesthatyoucanusetoaccessthe
mouseposition.
mouse_x
mouse_y
Thesetwovariablesareglobalandassuchcanbeaccessedinsideanyobject.
KeyboardInput
KeyboardInputisprettyeasytolearnhowtouseifyoualreadyknowhowtouse
mouseinput.Thetwoareverysimilar.
if
(
keyboard_check
(
vk_right
)){
// The right arrow key is being pressed
}
vk_rightisanotherconstantsimilartothemb_leftconstantthatthemouseinput
uses.Therearequiteafewdifferentvk_prefixedconstantsthatyoucanuse.
Therearemorethanthis.Mostofthemareratherobviousandifyouwantthefull
listyoucansearchanyoneofthemintheGameMakerhelpfile.
Theimportantthingtonoteisthatyoucannotusesomethinglikethis.
if
(
keyboard_check
(
vk_a
))
{
// Causes an error
}
Ifyouwanttocheckiftheplayerhaspressedanyoftheletterkeys,youneedto
useaspecialfunction.
if
(
keyboard_check
(
ord
('
A
')){
// The "a" key is being pressed
}
Maybeyouwanttocheckforanykeyandthendisplaythekeypressedtothe
screen.Youcandoitlikethis.
if
(
keyboard_check
(
vk_any
)){
show_message
(
keyboard_lastchar
);
}
keyboard_lastcharwillreturnastringofthelastkeyboardkeypressed.Thiswill
onlyworkwithlettersandnumbersthough.Itreturnsanemptystringforeverything
else.Ifyouwanttocheckforakeythatisntanumberoraletteryoucanuse
keyboard_key.Thisglobalvariablereturnstheasciivalueofthekeypressed.
if
(
keyboard_check
(
vk_any
)){
show_message
(
string
(
keyboard_key
));
}
Therearetimeswhereyouwillwanttohavetwokeysdothesameexactthing.
Forexample,manygamesallowtheplayertouseboththearrowkeysandASWDletter
keystomovetheircharacter.Youcanmapkeystoeachotherlikethis.
Youonlyneedtocallthesefunctionsonce,say,inyourcreateevent.Youwould
continuetoprogramyourgameandcharacterusingthearrowkeysbuttheASDWkeys
wouldworkaswellbecausetheyaremappedtothearrowkeys.
GamePadInput
Usinggamepadinputisalittleharderthankeyboardinputbutitisverysimilar.
Mostofthetimeyouwillprogramyourgametobecompatiblewithatleasttwoinput
devices.Youmayhaveitsetuptowheretheplayercanusethekeyboardbydefault
butiftheypluginagamepadthenthekeyboardwillbedisabledasinputandthe
gamepadwilltakecontrol.Letmeshowyouasimpleexamplethatallowsyoutodothis.
vardevice =
0;
if(
gamepad_is_connected
(
device
)){
// Get gamepad input
}else{
// Get keyboard input
}
Thatcodecheckstoseeifafirstplayergamepadconnected.Thedeviceisthe
assignedplayernumber.Ifyourcontrollersaysthatyouareplayernumberone,then
thedeviceshouldbe0.Ifitsaysyouareplayernumbertwo,thenitshouldbe1.Three
is2andfouris3.Thisisimportantbecausethedevicenumberisusedinmostofthe
functionsforgettinggamepadinput.Letslookatsomeofthem.Assumedeviceisstill
setto0.
if(
gamepad_button_check
(
device
,gp_face1
))
{
// Do something cool
}
Thiscodecheckstoseeiftheuserispressingabuttononthegamepad.The
buttonbeingcheckedisgp_face1.OnanXboxcontroller,thatistheAbutton.Thereis
anentirelistofbuttonconstantsassociatedwiththesefunctions.Youcancheckthe
GameMakerhelpfileforthatlistaswell.
MethodsofControllingInput
ThemethodIgenerallyuseforcontrollinginputistoseparatetheinputfromthe
actualmovementwithvariables.Thisallowsmetomoveacharacterorenemywiththe
variablescreatedanditdoesntactuallymatterhowthevariablesgetset.Imgoingto
setupasimpleexampleofthis.
Firstletscreatethevariables.
CreateEvent
xaxis
=
0;
yaxis
=
0;
abtn
=
false;
Nowletssettheminourplayerbasedontheinput.
SetEvent
if
(
gamepad_is_connected
(
0
))
{
// Get gamepad input
xaxis
=gamepad_axis_value
(
0
,gp_axislh
);
yaxis
=gamepad_axis_value
(
0
,gp_axislv
);
abtn
=gamepad_button_check
(
0
,gp_face1
);
}
else{
// Get keyboard input
varright
=keyboard_check
(
vk_right
);
varleft
=keyboard_check
(
vk_left
);
varup
=keyboard_check
(
vk_up
);
vardown
=keyboard_check
(
vk_down
);
xaxis
=right
-left;
yaxis
=down
-up;
abutton
=keyboard_check
(
ord
('
A
'));
}
Oncewehavecreatedthevariableandsetthemaccordingtotheinputthe
playerhasgivenusthenwecandecidehowtomovetheplayer.Thiscodeassumes
youareusingobj_solidasyourwalls.
StepEvent
// Set the speed
varspd
=
4;
// Horizontal movement
if
(!
place_meeting
(
x
+
xaxis
*
spd
,
y
,
obj_solid
)){
x
+=
xaxis
*
4;
}
// Vertical movement
if
(!
place_meeting
(
x
,
y
+
yaxis
*
spd
,
obj_solid
)){
y
+=
yaxis
*
4;
}
if
(
abtn
){
// Shoot a bullet
instance_create
(
x
,y
,obj_bullet
);
}
States
Settingupastatesysteminyourobjectsissmartifyouwantthatobjecttohave
differentbehaviors.Letssayyouwantyourplayerobjecttobeabletoswipeasword,
rolloutoftheway,andthencontinuerunning.Eachofthesebehaviorscouldhavetheir
ownstatewithintheobject.TherearedifferentwaysofimplementingthisandImbyno
meansaprobutIcanshowyousomeverygoodwaysthathavebeenhelpfultome.
WhenIwasfirstlearningtosetupstatesystems,Itriedcreatingadifferent
objectforeachstateandthenIwoulduseinstance_changetochangestates.This
workedbutnotverywellbecauseofthewayinstance_changeworks.Instance_change
isgoodforsomethingsbutIwouldntrecommenditforastatesystem.Aftertryingthat,
IrealizedthatIcouldjustuseastatevariablecombinedwithaswitchstatementinthe
stepeven.Letmeshowyou.
CreateEvent
StepEvent
case
'
move
':
scr_move
();
break;
case
'
attack
':
scr_attack
();
break;
case
'
roll
':
scr_roll
();
break;
}
InordertohelpmeorganizemycodebetterIchosetocreateascriptforeach
stateandthenjustrunthoseineachcase.
IvelearnedsomedifferentwaysofdoingthisandIcameupononethatIlike
quiteabit.Ofcourseitisntperfecteitherandcouldbeimproved,butitsprettysimple
andeasytounderstand.Thiswayusesacombinationofmacrosandscript_execute.
Letmeshowyouasimpleexampleofhowitworks.
OneofthecoolfeaturesofGameMakeristhatyoucanassignanexpressiontoa
macro.Youcanassignascripttoamacro.Forthesakeofthisexample,Illusesome
fakecodewiththeassignmentoperatorbutinreality,youwillsimplyusethe
GameMakeruserinterfacetocreatethemacro.
=scr_player_walk
PLAYER_WALK
Onceyouhavethatasyourmacroyoucansetyourstatelikethisinthecreate
even.
=PLAYER_WALK;
state
Youwouldusethissamemethodtochangestatesofyourobjectinsideother
events.Afterdoingthesefirsttwosteps,youstillneedtoactuallyrunthecode
associatedwiththatstate.Todothatyouwouldplacethisinthestepeventofyour
object.
execute_script
( );
state
Itsthatsimple!Inthefirststepyouassignthescriptidtothemacro/constant.In
thesecondstepyouassignthatmacro/constantthatcontainsthescriptidtothestate
variable,andinthelaststepyouexecutewhateverscriptisassignedtothestate.Itsa
simplesystembutitcanworkreallywellforseparatingthecodeoutforeachstate.
Makeanobjectfollowthemouse
Thisisaprettysimpletaskformostpeoplebuttherearesomereadersthatmight
notknowhowtodothisproperlysoImgoingtoputaquickexamplein.Thetrickwith
thisismakingsureyouusethe EndStepEvent
EndStep
x
=mouse_x;
=mouse_y;
y
Thiscodesetsthexpositionoftheobjecttothecurrentxpositionofthemouse
attheendofeverystep.Ifyouusethenormalstepeventthetimingofthemovewillbe
offandtheobjectwillappeartolagbehindthemouse.
Makeagunaimtowardsthemouse.
Thisisalsoasimpletaskbutitisveryuseful.Thetrickhereisknowinghowto
usethebuiltinimage_anglepropertyandthepoint_directionfunction.
vardir
=point_directioin
(
x
,y
,mouse_x
,mouse_y
);
image_angle
=dir;
=dir;
direction
Youshouldalsomakesurethatyourspritehasitsoriginwhereyouwantthe
pivotpointtobeandthatthespritestartsoutfacingexactlytotheright(or0degrees).
Variables
VariablesinGameMakerLanguage(andineveryotherprogramminglanguage)
areusedtostorevaluesinmemory.Oncethevalueisstored,itcanlaterbeaccessed
andmanipulated.Thishelpscreatecodethatiseasiertounderstandandmaintain.As
youlearntousevariablesandbetterunderstandthemyouwillbeabletoappreciate
theirpower.
VariableTyping
Variabletypingshowsupinmanyprogramminglanguages.Generallywhenyou
createanewvariable,thecompilerwillwanttoknowifthatvariableisgoingtobeused
toholdastringvalue,anumbervalue,abooleanvalue,oranyothertypeofvalue
availableinthelanguage.Thiscangetconfusingfornewprogrammers,sovariablesin
GameMakerLanguageweredesignedtobelooselytyped.Thismeansyoudontneed
tonamethetypeofavariablewhenyoucreateit.Italsomeansthatavariablethatwas
onceusedtoholdanumbervaluecouldatanytimeinyourcodebechangedtoholda
stringvalue.
Evenjustthisbasicunderstandingofvariabletypingwillhelpyouasyoulearn
moreaboutGameMakerLanguageandhowtouseit.
VariableDeclaration
DeclaringavariableinGameMakeriseasy.Thereareafewdifferentwaysto
declarethem,dependingonthescopethatyouwouldlikethevariabletobein(seenext
sectionformoreinformationonvariablescopeandhowtodeclarevariableswith
differentscope),butforthemostpartallyouhavetodoisnamethevariablewithan
identifier,usetheassignmentoperator(singleequalssign),andtheninsertavalue.
Hereisanexampleofonewaythatyoucoulddefineavariable.
name
=
"Benjamin"
;
// String variable type
age
=
25
;
// Number variable type
happy
=
true
;
// Boolean variable type
Thenextsectionwillshowyoumoreaboutvariabledeclarationandhowyoucan
declarevariablesindifferentscopes.
VariableScope
Thescopeofavariabledescribeswhereinthecodeyouhaveaccesstothat
variable.InGameMakerLanguage,therearethreemainvariablescopes.Thereisa
globalscope instancescope
,an localscope
,anda .
GlobalVariables
Ifavariableiscreatedintheglobalscopeitcanbeaccessedfromanywherein
thecode.Variableslocatedintheglobalscopearecalledglobalvariables(bigsurprise
there).Thesevariablesaredefinedbyplacingthekeyword global
andadotbeforethe
variableidentifier.
global
.
name
=
"Benjamin Anderson";
Aftertheglobalvariablehasbeencreatedyoucanaccessitinsideofanyscript
orobject.
InstanceVariables
Ifavariableiscreatedintheinstancescope,itcanonlybeaccessedwithinthe
codeofthatinstance.Variableslocatedintheinstancescopearecalledinstance
variables.Bestpracticeistodefinethesevariablesinthecreateeventoftheobjectin
ordertoavoidundefinedvariableerrors.Thereisnosecrettodefiningthesevariables,
yousimplynamethevariablewithanidentifierandthenassignavaluetoit.
name
=
"Benjamin Anderson";
Afterdeclaringit,youcanaccesstheinstancevariablesinsidetheobjectitwas
createdin.
InolderversionsofGameMaker,instancevariableshadtobedefinedinthe
createeventofanobjectorthecompilerwouldthrowanerror.
LocalVariables
Ifavariableiscreatedinthelocalscopeitcanonlybeaccessedwithinthat
actionorscriptinwhichitwasdefined.Variableslocatedinthelocalscopearecalled
localvariables.Definingalocalvariableisjustlikedefininganinstancevariableonly
youplacethevarkeywordbeforetheidentifier.
varname
=
"Benjamin Anderson";
LocalvariablesdontexistinolderversionsofGameMaker.Ifyoutrytocreatea
localvariableinanolderversion,youwillgetanerror.
MacrosorConstants
Aconstantissimilartoavariableinthatitcancontainavaluethatcanbe
accessedincodeitisdifferentfromavariableinthatconstantscanneverchange.
Onceyousetthevalueoftheconstant,itcannotbemodifiedinsidethegame.Inmany
programminglanguages,theidentifierfortheconstantiswritteninallcaps.Thishelps
theprogrammertodistinguishitfromothervariablesandremindshimorherthatit
cannotbealtered.
Todefineaconstant,youwillneedtoclickontheconstantsnodeintheresource
tree.Oncetheconstantswindowcomesup,youcanusetheaddbuttontocreateas
manyorafewasyouwillneed.Aftertheconstantisdefined,youcanuseitinsideyour
codejustlikeanyothervariable.
draw_text
(x
,y
,COMPANY_NAME
);
Itshouldalsobenotedthat
constantsfallundertheglobalscope andcanbe
usedanywhereincode.
Enums
Enumsarearathernew,butveryuseful,partofGameMakerLanguage.The
wordenumisshortforenumerator.Letslookathowyoucancreateone.
enumbasestat {
hp
=
50,
att
=
20,
def
=
18,
spd
=
24
}
Easyenough.Afteryouhavecreatedtheenumyoucanaccessthevaluesinitlikethis.
varbasehp
=basestat
. // Returns 50
hp;
Itsimportanttoknowthat
enums,likeconstants,areglobalscopesotheycanbe
accessedanywhereinyourcode .Theyareconstants,meaningtheycannothold
valuesthatwillchange.Enumsalsohavedefaultvaluesandyoudonthavetoassigna
valuetothem.Youcouldcreateanenumwithadefaultvaluelikethis.
enummonths {
january,
february,
march,
april,
may,
june,
july,
agust,
september,
october,
november,
december
}
Thedefaultvaluesstartand1andcountsupfromthere.Inthisexample
month.january nowhasavalueof1andmonth.december hasavalueof12.
Inalaterchapter,Iwillshowyousomeusefulwaysthatyoucanuseenumsin
combinationwitharraystogiveyourcodebetterstructureandmakeiteasiertoread.
UndefinedVariables
Anundefinedvariableisavariablethathasneverhadavalueassignedtoit.
Thesetypesofvariableswillshowupquiteoftenwhenyouarefirstprogramming
becauseyouwilltrytouseavariablethathasnotbeendefinedandthecompilerwill
throwanerror.Ifyoureadtheerrormessagecarefully,itwilltellyoutheobjectand
oftentheexactlineofcodewheretheerroroccurred.