# Custom Anchor Program

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](https://www.youtube.com/watch?v=_vQ3bSs3svs)

## Add AnchorProgram Node

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)

## Import the IDL

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.

<figure><img src="/files/sRdymJVLbOYGlrCRWRif" alt=""><figcaption></figcaption></figure>

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:

<figure><img src="/files/OSX6n3d7V1OtaLEZyLwH" alt=""><figcaption></figcaption></figure>

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!&#x20;

<figure><img src="/files/zPOEqtKJMpuVhRGqLQqp" alt=""><figcaption></figcaption></figure>

## Call The 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.

```
func init_game() -> void:
	var tiny_adventure_key:Pubkey = Pubkey.new_from_string(tiny_adventure_pid))
	var level_pda:Pubkey = Pubkey.new_pda(["Level1"],tiny_adventure_key)
	var vault_pda:Pubkey = Pubkey.new_pda(["Vault1"],tiny_adventure_key)
	
	var prize_in_lamports:int = int(chest_prize*pow(10,9))
	var instructions:Array[Instruction]
	
	var init_ix:Instruction = anchor_program.build_instruction("restartLevel",[
		level_pda, #gamedata
		vault_pda, #gamevault
		SolanaService.wallet.get_kp(), #signer
		SystemProgram.get_pid() #system program
	],
	AnchorProgram.u64(prize_in_lamports))
	
	instructions.append(init_ix)
	var transaction:Transaction = await SolanaService.transaction_manager.create_transaction(instructions)
	var tx_data:TransactionData = await SolanaService.transaction_manager.sign_and_send(transaction)
```

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 ](/solana-godot-sdk-docs/core-concepts/program-derived-address-pda.md)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 ](https://github.com/magicblock-labs/SOAR)program by Magicblock. You can find it in *SolanaSDK -> Demos -> HighscoreDemo* in the addon!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zenwiki.gitbook.io/solana-godot-sdk-docs/guides/custom-anchor-program.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
