Sei sulla pagina 1di 13

Custom High Score Tutorial (Game Maker Studio)

Written by Noel Dundas (AKA Noele) www.software4me.net/contact

Contents
Contents..........................................................................................................................1 Introduction....................................................................................................................2 Requirements..............................................................................................................2 !er!ie"........................................................................................................................# Getting Started............................................................................................................$ Resources....................................................................................................................% Customi&ing...............................................................................................................' (aria)le *escri+tions.............................................................................................' Game Start.................................................................................................................., Ho" it -orks.................................................................................................................. )/ect o)/0hiscore....................................................................................................... Scri+t scr0add0hiscore............................................................................................ )/ect o)/0in+ut0te1t.............................................................................................2 Scri+t scr0te1t0in+ut.............................................................................................13 4rror -hen 41iting the High Score.....................................................................13 5sing Strings 6or 41tra Score...................................................................................11 7++endi1 7..................................................................................................................1#

Disclaimer and Copyright Information: This tutorial document and associated files may be freely copied e!chan"ed or distributed pro#idin" no char"e is made for the actual material. $f the medium of reproduction incurs a cost you may optionally pass on such costs pro#ided you clearly state such costs are for these or your time and not for the product. This is a FREE PRODUCT. Yo may !OT sell it. Yo may !OT claim o"nership. Yo may !OT claim a thorship. Yo #$Y gi%e it a"ay. This is supplied A%&$% without warranty of any 'ind. Software4me is not affiliated to or connected with Game Maker or with YoYo Games Ltd and whilst every endeavor is made to ensure information contained in this publication and associated files are accurate and up to date, no liability for inaccuracies or omissions may be accepted.

8age 1 o6 1#

Custom High Score Tutorial (Game Maker Studio) Introduction


(ame )a'er %tudio contains a built&in hi"h score system which allows you to maintain and display the top ten scores locally for the "ame. Althou"h functional it isn*t particularly attracti#e only stores the name a"ainst a score and offers little in the way of customi+in". This tutorial is aimed at allowin" you to create a custom hi"h score system which you can customi+e to suit your "ame.

Requirements
(ame )a'er %tudio ,e"istered -!tension .ac'a"es D//*s ,e0uired 1odin" 'nowled"e (All #ersions) No None None )inimal

Note2 Due to the default s'in of %tudio bein" difficult to replicate this documentation uses the ()3 s'in and code hi"hli"htin" colors. 4ou can chan"e the s'in %tudio uses from the (eneral tab found in .references. 4ou can also download a ()3 code hi"hli"ht color file.

This 1ustom 5i"h %core system allows you to store the player*s name and score to"ether with an e!tra score #alue. This could be used to record a monitory amount or the le#el or ran' attained. (ames created usin" (ame )a'er %tudio are not permitted to bloc' the client de#ice and to that end the 1ustom 5i"h %core system does not halt the "ame either when simply displayin" the hi"h score table or when the player enters their name.

8age 2 o6 1#

Custom High Score Tutorial (Game Maker Studio) Overview


6n platforms with a physical 'eyboard the custom hi"h score system inputs te!t directly into the hi"h score table at that position emulatin" an input te!t bo! as shown below.

.latforms without physical 'eyboards will only display a #irtual 'eyboard when e!pectin" te!t input so for these platforms the 1ustom 5i"h %core %ystem will produce a window into which the player enters their name instead of enterin" the te!t directly into the table.

8age # o6 1#

