Sui Development Tutorial: How to create characters and props for role-playing games on SUI?

This article is machine translated
Show original

Original: " RPG Building Basics - Encode Club Sui Series #5 " by SUI Foundation

Compilation: SUI World

The Sui Foundation has partnered with Encode Club to provide a series of six developer-focused educational videos ranging from the basics of Sui to tutorials on building smart contracts and using objects in Sui Move. Previous videos refer to SUI World tweets.

This tutorial will show us how to create characters and props for role-playing games (RPG), and how to make them interact, don't miss it! For details, click "Read the original text" to view the video.

learning highlights

Due to Sui Move's object-centric programming model and its scalability, Sui is poised to be the first blockchain to truly deliver web2 experiences to web3, including games, which are inherently complex to program, And a robust infrastructure is required to ensure a seamless experience for players. Thanks to Sui Move's object-centric programming model and its extensibility, Sui was up to the challenge.

Let's look at a coded example of an on-chain RPG in Sui Move. The following example is adapted from Sam Blackshear's hero.move code.

https://github.com/MystenLabs/sui/blob/main/sui_programmability/examples/games/sources/hero.move?ref=blog.suifoundation.org

Sui Development Tutorial: How to create characters and props for role-playing games on SUI?

The code above defines our playable character. As can be seen from its domain, this hero is comparable to other characters in RPGs, with health points (HP), experience, and inventory.

Sui Development Tutorial: How to create characters and props for role-playing games on SUI?

The code above shows our hero's sword, notice that this sword has a key and a storage ability. Looking back at the previous lessons in this series, a key means that it is an ownable asset that can exist in top-level storage. Move objects in this category can also be accessed from external APIs, creating the unique possibility for Sui to use items in multiple games. And storage means that this object can be freely packaged and transferred.

Sui Development Tutorial: How to create characters and props for role-playing games on SUI?

Above, we defined boars, non-playable characters (NPCs) or enemies in our game, similar to other games of this genre, we can create NPCs for our heroes to fight and gain experience, or buy items and accept quests.

The Action

Sui Development Tutorial: How to create characters and props for role-playing games on SUI?

The actions shown in the code above describe the slay function. At a high level, this function first checks to make sure both Hero and Boar belong to the same game instance. Then a duel between the hero and the boar happens, check to make sure the hero's HP cannot reach zero. After the duel, the hero gains experience points proportional to the boar, and the strength of the hero's sword increases by 1 (if the hero uses a sword). Finally, the function emits an event BoarSlayEvent. Events in Sui Move allow indexers to track actions on the chain, which is an important means of achieving universally recognized object states.

The code sample above is a brief excerpt of Sam's hero.move code. This code provides an invaluable example for game developers on Sui, and since it's open source, feel free to fork and build your own!

Source
Disclaimer: The content above is only the author's opinion which does not represent any position of Followin, and is not intended as, and shall not be understood or construed as, investment advice from Followin.
Like
Add to Favorites
Comments