create account

Bitshares RPG-JS Tutorial - Selecting an user sprite & changing accounts without refreshing by nftea.gallery

View this thread on: hive.blogpeakd.comecency.com
· @nftea.gallery · (edited)
$8.84
Bitshares RPG-JS Tutorial - Selecting an user sprite & changing accounts without refreshing
![image.png](https://images.hive.blog/DQmSz7NPUhG1xCrJaNvXiM565HL7C1jj2HNGAMkVDBtUHfY/image.png)

As you may have been following, I've recently been blogging about my efforts working with the [rpg-js](https://rpgjs.dev/) framework, check out my profile if you've missed out on this content.

Today's tutorial covers several areas - the initial player gui, sprites and inventory items!

## Enabling the user to select a character sprite!

So, up until now the user has just been allocated a default hero character sprite, this works well but it distinctly lacks personalization, so by letting the user select their preferred looks hopefully they feel a greater personal attachment with their in game character, even though there's no real gameplay going on right now..

So, after sourcing some additional sprites for the user to pick between, I added this step after the user has selected the new account they want to use:

```
      <div v-if="chain && method === 'sprite'">
        <h3>Choose how your character appears</h3>
        <sl-radio-group :value="spriteType" @sl-change="spriteType = $event.target.value" size="medium" label="Select a sprite type" name="gender">
          <sl-radio-button style="margin-top: 10px;" pill value="male">Male</sl-radio-button>
          <sl-radio-button style="margin-top: 10px;" pill value="female">Female</sl-radio-button>
        </sl-radio-group>

        // image removed for hive - check github for full code

        <p>Viewing {{ spriteValue }} of {{ spriteTypeQty }} {{ spriteType }} sprites</p>

        <div class="smallGrid">
          <sl-button
            variant="neutral"
            @click="spriteValue > 0 ? (spriteValue -= 1) : (spriteValue = spriteTypeQty - 1)"
            size="small"
            pill
          >
            Previous
          </sl-button>
          <sl-button
            variant="neutral"
            @click="spriteValue < spriteTypeQty - 1 ? (spriteValue += 1) : (spriteValue = 0)"
            size="small"
            pill
            >Next</sl-button
          >
        </div>

        <div class="microGrid">
          <sl-button
            variant="primary"
            @click="
              setCurrentUser(
                searchResult.name,
                searchResult.id,
                searchResult.referrer,
                `${spriteType}-${spriteValue}`,
                chain
              );
              closeGUI();
            "
            size="small"
            pill
            >Use this sprite</sl-button
          >
        </div>

      </div>
```
.
This code lets the user easily peruse the available sprites, and when ready will save the additional sprite info to the persistent user nanostore!

Let's see how this looks in action!

https://s12.gifyu.com/images/SYZyE.gif

Once we've stored our recently used Bitshares account to persistent nanostore state, we can look up our previously used accounts and the sprite will now be shown alongside a new "delete" button, to erase your historical account usage in game.

```
      <div v-if="chain && method === 'existing'">
        <p>
          {{
            chain === "bitshares"
              ? "Selecting a previously used Bitshares (BTS) account"
              : "Selecting a previously used Bitshares testnet (TEST) account"
          }}
        </p>
        <div
          v-if="
            storedUsers &&
            storedUsers.users &&
            storedUsers.users.length > 0 &&
            storedUsers.users.filter((user) => user.chain === chain)
          "
        >
          <p>Choose an account from the list below:</p>

          <div style="max-height: 200px; overflow-y: scroll;" class="microGrid">
            <div v-for="user in storedUsers.users.filter((user) => user.chain === chain)">
              <sl-button
                variant="default"
                @click="
                  setCurrentUser(user.username, user.id, user.referrer, user.sprite, chain);
                  closeGUI();
                "
                :key="user.id"
                style="margin: 5px; width: 80%;"
              >
                <div class="parent">
                  <div
                    :style="{
                      width: '32px',
                      height: '32px',
                      backgroundImage: `url(/main/spritesheets/characters/${user.sprite}.png)`,
                      backgroundPosition: '-32px 0px',
                      backgroundSize: '96px 128px'
                    }"
                  ></div>
                  <div>
                    {{ user.username }} ({{ user.id }})
                  </div>
                </div>
              </sl-button>

              <sl-button
                variant="default"
                @click="removeUser(user.id)"
                :key="`${user.id}_remove`"
                style="margin: 5px;"
              >
                ❌
              </sl-button>
            </div>
          </div>
        </div>
        <div v-else>
          <p>No previously used accounts found, please use a new account.</p>
          <sl-button slot="footer" variant="primary" @click="method = 'new'" style="margin-bottom: 10px;">
            Find account
          </sl-button>
        </div>

        <sl-button slot="footer" variant="neutral" @click="method = null;" style="margin-top:15px;">Back</sl-button>
      </div>
```
.
Let's see it in action!

https://s10.gifyu.com/images/SYZy9.gif

## Enabling the user to switch between accounts without refreshing

Rather than have to refresh the app to be able to switch to another blockchain or to another account on the same blockchain, why not offer the user an item which allows them to do just that?

Introducing - the account switcher!

```
import { RpgPlayer } from '@rpgjs/server'
import { Item } from '@rpgjs/database'

import { $currentUser, eraseCurrentUser } from '../nanostores/users';
import { playerGold } from '../common/player';

@Item({
    id: 'accountChanger',
    name: 'Account changer',
    description: 'Switch between accounts and blockchains with this tool.',
    price: 0,
    consumable: true // Must be true for GUI to launch
})
export default class Book {
    async onUse(player: RpgPlayer) {
        await player.gui("intro").open({}, {waitingAction: true, blockPlayerInput: true});

        const usr = $currentUser.get();
        await playerGold(player, usr);

        player.setGraphic(usr.sprite);

        player.items = [];
        player.addItem("accountChanger", 1); // default user item
        player.changeMap('map');
    }
    
    onRemove(player: RpgPlayer) {
        player.addItem('accountChanger', 1); // avoid selling the tool
    }
}
```
.
It does what it says on the tin, this item when selected from the user's inventory will launch the initial `intro` GUI, from where the user can select a chain and blockchain account to proceed with.

When will the user be given this item? Immediately after the user selects their account!

```
  async onJoinMap(player: RpgPlayer) {
    // User selects an account here
    player.addItem("accountChanger", 1); // give item to user
  },
```
.
And finally, let's see the account changer item in action!

https://s10.gifyu.com/images/SYZyh.gif

Awesome stuff! Several quick coding wins resulting in greatly improved potential user experience in game!

---

Have any questions? Comment below!

---

These developments were brought to you by the [NFTEA Gallery](https://nftea.gallery).

Consider collecting an [NFTEA NFT](https://nftea.gallery/gallery) to or donate to the following [BTS](https://blocksights.info/#/accounts/nft.artist) or [EOS](https://bloks.io/account/nfteagallery) blockchain accounts directly, to fund continued developments.

Don't have a Bitshares account? [Make one today](https://bts.exchange/?r=nftprofessional1)!
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 394 others
properties (23)
authornftea.gallery
permlinkbitshares-rpg-js-tutorial-selecting-an-user-sprite-and-changing-accounts-without-refreshing
categoryhive-120117
json_metadata"{"app":"hiveblog/0.1","description":"A tutorial on how to enable the user to change their sprite in rpg-js when selecting a bitshares accoutn","format":"markdown","image":["https://images.hive.blog/DQmSz7NPUhG1xCrJaNvXiM565HL7C1jj2HNGAMkVDBtUHfY/image.png","https://s12.gifyu.com/images/SYZyE.gif","https://s10.gifyu.com/images/SYZy9.gif","https://s10.gifyu.com/images/SYZyh.gif"],"links":["https://rpgjs.dev/"],"tags":["bitshares","development","developers","typescript","programming","opensource","tutorial"]}"
created2024-06-12 22:21:36
last_update2024-06-13 16:43:03
depth0
children5
last_payout2024-06-19 22:21:36
cashout_time1969-12-31 23:59:59
total_payout_value4.442 HBD
curator_payout_value4.393 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,813
author_reputation182,932,753,241,857
root_title"Bitshares RPG-JS Tutorial - Selecting an user sprite & changing accounts without refreshing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id134,557,377
net_rshares29,679,658,015,550
author_curate_reward""
vote details (458)
@hivebuzz ·
<center>[![](https://images.hive.blog/175x175/https://hivebuzz.me/api/level/nftea.gallery?202406131558)](https://hivebuzz.me/@nftea.gallery)
<center>@nftea.gallery, sorry to see that you have less Hive Power.
Your level lowered and you are now a **Red Fish**!</center>

**Check out our last posts:**
<table><tr><td><a href="/hive-122221/@hivebuzz/lpud-202406"><img src="https://images.hive.blog/64x128/https://i.imgur.com/pVZi2Md.png"></a></td><td><a href="/hive-122221/@hivebuzz/lpud-202406">LEO Power Up Day - June 15, 2024</a></td></tr></table>
properties (22)
authorhivebuzz
permlinknotify-1718294782
categoryhive-120117
json_metadata{"image":["https://hivebuzz.me/notify.t6.png"]}
created2024-06-13 16:06:24
last_update2024-06-13 16:06:24
depth1
children0
last_payout2024-06-20 16:06:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length549
author_reputation369,451,992,994,316
root_title"Bitshares RPG-JS Tutorial - Selecting an user sprite & changing accounts without refreshing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id134,572,656
net_rshares0
@redditposh ·
https://www.reddit.com/r/BitShares/comments/1guzugo/bitshares_rpgjs_tutorial_selecting_an_user_sprite/
<sub> The rewards earned on this comment will go directly to the people sharing the post on Reddit as long as they are registered with @poshtoken. Sign up at https://hiveposh.com. Otherwise, rewards go to the author of the blog post.</sub>
properties (22)
authorredditposh
permlinkre-nfteagallery-bitshares-rpg-js-tutorial-selecting-an-user-sprite37165
categoryhive-120117
json_metadata"{"app":"Poshtoken 0.0.2","payoutToUser":[]}"
created2024-11-19 15:32:27
last_update2024-11-19 15:32:27
depth1
children0
last_payout2024-11-26 15:32:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length343
author_reputation2,820,537,088,328,690
root_title"Bitshares RPG-JS Tutorial - Selecting an user sprite & changing accounts without refreshing"
beneficiaries
0.
accountnomnomnomnom
weight10,000
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id138,557,935
net_rshares0
@stemsocial ·
re-nfteagallery-bitshares-rpg-js-tutorial-selecting-an-user-sprite-and-changing-accounts-without-refreshing-20240613t035738397z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-nfteagallery-bitshares-rpg-js-tutorial-selecting-an-user-sprite-and-changing-accounts-without-refreshing-20240613t035738397z
categoryhive-120117
json_metadata{"app":"STEMsocial"}
created2024-06-13 03:57:39
last_update2024-06-13 03:57:39
depth1
children0
last_payout2024-06-20 03:57:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,930,909,407,157
root_title"Bitshares RPG-JS Tutorial - Selecting an user sprite & changing accounts without refreshing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id134,561,647
net_rshares0
@wlffreitas ·
It's so interesting how it's structured and connected and how well it all seems to work. 
From what I understand, the framework itself doesn't yet offer 100% support for running on blockchain or something like Web3, but you can easily create an MMORPG with it in JS.
This is a great project, and I'll be following it further.
👍  
properties (23)
authorwlffreitas
permlinkre-nfteagallery-sf064v
categoryhive-120117
json_metadata{"tags":["hive-120117"],"app":"peakd/2024.6.5"}
created2024-06-13 05:05:21
last_update2024-06-13 05:05:21
depth1
children1
last_payout2024-06-20 05:05:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length325
author_reputation89,938,242,082,961
root_title"Bitshares RPG-JS Tutorial - Selecting an user sprite & changing accounts without refreshing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id134,562,548
net_rshares15,400,500,272
author_curate_reward""
vote details (1)
@nftea.gallery ·
Yeah I was really impressed by how the rpg-js compiler works, there's so much you can build with just tiled and limited programming skills, it's still a lot more free and open source than propriatery rpg maker software alternatives currently out there
👍  
properties (23)
authornftea.gallery
permlinksf0q6u
categoryhive-120117
json_metadata{"app":"hiveblog/0.1"}
created2024-06-13 12:18:30
last_update2024-06-13 12:18:30
depth2
children0
last_payout2024-06-20 12:18:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length251
author_reputation182,932,753,241,857
root_title"Bitshares RPG-JS Tutorial - Selecting an user sprite & changing accounts without refreshing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id134,568,396
net_rshares3,006,931,907
author_curate_reward""
vote details (1)