(oin" to this room with a score of +ero or a score less than that in position ten of the hi"h score table will simply display the hi"h scores. 6nly if the score was "ood enou"h to enter is it hi"hli"hted in red and the player allowed to input their name. The custom hi"h score system will also sa#e the top scores locally and re&load them when the "ame is ne!t run thus preser#in" them across "ame runs.

Custom High Score Tutorial (Game Maker Studio)

Getting Started
As supplied the 1ustom 5i"h %core will run as&is so if you ha#e not done so already import the ()7 into %tudio and run it to see it in action. $t will show a simple menu containin" four option buttons. &igh 'cores %electin" 5i"h %cores will ta'e you to the hi"h score room usin" a simple fade out and in effect. As the score is +ero it will display the current scores but not allow you to enter a name. Play (ame %electin" .lay "oes to a dummy le#el room which for the purpose of this demo allows you to enter score and e!tra score #alues before "oin" to the hi"h score room. $f you are testin" usin" Windows and the score you entered was "ood enou"h to enter the entry in the hi"h score table will be hi"hli"hted in red and you will be able to enter your name. Testin" on other de#ices (such as i6% Android) will display a messa"e bo! into which you can enter your name instead. This is re0uired to si"nal these de#ices to produce a #irtual 'eyboard. Clear 'cores This option is primarily for testin"8 it deletes the current top scores and re#erts them to their default #alues 9 This offers a warnin" that it is permanent and there is no :ndo; ) it (ame This uses the "ame<end() function to terminate the "ame which has the followin" restrictions2 $t will wor' on Windows (includin" %team Wor'shop) Windows .hone Android /inu! and )ac. $t will report an error on the Windows 3 and i6% modules as it is a"ainst the conditions of their respecti#e )ar'etplaces. $t will silently fail on the 5T)/= module. (Some +lat6orms require the S to end the a++lication e.g. )9 closing its "indo".)

8age $ o6 1#

Custom High Score Tutorial (Game Maker Studio)


Resources
%ome of the resources used in this tutorial are only to demonstrate how it wor's and are not re0uired in your own "ame. All resources contained in "roups mar'ed !ot !eeded you will not need to import into your "ame. As you would use or create your own room to display the scores you won*t re0uire any of these rooms either. The ob>ect o*+,loader contains some code which you will need. 4ou can either import it or simply copy its short code from these instructions. The three sprites used in this e!ample can be imported or you can substitute them for your own howe#er pay attention to their si+es and ori"ins. Alterin" these can be done but it would alter the display positions. 6ther than copyin" the code from the loader ob>ect you only need import the ? scripts three ob>ects and @ sprites so it won*t bloat your "ame. Aefore importin" resources into your "ame and you ha#en*t done so already import the 1ustom5i"hscore."m+ into %tudio so you can e!amine its codes and more importantly chec' it will do what you want. The 1ustom 5i"h %core %ystem allows for some customi+ation. 4ou can alter the headin" te!t used in the hi"h score display and of course chan"e the sprites it uses. 5owe#er alterin" the si+es or ori"ins of the sprites or si+e of the font used may re0uire you to manually alter where it draws thin"s. The font used is Arial si+e BB as this offers the most portability. $f you alter this you may need to ad>ust the positionin" to "et thin"s to ali"n correctly.

8age % o6 1#

Custom High Score Tutorial (Game Maker Studio)


Customizing
6ther than replacin" the sprites used with your own most of the customi+ation can be done in a script called scr,hiscore,config scr<hiscore<confi" 1 2 # $ % ' , . 2 13 11 12 1# 1$ 1% :: scr0hiscore0con6ig(); (<o +arameters) :: Called to &ero scores and set custom data !ar e1tra= decimals= reset0score= de60!alue= 6name; score > 3; e1tra > 3; :: e1tra score amount decimals > 2; :: e1tra score decimal +laces de60e1tra > 3; :: Initial de6ault e1tra score !alue 6name > ?Scores.dat?; :: @ilename to sa!e scores as reset0score > 6alse; :: Resets score i6 true use0ccommas > true; :: use commas in score dis+la9 :: *e6ine high score headings A Change to suit 9our requirements glo)al.CH0header0name > ?<ame?; glo)al.CH0header0e1tra > ?@unds?; glo)al.CH0header0score > ?Score?;

4ou can customi+e the score display by chan"in" the default #alues in this script.

Variable Descriptions
(aria)le score e1tra decimals de60e1tra 6name reset0scores use0commas *escri+tion The initial score !alue. The initial !alue o6 the e1tra score data B can )e numeric or a string (enclosed in quotes). 4.g. ?<o!ice?= ?Ceginner? etc. The num)er o6 decimal +laces to sho" 6or the e1tra score data B use6ul to sho" mone9 amounts (4.g. 2 "ould sho" 3 as 3.33) )ut has no e66ect i6 the e1tra score data is a string. The de6ault e1tra score !alue used "hen 6irst creating the high scores. 4.g. 3= ?<o!ice?= ?Ceginner? etc. The 6ilename to sa!e the score data as. I6 true it "ill reset all scores to their de6ault !alues= use6ul "hen de!elo+ing 9our game. *is+la9s a numeric !alue using commas as a thousands se+arator B 4.g dra"0te1t(1=9=scr0add0commas(13$#3)) )ecomes 13=$#3

The headin"s used in the hi"h score table can also be set / chan"ed in this confi"uration script. To help a#oid namin" conflicts all "lobal #ariables used by the hi"h score system are prefi!ed with 15< (standin" for 1ustom 5i"hscore).

8age ' o6 1#

Custom High Score Tutorial (Game Maker Studio)


Game Start
The hi"h score system uses three data structures to contain the top ten names scores and e!tra scores. These are created by the initiali+ation script which must only be called once at the start of your "ame. 4ou can use the (ame 'tart e#ent of an ob>ect in your first room or the Create E%ent of an ob>ect in the first room pro#idin" that room is only #isited once.

1 2 # $ % ' ,

:D Initiali&e the high score s9stem. This is called once at game start D: scr0hiscore0initiali&e(); :: Mo!ing s"i6tl9 on... room0goto0ne1t();

6nce you ha#e added the re0uired resources to your "ame added the (ame %tart code abo#e and edited the scr<hiscore<confi" script to suit your "ame you only need to add an instance of o*+,hiscore to your hi"h score room in the position you want it displayed and you are almost "ood to "o. There is one other thin" to edit. After closin" the hi"h score display we need the "ame to "o bac' to another room. :sually this would be a room displayin" the "ame menu. $f your menu room is called menu<room then no further editin" is re0uired. $f not you will need to edit the code towards the end of the %tep -#ent in ob><hiscrore to point to the room you want to "o to.

22 i6(6inish)E 2# room0goto(menu0room); :: Change to suit 9our menu room name 2$ F (oin" to your hi"h score room with a score of +ero (or any score less than the one in the last place) will simply display the 5i"h %core Table. Cisitin" this room with a score "reater than the score in the last position adds the new score at that position and allows the player to add their name. The #ariable glo*al.C&,e-tra score can be used anywhere in your "ame to record your e!tra score amount.

8age , o6 1#

Custom High Score Tutorial (Game Maker Studio) How it Works


The first time the "ame is run it will detect there is no scores data file so it creates some default #alues and sa#es them. These are sa#ed directly from our data structures so althou"h they are in strin" form openin" the data file in an e!ternal editor (li'e Notepad) mi"ht show somethin" li'e2
2*31333337333333313333333.333333%3'C'1,2'%,223#1313333333.333333%3'C'1,2'% ,223#2313333333.333333%3'C'1,2'%,223##313333333.333333%3'C'1,2'%,223#$31333 3333.333333%3'C'1,2'%,223#%313333333.333333%3'C'1,2'%,223#'313333333733333 3$',2'%'$23%#'*'2,$'.313333333.333333%3'C'1,2'%,223#,313333333.333333%3'C' 1,2'%,223#.313333333.333333%3'C'1,2'%,223#2 2*3133333733333333333333333333333333333333333333333333333333333333333333333 3333333333333333333333333333333333333333333333333333333333333333333333333333 33333333333333333333333333333#$$33333333333333333333333333333333333333333333 33333333333333333333333333333 2*31333337333333333333333333333333$3.@$333333333333333333323.C$33333333333 3333333333.2$333333333333333333343.%$3333333333333333333C3.2$33333333333333 33333$3,@$3333333333333333333$3,@$333333333333333333333,2$33333333333333333 33C3,2$333333333333333333333'2$3

There is of course nothin" to pre#ent a player from editin" this file e!ternally but doin" so would most probably corrupt the data for all scores. Deletin" this file would ha#e the effect of it bein" re&created by the "ame the ne!t time it was run resettin" all scores bac' to their default #alues.

Object obj_hiscore
This is where all the ma"ic happens. When #isitin" the hi"h score room the current score is compared with those in the data structure. $f the score is not "ood enou"h to be entered this controller simply shows the #alues contained in the data structures and creates a 1ontinue button which is used to close the form and return to the menu.

Script scr_add_hiscore
$f the score was "reater than +ero the script named scr<add<hiscore is called to see if the score was hi"h enou"h to be entered or not. $t does this by loopin" throu"h each inde! of the data structure containin" the hi"h score #alues storin" the loop counter as the inde! and brea'in" out of the loop as soon as the score e!ceeds one in the data structure. An update inde! "reater than &B indicates we ha#e a new entry but before storin" the new entry in our data structures we must mo#e all entries from that position onwards down one slot.

8age . o6 1#

A"ain we use a loop but this time we wor' in re#erse order. Data structures start at +ero so our last position (BDth place) is at inde! E. We start at E and count down by one until our update inde! is reached replacin" whate#er is in each position with the #alue of the pre#ious inde!. Finally we add our new entries to the inde! position before returnin" the inde!. An inde! "reater than &B indicates we added a new score to the hi"h scores and we can now "et the player to enter their name. Dependin" upon the tar"et platform / de#ice one of two thin"s happens here. IO'. $ndroid and /inPhone .latform 6% which use #irtual 'eyboards cannot use our own te!t input method so use an asynchronous dialo" which in#o'es the de#ice to show a #irtual 'eyboard for input. To pre#ent the input spoilin" our layout it "oes throu"h a few chec's to remo#e all but letters di"its and spaces and is truncated to allow a ma!imum of GD characters. As this uses an asynchronous e#ent it will not halt the client de#ice so it is safe to use for all platforms. /indo"s. #acO'. 0in 6% platforms with physical 'eyboards can use the te!t input system which enters te!t directly into our hi"h score table in real time. After addin" a new entry an instance of obj_input_text is created at the position attained in the hi"h score table usin" the inde! which the scr_add_hiscore script returned.

Custom High Score Tutorial (Game Maker Studio)

Object obj_input_text
The >ob of this instance is to mimic as closely as possible a te!t entry bo! allowin" the player to enter their name yet pre#entin" certain characters or too many of them spoilin" our display. 1lic'in" on the te!t bo! or enterin" a character remo#es the default -nter Name. .ressin" -nter when the default te!t reads -nter Name chan"es it to No Name and concludes editin". 1lic'in" on the te!t bo! or enterin" a character si"nals the start of the input. $n %tudio "ames nothin" is allowed to bloc' the client de#ice the "ame runs on so this te!t input system does not halt the "ame whilst waitin" for input. Any animations or mo#in" ob>ects in this room would continue as normal durin" input.

8age 2 o6 1#

Custom High Score Tutorial (Game Maker Studio)


Script scr_text_input
The te!t input is handled by a script called scr_text_input. $t mi"ht appear a little comple! to the untrained eye but fortunately you don*t need to worry too much about it. We shall howe#er outline what it does. The script first chec's if a 'ey has been pressed since the last step e!itin" to sa#e time if none was pressed. $t then chec's to see if the editin" was ended by either usin" -scape or the -nter 'ey. $f it was and the default te!t reads H-nter NameH it is replaced by the default strin" of HNo NameH. Aecause we want to try to emulate a standard te!t bo! as much as possible we also need to allow for the delete and bac'space 'eys. .ressin" bac'space has the effect of deletin" the last character in the strin". $t is not possible to mo#e the cursor inside the te!t bo! usin" the arrow 'eys but pressin" Delete or enterin" a character when the current te!t strin" reads H-nter NameH clears the strin" completely without ha#in" to delete each one. A few more chec's are performed prior to addin" characters to the strin" to e!clude non printable characters but allow numbers lower and upper letters plus spaces. To pre#ent the strin" o#erflowin" the te!t area the len"th of the strin" is also capped at GD characters but the player can still edit the name input. -ditin" ends when the player presses the -nter 'ey or selects the 6K button. The edit bo! #anishes lea#in" the entry hi"hli"hted in red the button te!t chan"es to read 1ontinue and the input is complete.

Error When Exiting the High Score


$f you e!perience an error when e!itin" the hi"h score display ensure you ha#e edited the %tep -#ent of ob><hiscore so the room<"oto points to your destination room. (This is easy to o#erloo' and can be caused by renamin" a room.)

22 i6(6inish)E 2# room0goto(menu0room); :: Change to suit 9our menu room name 2$ F

8age 13 o6 1#

Custom High Score Tutorial (Game Maker Studio)


Using Strings for Extra Score
The 1ustom 5i"h %core %ystem uses data structures to maintain the top ten scores which unli'e arrays can contain mi!ed data types. $t does not mater to () if a ds<list contains strin" or numeric data. 6ften the e!tra score data will be numeric to show a le#el number to show an e!perience or achie#ement #alue attained or e#en coins earned. 5owe#er you may prefer to display a te!t ran' instead and the 1ustom 5i"h %core system can be confi"ured to enable this. As supplied it is "eared up to display a monitory e!tra score #alue representin" funds the player collected. The ds<list concerned is therefore storin" a real number rather than a strin" #alue and is confi"ured to show this e!tra score data to two decimal places. (i.e. BD will show as BD.DD E.= as E.=D and B.G@4= as B.G@) $n order to store a strin" #alue instead we need to modify a few thin"s. As supplied the hi"h score system is set up to display e!tra score data as a two decimal place monetary #alue but the followin" e!ample shows how you can use the e!tra score data to store the ran' a player attains. scr,hiscore,config 1 2 # $ % ' , . 2 13 11 12 1# 1$ 1% :: scr0hiscore0con6ig(); (<o +arameters) :: Called to &ero scores and set custom data !ar e1tra= decimals= reset0score= de60!alue= 6name; score > 3; e1tra > ?8ri!ate?; :: e1tra score amount decimals > 3; :: e1tra score decimal +laces de60e1tra > ?8ri!ate?; :: Initial de6ault e1tra score !alue reset0score > true; :: Resets score i6 true 6name > ?Scores.dat?; :: @ilename to sa!e scores as glo)al.CH0score0commas > true; :: use commas in score dis+la9 :: *e6ine high score headings A Change to suit 9our requirements glo)al.CH0header0name > ?<ame?; glo)al.CH0header0e1tra > ?Rank?; glo)al.CH0header0score > ?Score?;

5ere we chan"ed e-tra and def,e-tra to the strin" H.ri#ateH set decimals to D and set reset<score to true8 this is important.

8age 11 o6 1#

1han"in" the e!tra score data from a numeric #alue to te!t strin" would ma'e any e!istin" data no lon"er compatible causin" errors if loaded. %ettin" reset<score to true forces the "ame to delete any e!istin" scores data file and create a new one usin" the new format. After settin" it to true and runnin" you should e!it and edit the script a"ain to set it bac' to false8 lea#in" reset<scores set to true deletes the scores each time the "ame runs. The #ariable glo*al.C&,e-tra,score will now contain a te!t strin" which you can use in your "ame to set / show a player*s ran'. E-ample Use i6(+romote)E glo)al.CH0e1tra0score > ?Cor+oral?; F :sin" the abo#e e!amples the hi"h scores would show a te!t ran' for the player which mi"ht loo' similar to this.

Custom High Score Tutorial (Game Maker Studio)

,emember if you chan"e the e-tra score from numeric to strin" you must also chan"e the def,e-tra score to a default strin" too. The def,e-tra strin" is used the first time the hi"h score table is built. The script scr hiscore confi! performs some rudimentary chec's to help a#oid type mismatch errors but it can still "i#e unwanted results. %ee Cariable Descriptions for details. 8age 12 o6 1#

Custom High Score Tutorial (Game Maker Studio) $ppendi- $


'cript Use scr add commas Adds commas as thousands separator to a numerical #alue which is then returned in strin" format. -.(. draw<te!t(! y scr<add<commas(score))8 scr add hiscore 1hec's if the current score can be added to the hi"h score list and returns the inde! of the position it added the score to or &B if it was not "ood enou"h to add. scr hiscore confi! 1ontains the confi"uration #ariables used by the hi"h score system. )ost customi+ations can be done by chan"in" these #alues. scr hiscore initiali"e 1reates the data structures to contain the custom hi"h score data populatin" them either from file (if e!istin" hi"h scores e!ist) or with default #alues. scr hiscore load /oad the data structures from the data file to re&populate them. scr hiscore save %a#e the current hi"h scores from the data structures to a file in an encoded te!t format. scr te#t input %imulates enterin" real time te!t into an input bo! by "rabbin" one character at a time as 'eys are pressed 9 can only be used with de#ices ha#in" a physical 'eyboard. O*+ect Use ob$ hi!hscore button This displays a close button on the hi"h score table. ob$ hiscore This controls the hi"h score formattin" and display and is the only ob>ect of the three you need place in the room. $t creates the close button and optionally an instance of ob><input<te!t if re0uired. ob$ input te#t This handles the te!t input when enterin" a new hi"h score name. $t is not used by all de#ices specifically i6% Android and Windows .hone will not use it nor will an un'nown 6%.

8age 1# o6 1#

Potrebbero piacerti anche