Tutorial: Building Games on Solana

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody um very happy that so many of you came here to learn about onchain games on salana um so what we got to do today is uh first a little intro then um I'm going to tell you why you should build your games on Solana then we do a little live demo of a few games that I wrote and then we going to write our own onchain game uh actually two of them and then when we still have time we're going to Port a game to Unity or depending on what you want I will show another onchain game like using an energy system or doing some staking or so on and then we have a little where to go from here section all right so uh I'm yonas Han and I worked as a game def for many years I worked on Forge of Empires lost survivor did some indie games world on world of kyodi and also contributed to the Solana Unity SDK and now I work for Solana Foundation as a developer relations person so why is Solana um games on salana just makes sense like salana is basically built for games um you have confirmation times of the blocks are 400 to 500 milliseconds the transactions fees are extremely low it's like 0.00005 Soul per transaction and um yeah you can reward your players with um tokens for example or with um NTS and there's always good to have some rewards for players that they feel like their time is valuable that they spent in the games I see that I'm here a bit in the in the corner so let me try to to move myself a little bit um yeah the if you build your game on chain will also make it composable so for example if you build a ranking program you could just use the ranking program of another game for example or your nfts could be used in other games or you could build your own client for another program which someone else wrote for example so that uh totally makes sense I'm going to put a link to a high score program that someone wrote in the in the comments below or in the slides later that you can download um yeah then another advantages of building on Solana is it's free for all it's like running your own back end on the cloud decentralized um will always be there like you don't need to care about like that the servers go down like I for example played for a long time Ragnarok online and at some point just the European servers went down and like my characters were gone so that feels really bad but if it's on chain on Sana then this basically can't happen and it also takes away from you the the need to take care of the accounts like you don't need like a back end and a datab base and security but you can just let people log in with their wallets that they have anyway for example a phantom wallet or Soul flare or backpack or whatever and you can just use this as a digital identity and they can use their nfts as their profile pictures and so on they can show off what they have so that's very good it also replaces your payment provider like if you have a game and you want people to do microtransactions in it so they should like buy some in-game coins so that they can buy potions and so on now you can do this with um SPL tokens or S so that takes that away and also when you release it on mobile then you save the 30% fee that Apple and Google usually take this is um yeah this is I would say very useful but it's a bit hard still to get into the iOS store like I tried with a few games and uh as soon as you have an nft in the game and you give it utility then you need to purchases via Ina purchases on the Apple store that's of course because they ear their 30% fee so it's kind of understandable but to get around this you would probably need to be implementing your own like minting via in a purchases on iOS so it would be a nice project if someone wants to tackle that maybe um yeah and you have a very engag target audience like the people that are already on Solana and like games they like they stick to your games they are excited about it and yeah would be now is currently a good time to be like the first ones who's like on the new zaga phone the Solana phone that they are releasing and also there is the new wallet called backpack and the backpack has X nfts I'm going to show you one of them later and these X nfts are basically nfts that you can execute so you can put your um X nft in the backpack wallet and then people can click on it and directly play it in the wallet so that's uh really cool system then there are also some reasons why you should not build on chain like uh Krypto is not for every game like you should try to find something why crypto make sense for your game and not just try to flunch it on top better come up with the onchain first idea if you want to build an onchain game and then build on top of that and the regulations are not really clear yet like if you release a token and then you like sell it or you let people gamble with it um yeah you need to take care of the regulations in your country like for example if you have gambling laws or moneya laundering laws or it could be seen as the security for example so all of that you need to keep in mind and it's kind of hard to make a real Time games uh on chain because um yeah the block times are 500 milliseconds so basically you can only have a state transition every 500 milliseconds confirmed so like if you have a shooter that's probably not fast enough but um if you have something like warms for example where you like uh put the angle and then you shoot a rocket that flies slowly that is something you could probably still build on chain so I'm going to show you some example games that are POS like just to show you what's possible to build on chain for example I have this game here it's called soul Hunter no that's a tiny Adventure uh this one is soul Hunter tiny Adventure we going to build later this is also not soul Hunter sorry let me quickly open this up so this is the game I wanted to show you first it's a completely onchain multiplayer action adventure game basically so there's one State uh on the blockch chain which is this grid here so it's an account which has like 4x4 tiles and every tile has a public key of the player and a avatar public key which is the nft and it has um some soul saved so if I now for example spawn another character with another vet I just minted this little 3D nft here I can select it I can spawn this player and then like if I go to the other um instance of the game I can see that there's this car now and if I walk up there then I kill the other player and I steal his soul so you can see it has an auto approved wallet so you basically deposit some s in the in-game wallet and then later you can just withdraw it again so like this you can have a very fluid gameplay you can just um like outo approv the transactions in the client and you don't need to click the uh approve on Phantom all the time and there's also this chest spawned here like whenever you spawn in your character and you can also collect it for some soul so if anyone of you wants to try it out you can just go to this URL and collect the chest now get some free s um yeah this is the live also works on mobile so uh from Unity if you build your game in unity you can easily just export it to Android and iOS and um yeah it will work the same way then another game I wanted to show you that I found recently is really cool it's like Solana plays Pokemon so basically what you do is you play this uh Pokemon game boy game and on the bottom you vote vote for certain key presses and then like I don't know if you have seen this like a few years ago that was on Twitch like Twitch Plays Pokémon and there was a chat on the side that controlled this game and here now you have like um just people playing this game together and voting for the moves on chain on Solana so I found this uh really exciting and there's lots of possibilities here like you can could like first of all build any Game Boy game but you could also they could even to make Mario like if you like inre decrease the frame rate by a lot then you could like have people vote on jump or something like this um would be interesting to have a playthrough of Mario on chain actually then uh the next thing I wanted to show you is like uh it's a game that was um recently released to one of the hackathons it's Lumia online it's still very basic but um if you want to have an example uh of how to build an adventure game like you can kill monsters and you have missions and you can spawn something um I really like learning by example so that's why I'm showing you these like I would like just looking at the source code and if I have an idea what I want to do like hey I want to do something with tokens in the game on chain then I look up a game that did it and then I read through the code and then I have a good example of how it works then like one of the first games that ever released on chain on salana was letter Caster they joined like I think almost all the hacker houses and yeah but you can do is you can walk around you can collect things you get tokens you can get items and they have this interesting crank system so whenever you want to proceed to the next round someone needs to press the button to create the account for the next round so that's also an interesting game and like uh one of the most uh um amazing onchain games I've seen so far is K Ken cash Clash Ken Clash by dominari and he's using an entity component system that jump crypto build from it's called Arc and it's basically a system where you can have entities on chain like for example you have a character it could be an nft and it has certain traits that you can just put on it there's a little video here and yeah it's a can be played on a big map there are buildings that spawn units you can move units these units fight each other and it's also released on the in the X nft as a in the backpack As an X nft so I would recommend you try this one out that's um really amazing but now let's do some life coding so we going to build this game here now it's called tiny adventure and it's not much but it's honest work so you can press here get data and you can move left and you can move right so and in the end if you reach the end the character will be super happy so I move right here and now I reached the end and the character is happy and how we're going to build this is there is this thing called Solana playground which is basically a web UI where you can build and deploy and test your salana programs and what we're going to do is we go to beta. solp g. i/t tutorials since I uploaded this tutorial already the tiny Adventure there's also some instructions here um if you want to read it yourself you can do that but we're going to go now step by step how you would build and deploy such a program so the first thing you need to do is you click here on the bottom left to create a wallet it will probably ask you to add some seat phrase and like write down your seat phrase and then you have a wallet in the playground that will be used to deploy the programs this will be the update Authority for your programs and yes let's build this thing so for that you just need to type build then it's building the project and it will update this you um this ID up here so we can look at this by going to the Solana Explorer and we go on definite and then we type in this um yeah we paste in this address of this program and then we can see here already okay it is deployed um it is uh still upgradeable it is executable which means it's a program and yeah now this program is already deployed and now you can also do the same thing by clicking here on build by the way and here on the bottom left you have different end points that you can pick so I'm now on defet but if you have some Pro problems like for example getting an airdrop or getting errors when you deploy the program you can pick um different ones here so probably what you need to do before you deploy the program is to get some airdrop so you can just try uh type salana airdrop 2 and this will yeah it does an RPC call to the RPC and will just air drop you some free definite Sol okay so that worked now we can deploy the program and uh for me it says now I don't have the upgrade Authority for this one so I guess I uh I did something wrong earlier so I just going to create a new one and um then deploy this uh you shouldn't do this maybe like like this you lose your upgrade Authority if you haven't saved it before but um in my case this is okay so now um yeah deploying this then this will be on chain so we can look at the signatures what this does now it basically it builds the program this creates an S so file and this s so file will then be put into the data of this um account um and it does a lot of transactions because the transaction size is limited on salana you can only send 1,232 bytes so it needs a lot of um transactions to deploy this program actually okay now it's deployed now the first thing we're going to do is we just run this client so we just run it the declared program ID doesn't fit okay so we need to let me quickly yeah it's because I created this new um key pair now I also have a new program ID so let's quickly do this again deploy it again so I just copied this program ID here and put it into the declare ID in the in the lip RS okay so now let's run this finally so we just type run and we can already see here it says a Journey Begins you're at the start and then it walks one place to the right but how does that work so first of all congratulations you deployed your first Solana game uh on chain on defet so that's really nice okay so the first thing we do here is we create a program account for our level so what this means is this is called a PDA and what this means it's a it's an address on Solana which is derived from the program ID and some seat so in this case we just put a string here it's level one so this will basically find us an account which is owned by the program which the program will be able to sign for so the program can change this account as it wishes and um it has a seat of level one so you can think of this as a database entry basically where the table is the program ID and the seats are all the different entries and in this uh address here we're going to save the position of the player so and then what we do is we see if the account is already there and if it's not already there then we initialize the account and then we call move right on this account so now let's look at how this looks like in our program so this is a Solana program it is written in Rust and it uses the anchor framework the anchor framework is just something that is put on top of normal Solana programs to make uh working with them easier so it enables you to have a nice serialization of accounts it enables you to do some sign checks and basically it just makes makes everything a bit easier to work with on Solano so all everything in this game that we care about basically is this player position here and we don't actually do much in the game like all we do is we create this um data game data account with the player position and then we have a move right instruction which increases it by one and we have a move left instruction which decreases it by one so here we we can see how we initialize the account the struct is called initialize and here we can see the same seats that we put in uh into this account in the client so we have again the string level one then we have um in it if needed which just means it will create the new account and the payer of this will be the signer so if we send this transaction transaction to the program then we will pay for the space that this needs on chain in this case the space is 8 + 1 8 is just the default account discriminator that Anor creates so it puts a little discriminator on the beginning of the data so that it knows which account it is and then we have just one which is our u8 so this is like we can go from zero to 244 yeah 454 sorry um yeah then we have the signer and we need to put in the system program because the system program is always needed to create a new account and on Solana you always need to pass in all the accounts that the transaction needs so this is for Solana to be able to paralyze um the execution of many transactions so basically all transactions that don't try to write on the same account can be done in parallel because they don't influence each other and this makes Solana so much faster than many other blockchains okay this is um the initialized struct and this here is the initialized instruction and all it does it sets the player position to zero and it prints a Journey Begins and um this little um animation here of the character being at the start of his journey then the next thing we're going to do is we going to move left or maybe let's move right first because we start on the left so what we do here is we put in the move right con context as you can see this is again a list of accounts in this case we only need the game data account because it's the only one we need to we want to change so we put in this account and then we can load the account from the context accounts game data account and then we can check if the player position is three then we know hey the player already reached the end and we say hey that was super and uh if he's not at the end then we increase the player position by one so we just take the player position and say hey it's equal to player position plus one and then we print the player position this just a little function here which just says like if the player position is zero we say A Journey Begins and if it's like goes to three then you have the end you reach the end and this is super so let's go back to the client now so now we learned already how to create a PDA we learned how to initialize an account so we just call playground program methods initialize then we put in all the accounts that we need in this case it's only the new game data account the signer in this case we need because we want to pay for the account and the system program for creating the account exactly and then we put in the key pair so that we can sign this and then we just confirm the transaction and also in the client we print uh print the little game animation and here here now you can like play around a little bit so let's um let's run this again for example so we run and now we can see we are at position three and if I run this again then hey we are at position three so we can also move left so I can just comment this in and comment the move left function uh out and then yeah we are at position two again and the nice thing is you can always like um take this um I think you can copy this whole thing here actually paste it in here and then you it will show you the transaction I usually find it nicer to look at the transaction in the Explorer so what I usually do is I just copy paste this ID and put it in here uh I really got used to the Solana Explorer like to me it shows everything in a nice way so here you can see this is our account it has like a tiny balance um it's quite interesting actually if you have this CLI installed you can just write Solana rent and then we can say how much does it cost to pay for nine bite which is the account size so in this case we can see it's 0.00 95353 and that's exactly the balance that this account has for it like to be rent exempt so it it or forever stays on the Chain um yeah and this is just our program and here we can also see hey we moved left and we are at position two here so that is already it for the first game so what you could do now is for example you could like write a longer story about this send it to your friends let them play it um but now we're going to go to the next example because we want to have a bit more so what we're going to do in this example is we're going to build our first play to earn game so what this means is what we're going to do for this program is we going to transfer some soul into um PDA so into a chest Vault and then when the player reaches the end and opens the chest then he will get the soul that's in this chest you can already see here it's uh represented with this little diamond and let's in this case go again first to the program so what you can see here in line 14 is the chest reward it's just the lards per Sol which is like I'm not sure 100 million or 10 million or something and then divide it by 10 and this is the chest reward so it will be 0.1 soul and here we already have like a little error that I actually I wanted to show you later how this is done so let's quickly remove this so um yeah so what we're going to do is the same thing as before we initialize the level then we have a new instruction now it's called reset level and spawn chest what this one does it sets the player to the beginning of the level and then it transfers some s into a chest Vault and um this is done by doing a so-called cross program invocation uh all it does basically is from our program we call the system program to do a transfer so for that we create a cross program invocation context then we put in the program that it should be calling in this case it's the system program then we put here the instruction that we want to call in this case it's transfer and we want to call transfer from the payer which is the account that calls this function and we're going to transfer it to the chest Vault and going to show you later how we created the chest Vault but um yeah now we transfer the chest reward so to this chest Vault and then we just type hey we reset the level and we spawn the chest at position three so we we need to walk three times to collect this chest and now we have like a little bit difference also in the move right account so what we do here is we get the game data account again we check if it's already at the end then we say hey you just reached the end uh but if he is at position two now and he wants to move to the right then we know in the next step he will collect the chest so what we do here is hey you made it you will get this reward and here you can see that it looks uh a little bit different than how we transferred this so into the chest VA because our chest Vault account is a PDA so it is owned by our program and you can only do um cross program invocations to the system program with system accounts a system account is basically every account on Solana when you create it so like all your wallets uh they are all system accounts and yeah to get around this uh limitation we what we do is just we subtract the lamparts from the chest Vault account and then we uh add the lamparts to the player account so yeah that's what you can always do like when you're the signer of a program you can change the Lampard of this account and uh in this case the player will be the signer of this transaction so um the um program is the PDA and the program owns this PDA so the program can change the lampers of this PDA and you can always just increase the lmods of another account only thing that's very important of course is that in the end of your transactions all the balances of all accounts need to be equal otherwise the Solana runtime will just say hey you can't create a soul out of thin air okay this is already how we send the S out of back out of our chest Vault account and then we here have the print player now with this little diamond and the accounts here are almost the same so the level one is the same as in the first example but the second account now is the chest Vault and it's created like the other one so it's like just um here in it if needed you can also just say in it cuz otherwise you could call this function multiple times and you don't want this oh so just like give this back and the space will be 8 kilobyte 8 byte in this case because we don't have any data in the chest Vault account as you can see it's just empty so all it needs is the 8 byte for the account discriminator and then we have here the spawn chest this is also like just putting the account chest Vault and what's kind of important is that you put also the seats in here what um Solana does in the Anor does in the background is it checks that the seats are correct and if the account is really owned by the program like if you don't do this you could for example like just put in another account so this is like a security concern you could um spawn a chest but as a as a payer or as a chest Vault you put in your own account so that would mean you would reset the level but you would transfer the S that's supposed to go in the chest to your own account so that's something you always need to keep in mind that you put the right checks on these accounts and then you are here for the move right um it's the same we also need to put in the chest Vault because all accounts that are interacted with uh in salana need to be in the instruction um yeah that's already the program it's um it's not much like the most important things are the CPI and how you transfer s from one account to another now we're going to look at the JavaScript client let's quickly build and deploy this I hope I have the correct update Authority now otherwise I will quickly need to create a new one um maybe I can just run this I think I deployed earlier already yeah so this looking good um here we can see that uh this is our chest balance be our soul balance before we spawn the trans uh before we sponsor chest this is the soul balance after we sponsor chest so it has 0.1 Soul Less in the account now then we walk to position one then we walk to position two then we walk to position three and here we can see hey we collected the chest and we got the all back from the chest so how this looks like um here we create the PDA again here we create the PDA for the chest Vault it's just it's the same thing as the player data account just the string is now chest Vault you can put whatever you want here um and if you want like uh a game that can be played by only one person then you could for example put in here also the public key of the player and then you would have one instance per player oops um yeah here we uh initialize level one we just need to put in all the accounts that will be created and the s again pays for these accounts then what we do is we reset the level and spawn the chest so we pay 0.1 Soul transfer it into the chest Vault and then from there on we can collect it later if we walk now three times to the right so this like just a for Loop 1 two three and then we move right we move right we move right and when we are at the last position then we can collect this account uh the all in the in the account so now let's um change this thing a little bit like let's say for example we don't want everyone to be able to collect the chest but we want only people to be able to collect the chest who know the password so now you can like maybe pause a little bit and try to implement this yourself maybe you saw it earlier already before I deleted it but uh I got to come back in a few minutes and then you can yeah try it out yourself so I guess uh bunch of you already figured it out so all we need to do is we put a password Here uh it's a string and then we just check here if password unequal for example GB it's German for GF then we can just say um um we just say eror error oh no I think we need to like we can just panic and say oh no know the password was wrong so what this does is like as soon as the password is not correct it will just the program will panic and we will um yeah the game will stop I think this uh actually needs to be here so I going to build and deploy this and then we're going to run and look at the error so now in the client we also need to provide a password of course so first let's put a one so we just could put get hair then we run this and we can see like we can reset the chest we can walk up but then the last one will fail and um yeah because we didn't put the right password so if we now put the right password then we can just type run again and then we will be able to collect the chest because the check in the back end does check if we collect it uh if we can collect it so this uh looking good we collected the chest so a little tip here if you want to see the error um on chain then what you need to do here is you need to put in um uh some parameters for the RPC call so usually what the RPC calls do is like there's a simulation step before you send the transaction and if this one fails then the playground doesn't send it out but you can U skip this check by just providing here skip pre-flight true and now if I run this I will get the transaction even though it would fail the the transaction hash so I will copy this one put it here in the Explorer and then we can see that this one went through ah because the password is correct so if I put the wrong password that's what I wanted to show you like if the password is wrong and I skipped the pre-flight checks then the trans trans action would go through and it would fail on chain so this is sometimes helpful if you want to investigate some errors the only thing that's not good about it is that they always have to pay the fee for the transaction and then it would fail anyway but they are cheap so it's not that a big deal so here we can see hey it panicked at Auto the password was wrong so maybe we fix the typos so and then what you can do What's um actually like a little bit nicer to do is that you create um real error code so we try to create this here [Music] so we do uh going to cheat a little bit because I'm not so sure always how this how the syntaxes of this um that's a nice thing I like about like working locally in Visual Studio instead of playground actually is that you have um autoc completion um it will come soon to playground as well probably and you can also lose use like some AI like a co-pilot for example which helps you a bit so I just going to copy this over here so what we create is like we create a custom error we say hey the password was wrong and we say wrong password and then here on the bottom we just return an error instead of panicking so like this and now we have a proper error and then you can also properly handle it in the client you can for example say like show different error messages when the password is wrong or show another error when the player doesn't have enough Soul or when he like walked against the wall or whatever so now that we learned about how to write a program let's go a little bit back to the document to the slides and do some theory about it um so this is what we did today like um we created first the game Tiny adventure and here you can see um like we had the data account which has the representation in ASI with the little character walking around we just have move left which decreases the value and we have move right which increases the value then I want to quickly show you or explain you what actually happens with the transaction so here we have the client and the client uh in our case in the playground it was a JavaScript client but you can build clients in many languages I will later show you the unity SDK for example where you can just write your salana program clients in C and there you can also write it in Python and so on so what you do is you create a transaction and in this transaction you create an instruction in this case the move right instruction this one is then sent to one of the RPC C noes this um is the URL that you put in when you create a transaction like when you create a connection to an RPC node like usually you would just use defnet dos sol.com or something but you can also get like um RPC nodes from Helios for example or from Quick node and usually the ones that you pay for are a bit faster and um normal Solana ones shouldn't be used in production since they are very heavily rate Limited anyway this transaction gets serialized and then it's gets sent to one of these RPC nodes and then it's going to be sent directly to the leader of the validators so in Solana every block you have or every few blocks you have a different leader and the leader create the block and then all the validators um validate that the block is correct actually and then so it's sent to the validator it updates at State and then the state is in small chunks sent back to all the RC nodes uh these little chunks of the of the smallest chunk of a block is actually called a shred maybe you saw this somewhere when people discuss on Twitter and yeah then the RPC node um updates his State and uh from the client we can open a websocket connection to this RBC node and then as soon as the account changes it will be pushed to our client so that's very good for games like if I for example show you this game here again then we can see that um like almost as soon as I uh as I approve the transaction I already get the update so this is like less than a second and um what makes it so fast is that I'm not like poing the data for example like every second or every few hundred milliseconds but I have a web socket connection to the state and as soon as the account changes in this case our game data account it gets pushed to the client and we can immediately update so that is very good and this is very fast on Solana then um yeah this is Tiny Adventure 2 and it's basically the same as before just that we now have also the chest Vault account with the seeds and the Lampard that are in there and yeah we looked at it already in the code so I don't need to go into too much into detail but as soon as we reach position three we transfer the soul to the player and this can be used for all kinds of things like imagine what you could do now with this game is you could instead of moving left and right you could create a grid like uh like I did in soul Hunter and you can can let the player move left right up down you could have some quiz games where people need to put in their correct answers um this could also be multiplayer game and yeah uh again to show show a few more examples maybe later if we still have time then what I want to show you now is uh how you can build a client from for this game in unity so using the Solana Unity SDK it's currently maintained by um magic block and it's open source so you can either just uh include it into your game like this but it is also now a verified solution in the unity asset store so you can just very conveniently included like this in your project it also has a decent documentation so salana. Unity sdk. you can like there's some sample scenes where you can connect your wallet um configurations uh how you do token accounts and so on so that is very nice and but um it's a bit inconvenient to like create these uh account discriminators and all that stuff by hand so there's a very cool thing that you can do is you can generate a C client from the code that uh is in the IDL and an IDL is a Json representation of your program so if you're in the playground you can go here on this little Hammer icon and then click on IDL and exported and I did this before already and then you can get this um this Jason file here and how this looks like is just basically a representation of all the accounts and the data that you need to call these functions in adjacent representation and what you can do with that is you can convert this into a C client so for that you install this tool here uh solar. Unity anchor tool and then you just call this command here it just has an net anchor gen then it has one input is the IDL file and then it exports your um C client so let's quickly do that here um net anchor gen and then I put in the Json file I just downloaded from the playground which is the tiny Adventure so tiny Jason and then we say minus o should be the output and then we just say tiny. Cs and what this does is it creates us this C file here and we can open this and then here we can see it has everything that we also had in the JavaScript client so it has um the move right instruction it has the all the data for the chest Vault account it even has our little error here so this is Tiny Adventure 2 actually um yeah it has um you can get all the data so you can just say hey give me the current data account like give me the position of the player and and you can just very conveniently call these from C and then you can build your game in unity and Export it as webgl for example um this is how the tiny Adventure T2 would look like so if I move right here without a password um which I should get an error actually maybe I have the wrong program ID in this one and this is how the JavaScript and the C code look side by side so you can see it's almost the same like you create a transaction you set a fee payer you get a recent block has um if you're interested like this block has is only there actually so that the validators know when they should have a transaction expired so these block hases are valid for like 30 seconds so you can just put the block and then reuse it for a few seconds and then get a new one this is a tiny performance improvements uh per Improvement if you're in a game then you might not want to do an RPC call to get the block hash and then put it in the transaction but you can already prefetch them and then you have a few milliseconds uh that your game is even faster and yeah this the same how it looks like in in C basically you just create a transaction you set a fee payer the block hash put your instruction put a signature and then you send it and the unity SDK also supports a bunch of things already so you can connect to your Phantom wallet you can connect on mobile to Phantom via deep links so you can actually build a mobile casual game with this now if you want and yeah now I uh if you still have time I could now show you a little bit about the client in unity so now we in unity so I quickly got to show you how the code looks like this in unity um we just go to the tiny Adventure service um you can just check out this repository later and then try it out yourself you just need to like check out the repository and start the tiny Adventure scene and then you will have the same as here so here you can see we did the same thing as in the playground so we Define the program ID um this case it's tiny something then um in awake we create the program address like we find the PDA it's the same thing again the string and the program ID and a bump then here we do the socket connection so that we always get direct updates from the account so we just subscribe to pup key data with the game data account PDA then um we deserialize the data and then we publish the data and in the tiny Adventures screen we are listening to this change and then we update the game data View and all we do here is basically the same thing again we just show a string and um yeah this is how you can get the data you just do um get account info Asing and the this is how you initialize move left move right and here is how you would um create the whole transaction so the same thing we did before we create a transaction we set a fee payer we get a recent block hash um and then we put our move left instruction and this one is just like from our program that we created from the IDL we can set the account here then we can like just from our tiny adventure program get the move left instruction put in the account and the program ID and then we can send this transaction to move the player um yeah so this is how it looks like in unity so in c u but I also want to quickly show you how it would look like um if you build a react client so for this one I will also put it into the description uh or in the slides later you can download um but this is how it looks like if you build it in JavaScript so you can basically build a client in whatever you want for the this so I connect my wallet I move right I approve and then I move and uh yeah it's basically always the same um here we uh print the player position um when we click on initial initialize we just do program method initialize put in the accounts say transaction and then in this case like in in react um we used the the salana mobile salana wallet adapter to send the transaction so this creates this nice button here where you can use different wallets cuz currently the unity SDK only supports um Phantom wallet or I think also Soul flare but um yeah this is how you do it in JavaScript like when I click on the button right then I do the move right instruction I put in the game data account I create a transaction so it's a transaction Builder and then in this case we use the transaction Builder and we confirm the transaction with the latest block hash so now that you know how to build an onchain game on salana and uh also build different clients for it where can you go from here so uh I have a few game ideas that I just wrote down We can brainstorm later a little bit when the stream is over what we uh what else we could do but you could for example build a proper salana Royale like you could take the soul Hunter source code it's open source and then you could like improve on it like you could for example have people shoot you could have a round Tim that only every five seconds you can move you could have like a castle that spawns units you could try to build a tower defense game even like it's maybe a bit more complicated because you need to synchronize the times between the clients but if someone can pull it off that would be really cool then uh any any kind of trading card game would be great of course like HearthStone for example where the cards could be nfts and you play them and then yeah you have this round based game basically then something like puzzle Pirates would be cool like I don't know if you know this but it's like was a very popular game with a lot of mini games um you could have a Mets three game on game I think someone did this already now like uh for the grizzly th it's called D Quest so you can look it up it's also Open Source by the way it has a very interesting matchmaking uh which is on chain so that's definitely worth checking out it's also fun to play like you pick your nfts and they have different stats and depending on which things you match you attack the player so it's really cool uh any kind of idle game is of course perfect because you could just like um calculate the times and whenever you do a request to your program you could calculate how much time passed and what the player gets for it so you could just generate some tokens and give them to the player and if you want to um call transactions in the future you can use Clockwork that's also very uh interesting project clockwork. XYZ I think it is so this is like a framework where you can automate transactions to be performed in the future so that's also something you could look into then yeah chess of course that's really cool there's already an open source chess game and poker would be cool the thing with poker is the same problem that we had earlier with the password actually we did in the program like as soon as you call the transaction uh everyone can see the password because you need to put it in as the data so it's quite hard to hide data on chain actually there's a few games who do it already I'm going to put the links for that also in the slides um there's a bonk paper scissors game for example where you um hide the data on Chain by on your client and at some point you need to reveal and then the game decides who wins um then you could also build something like Boulder dish I don't know if someone still knows it's like where you walk around and then Stones Fall Down something like this could be a cool or like we said earlier warms game for example or some location based games and uh what also would be really cool if someone Builds an Airbnb on salana actually I mean it's a bit off topic since it's not a game but would be really cool if you could just pay for your Flats in uh with uh with crypto and then the entry for the flat could for example be an nft that gets burned after you after your time is up or you could um save them and have some kind of travel book or something so that would be really cool here are a bunch of resources that you can uh look at later I'm going to put a few more links uh in these and yeah thank you very much for listening and I hope you learned something about building games and I hope you're going to build some games let me know um you can find me on Twitter soulplay yonos and yeah I'm always interested and excited about new games so if you build something just uh send them to me or uh link me on your posts about it and yeah tell everyone about it like get it out early um get feedback from the community play test iterate on your games and yeah I hope I'm going to see you at one of the hecka houses from Solana and see you next time bye-bye
Info
Channel: Solana
Views: 4,905
Rating: undefined out of 5
Keywords:
Id: KT9anz_V9ns
Channel Id: undefined
Length: 53min 4sec (3184 seconds)
Published: Mon Jul 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.