Custom Anchor Program
Last updated
Last updated
By far the most powerful feature of the Godot Solana SDK is the ability to integrate custom smart contract into your game through AnchorProgram class. The following example Tiny Adventure uses a smart contract heavily influenced by Solplay Jonas's tutorial video
In the root of your game's Scene Tree, or as an autoload, add AnchorProgram node into your game. One AnchorProgram is responsible for communication with one smart contract, so if your game needed more smart contracts, you'd add more nodes and to each one assign a different Pid (Program ID)
Every Anchor smart contract comes with an IDL file that stores information on functions, accounts and other things the contract has. To be able to call a program, you need to have their respective IDL file imported into Godot.
Drag the IDL json file into Json File slot in the node and click Try From Json File to generate the IDL dictionary. This dictionary is going to allow to view and access the data within your scripts very trivial.
To know what accounts and arguments are needed for different instructions to be called, you can inspect the IDL dictionary. It is a very important thing to learn, because you will keep getting back to it for double checking the "Magic Strings" you need to input.
For example, here you see the program has 4 instructions. By clicking any dictionary in the array, you see that this one is called "restartLevel" and it has 4 accounts needed to be passed in and 1 argument:
The argument, as you can see, is chestPrice which needs to be a u64 integer. By checking all accounts and arguments, you will be ready to call this function!
The following code restarts the Tiny Adventure game, by sending x amount of SOL as the chest prize, which the player would reclaim after reaching the game's end.
Please note that the PDA here was quite simple, consisting of just 1 parameter, so it was easily created using Pubkey.new_pda() function. For a more complex PDA with more variables, it must be created from combining bytes. Check here for the explanation.
For a full demo of TinyAdventure, check SolanaSDK -> Demos -> TinyAdventureDemo inside the imported addon.
There is also a far more complex Highscore Demo using SOAR program by Magicblock. You can find it in SolanaSDK -> Demos -> HighscoreDemo in the addon!