In this tutorial we will create a very simple plugin that displays a button in the top menu, that when clicked will open a new window to display all currencies in your wallet.
Setup #
To begin, create a new folder called wallet under plugins/. Next create a new scene and call it Ingame. Since we want our plugin to add a button to the top menu, we choose Other Node and then PluginButton as our scene root. By setting the Menu variable to Top Menu this will automatically put our button into the menu at the top of the screen when activated.
Note: Make sure to also enter some text for the Button or it won’t be visible!
Next we add a WindowDialog to display the information, a ScrollContainer and an ItemList to display all currencies. Rename the WindowDialog to WalletDialog and the ItemList to Wallet and also set the Max Columns property of the ItemList to 2 and Same Column Width to true.
Code #
With the initial setup out of the way we can now begin to implement the actual functionality of our plugin. For that, we first need to extend the WindowDialog with a new script
To retrieve information about the wallet we need to get a reference to the WalletManager. We can do this by simply declaring a new onready variable at the top of our script and retrieving the WalletManager from the GameServerClient
@onready var wallet = ClientSession.gs.get_manager(WalletManager)
Note: declaring this as a normal variable will crash the client, as the WalletManager is only available once the Ingame scene is ready!
To display the currency information we will simply override the _process method to constantly refresh the data.
func _process(_delta):
$ScrollContainer/Wallet.clear()
var currencies = wallet.currencies.keys()
currencies.sort()
for currency in currencies:
$ScrollContainer/Wallet.add_item("%d" % [currency])
$ScrollContainer/Wallet.add_item("%d" % [wallet.currencies[currency]])
Note that we first clear the Wallet list to get rid of all information from the previous frame and then add a new item for every currency. To display a nice header we can also add
$ScrollContainer/Wallet.add_item("currency")
$ScrollContainer/Wallet.add_item("amount")
right after the call to clear.
The only thing that is now missing is to connect the pressed signal of our Button to the show method of our WindowDialog and our basic Wallet plugin is ready:
To test the plugin simply launch the client, login and activate it in the settings menu.
Full code available here.