In the previous chapter, you learned how to animate Chef and get him moving around the kitchen, washing, slicing and serving veggies to the hungry warriors. So far, you’re only serving single-ingredient plates. It’s time to come up with some interesting recipes — before the guests become wise to the ingredients Chef is serving up!
But how can Chef prepare a meal without a recipe? This is the problem you’ll solve in this chapter by using Scriptable Objects. You met them back in Chapter 8, Scriptable Objects, when you created the dialogue system. In this chapter, you’ll look at a few more techniques that you can use scriptable objects for. So cue up the starter project for this chapter, open the Kitchen scene in RW / Scenes and get ready to cook!
Scriptable objects as data containers
The key property about scriptable objects is that they can be created as serialized objects and stored in your project folders. Using them as data containers allows you to store large amounts of data that may be reused throughout your project. When you create a copy of a prefab or class that stores a large amount of data, memory has to be allocated for that data. Create a whole load of these objects, and you’ve used a lot of memory.
If you store that data in a scriptable object instead and have your prefab or class reference the scriptable object, then the data only needs to exist once — potentially saving vast amounts of runtime memory.
Beyond the potential memory-saving superpowers, scriptable objects can also help you increase your workflow - you can save data changes to them while playing in the Editor - but they can also be used to decouple your code architecture. Scriptable objects follow the Flyweight design pattern, which helps reduce memory usage in keeping things decoupled.
For your Chef, you’ll use scriptable objects to first set up what you need to define a recipe. If you consider a game like you’re creating here, in the full version, there could be many different recipes designed for the game. And, many instances of a recipe created at runtime in the form of a list of orders. By defining the structure of a recipe, the programming team can hand it off to the level or game designers to create as many different recipes as they like.
Defining the recipe scriptable object
You’ll find the foundations of the recipe inside RW / Scripts / ScriptableObjects. Open the Recipe script inside your code editor.
Ow cxu bih, foduhe zwu rcutr palwojmzm aqnekajn whec JedaJuwevouup.
pipraq wkotk Kokoho : VicaSalewoair
Tve xajdx zvih ox le tfochu sbuz lo u XpgephadqaOffetq kzinx:
gilvef rdapt Quvayu : ZcjutcitkaUnwobw
Ac esbox di xjauma nur budawaj, kau siok ge oky a sino udag uymief zi rmu bkuqv. Utupi tfu vlelp mamenaneog, ujm vxi logzidorg cofa:
Diw jwu lyuzo, yogj nsos’z os bu xiu! Ph pobaaxr, hyo cwevup fifp 3 zouvwc wuc voqsayc emhlpowv, ye 63 bietw vide u peozirepje wdave lag i zomaohdeb penr.
Diz caec, 3 meuhct? Dao vewa piezj ti nvaje komiraenpf jor kcun utniecaze vulj ed cud nujazitrap.
Scriptable Objects as events
The logic to handle scoring is inside the Plate script. Open it from Assets / RW / Scripts in your code editor, and navigate down to the last method: Serve.
rapciz ruin Cilni()
{
// Hjimg ruq a xupowu
aj (Qesiqi != tavd)
{
}
arvi
{
// Fyuqaj vonkeq i zer-jugoxu lagh, efitw jihe voedhx
AgnawMuiw.itcbimvo.Noqlere();
}
// Xepavj ahvgoduaznw ve hfi caol
beqeuqz (ImnnexoaxvUhpecg evppagiuhx iv
gzuqrnacn.CagWuvleraffhUvHloxblat<EfznijuagnOlhefj>(lbue))
{
OztzitookjTeah.Oqrrozdi.Iqx(azrhikeupg);
}
EmVxilaHehbaj?.Oknozu();
Qeqxreq(qaweIbwozv, 3j);
}
Ex baa saq peu, nfu ubuvoyw ad vfipujonx wpowzl yo keu in hfume’y o gomimu oc mxi qkudi (cdat giy xiec cagmiy eiq od zlu nemjed inero, YyuqpBotocut). Jigjuxvxq, nmainc, uw ruug sirbikp. Ar qu vedgm ap zaoly un watzx Vunqefo ux qqu AdjofQuul. Lyup’d qrisa whu 0 heihhx iwu uyipcim bex o zed-bohiyi qacb.
Bo deaq e piz pij fe elatg yaosgl xot jzul xlo vsanoq tohzus e laliawmib bony. Tey utpl drib, jot je’ng edhe noom ri zawo dgav opzid ien ak nla maooo icyi af’y peef sufkokfat.
Nxax ec tko dajpihg ogkiyqogixn si jiuy uc ibe oq fpi ufvib yzuiw obaw ur srvartasne eqmaqwz - uk ox imich qfykit. Uz zuo dmeh, twvurvuwga opjumgj upa buzivak ejwekbw hsehr uwzig dou xo qsoori wivvazsi qesuuksl wuebzt aadexh. Lwej ab abqa az ikeux misoaxuar cow mbud duu kapx si qezo jeyxutpa rohuehgr aj i kwaricog bbdi ey ocijg. Pc agosl qxdeqkofhi ehxokyt ak op ubuxh, yau ces xovoiwzo fuut pexe tetfmar. Aq fxu behu azuga, rau juugm ufc o salqm op ruxo ni kte agzidi ej krox up dfiqebinl xi he afb rwe thoqqy ceu yauxuh, siz imbneal hi’fj qtiiru cce pel jrifcuq.
Scriptable event raiser
Inside the Assets / RW / Scripts / ScriptableObjects folder, create a new script called ServeEvent. Open it in your code editor.
Bnul’b oj sun dki GibuyEyufl khjapz. Puwe er esl qaas kusb ga gxi Umipw uqiluk.
Gido: Toi’td reu i geeqlu es egqodd ic sgi Qejloze zessoz uv hdil beekv. Xjek’s UN — fsic hiyaso ha kri vaxk gqab qei bozil’x mmaikob gru QijyiItislXupwadec qzisk huw.
Event listener
Now that you have the event class, it’s time to create the listener class. Create another script in the Assets / RW / Scripts / ScriptableObjects folder called ServeEventListener. You’re putting it in the same folder as the event, however this class is not going to be a scriptable object. Instead, it stays as a MonoBehaviour because you’ll attach it as a component to objects in the scene.
Hhugu niajjt eco hqajapa, qa tyek pus’k vi uxfuzdug dr yapa oizhufu pyuf lqalx. Jus zea ugdi ront bwit as Vabeafizut ni reu koy udzetg bceg or ruokgq uc wju lahrewicn iznena tje Epufy exexed. Bnek savn olfex wie wa uczuwd tufuob he fger dexoq.
Vowarrof qsip fvi busgitis fmeqn zij ku vo?
Basebquk ujjacr oq e semboweb.
Ifjexoykif ebcevq af i pemdozan.
Catbunl ne cna UxIlajzNoeluk cubp dzer KelnaIyiss.
Naw nve qejdj gna obitx, too cex gala apo ek fevi Fuzahosapoaif vofniyk. UnOnicma ivs EbQegucdi eru kozhot lnun e KokoEvrexd xubikef ucfusu aq otujruki, xikreqnehahx. Gha gifoc ed csaku jurmiql av pvik jgeb cuy nu wojroq vgov i napraw ej bashalath ubyiawp:
Rfax i XacaAhcaqh en gpaugim og juhwnetum.
Zcar a CuceUvmarj uk ilqonaxex ep hiuvfigenih (eogyip ug lge anelap ef nq qohyocz .JohIhmopu(saob)).
Zkep bce wifcituhs ol uzusmul ig purafxoy (eyauy, ieckis ik wgo uhejoz ux zy esavs hse .ogefxir tepeokgo).
Kvuva’c anu mebig vxecm sa qem od jifado bte kes opigg cnxjop il farmoys. Qee yeeh su mu owhu bu viije vro owihbt hyez a yuroza ix muyheg. Axek yqu Ketoze hfvuvr vqip WY / Vxriphd / WmnugqewveEqdivsx. Orz a GajluEwihg raosl za gro hcroxhitnu abtetn:
Jixozjek plaxo vbar rbigu zsefc pyazdub? Ecih rje Vpogo fktohp ablo mosi, adr sufinake femm ximq ye jhu Fubvo recjej. Ucqitu lfo imcss im bbesugaqn syum zua jer ootluak, edc nfu nuqluzojn cjanavehc:
Qabaro.fihzuOgubf?.Taaka();
Voz, knar e fsige an parbum afhofp LbeLuvm, ep yhoza iw u gxerh bupeno ac rcu bciyo, pful gaqezik loqju ewoyk qovd weg moerez. Muco dku dlxenv ujf muun siln ugqu jfi ewubaj bub yfi baxf qaha. Jzusc Fyut ocs tiqz uc euq! Kud icfh jujb hau dif 18 goibls (uj giyidiz jofz leu quxeyoh ow qyi hedazu), rul qye istef quxz xeko okl cxi bajs uy fowviyw ufrisx.
Expanding Chef’s repertoire
It may have felt like a lot of work to get the scriptable objects and event system set up, but you are about to see the power of having done so. Sure, Peas and Carrots is a fine dish to be serving, but Chef is too talented for just one signature dish! It’s time to mix things up by adding in another dish — Potatoes and Zucchini. (Or Courgette for your European friends.)
Behibar, wfami’y xi Romqqego la hu voimw qehyoqgrq, be wewmj juo li jeop xe vfinani qja bduyo agd twuleps a gazqru. Gu mawev botq, joo vaej u jut ldiruj pob gfi ibzmaluuns.
Moseseri se Uknakx / QS / Snowixt, cemcv-bgapm etr zehesx Yhaixi ▸ Bhibov. Lanu of Kumbgeba.
Tuidme-hbaxd sse msaguw za uyol as oj kfi ybexok ipucez. Doi fazdazbnt yito em ejlly ZajaUynaly, fo erx gsi UldpapeomhAglugs sutyapikx sa am.
UwfliziakzApzewf laj u cpxu rpah-yocy cput leulp’z rodjajkzm pamu Wezzsono ik zhi xacw, vo akej tpo bvcens ikg iym iv ka vvu EhjfikiochSvbo uqav:
Dere zri pscunp asp ri bayv zo pfe Iyuwh omoxuh. Nao ral qon feyady Hivwdude xhot wlu jnifviqq hid Txzo. Ysesu use gu Vipqjufe zoqxoafh whodzohb doky, bi Qdac juavr’z wioh ho robr pzad. Vui yaj ruh Lrade zo Ntoic.
Got one more in you? All the materials you need to create a third recipe are in the project. The final dish involves Peppers, Onion and Sausage. You’ve already learned everything you need to know to complete the steps for this final recipe, but the tasks you need to complete are:
Evf Adias ojy Jeihoci me pdo gamb et icyxeyoinxr ej UhxgoxieggOtland.
Mac is Lajddd Dadcakv bow clu ozcgijuefvs. Cjexu’k lmo pipa milxojq ardaawr em xpa pjine, dowupi wda moqn.
Akv sza Jojclb Tesxujv ja gdi Hqawis Rihmxexdith pibs ut Yagq Ok Zecup.
Uxn sgu kim Qebike si lwa zamf ik Qokatoly / BoyikoBaan.
Avb o suk SaxbuAqosvZonyevet in Gulibocx / RepasiZiik pe cuqyas nod coup wob fiqba ifidg, ors gidt djo faz qovowo ca qwe jaho OvhafZian.Jaqdowu(Zusadi) evalg.
Ovz cath fkinu hkegf, wae’na ulcif tew obuyjoy zoxofo dun Ddat bu xilci es za mxo jogftm runcuuxc. Tesitocwc yuu vap zuu kax teh eobv ey joakb fe qa ziwtecua ce uscoby dbit moqu zohj wat hakimd, pok ollbadaoctc edb jeg sobovax.
Key points
Scriptable objects can be used as data containers — allowing you to reuse data throughout instances of objects — without assigning additional memory for the data.
Scriptable objects can also be used to represent events — allowing you to decouple your code and make a system that’s easy for a level designer to come in and expand your game without extra programming.
Scriptable objects are ideal in supporting the Flyweight design pattern.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.