Game Update: Show me the loot!

When I finished the last game update my only focus was on the loot system, and I am here today to report that it is functioning beautifully. The biggest hurdle I faced is the poor documentation provided by WAX and AtomicHub. Luckily, WAX shares its codebase with EOS, a more mature blockchain, which has more mature documentation. After reading through docs on the EOS side, this started to look pretty straightforward.

First Steps

First things first, I needed a loot item. I created a new schema for “loot” and then a new template for a “Rat Pelt” (if you want to see how this is done, go back to this post). I created a simple loot item that is just an image and a name.

an example of a loot item NFT

These tokens will eventually be combined to create new items, so they don’t necessarily need much more information than that, but I will probably add a “loot value” property for future use / in-game markets.

I also modified my enemies schema to add a “loot” property to the enemies schema. This is an optional field, but if it is set to a template number, that will be the reward minted if the enemy is defeated.

Loot set to template #741510

Implementing the Loot System

First things first, I have to make sure my loot information is being set to my enemies when I query for the information, so the enemy object has to be modified to add that field.

As you can see, loot will be set to either the integer number of the reward template or it will be null and the player won’t receive an item after the enemy is defeated.

Next I need to add some dependencies to my back-end to make this work. I need WaxJS, a few modules from EOSJS, and node-fetch to make my calls to the blockchain.

After they’re brought in, I need to initialize the EOS modules to generate a signature to be used to send the transaction to the blockchain

And now the API is ready to use

Creating the Transaction

In my battle logic, if the enemy is defeated then it’s time to start doling out rewards. After the character’s experience is updated based on the experience given by the enemy, I’ll check to see if the enemy has any loot.

If loot isn’t null, the transaction will be built. First I need the required data. Here authorized_minter is the public address of the wallet authorized to mint this NFT and collection_name is the collection the NFT is coming from. You can see template_id is being set by lootItem and the wallet that the NFT is going to be sent to is the wallet associated with the character. I can add more properties to the NFT with the final three empty arrays but my values are already going to be set by the template, so for now I’m going to leave them empty.

Building the action

Now we just have to put that information into the action to prepare to send the transaction. I’m using the atomicassets contract and I’m using the mintasset function available to it. You can see I’m passing the authorized_minter variable to authorization. Then the rest of the variables will be put into data.

actions after being assembled

Sending the transaction

Finally, I just need to push the transaction to the API. The transact method takes the actions object as a parameter to be sent, bundled with other options including the privateKey associated with the minting wallet.

Sending the transaction

If I’m successful, I’ll see the transaction_id and the status in the server console logs, and I am.

The player will now see the loot item in their wallet!

Conclusion

This was a very important system to implement. I can’t have a blockchain game if I’m not minting NFTs, and this will be the basis for any other NFT minting and transacting I’ll need to do.

While I was building this, I noticed quite a few small items that I didn’t want to forget to do, so I’ve started using a fairly popular (free) project management app called Trello to start acting a little more Agile.

As you can see, I’ve got some big items Done. Next, I’m going to build out a little component to display the user’s loot in the inventory screen. Once that’s finished, I’ll move it over to Done and I’ll move another item from the To-Do list into Doing.

Leave a Reply

Your email address will not be published. Required fields are marked *