Testnet is now LIVE at testnet.spacetimedb.com! NOTE: This is a testnet, and all data will be wiped periodically.

1.0 RC1

Login

Unity Tutorial - Advanced - Part 5 - BitCraft Mini

Need help with the tutorial? Join our Discord server!

This progressive tutorial is continued from the Part 4 Tutorial.

Oct 14th, 2023: This tutorial has not yet been updated for the recent 0.7.0 release, it will be updated asap!

BitCraft Mini is a game that we developed which extends the code you've already developed in this tutorial. It is inspired by our game BitCraft and illustrates how you could build a more complex game from just the components we've discussed. Right now you can walk around, mine ore, and manage your inventory.

1. Download

You can git-clone BitCraftMini from here:

git clone ssh://git@github.com/clockworklabs/BitCraftMini 

Once you have downloaded BitCraftMini, you will need to compile the spacetime module.

2. Compile the Spacetime Module

In order to compile the BitCraftMini module, you will need to install cargo. You can install cargo from here:

Note

Once you have cargo installed, you can compile and publish the module with these commands:

cd BitCraftMini/Server
spacetime publish 

spacetime publish will output an address where your module has been deployed to. You will want to copy/save this address because you will need it in step 3. Here is an example of what it should look like:

$ spacetime publish
info: component 'rust-std' for target 'wasm32-unknown-unknown' is up to date
    Finished release [optimized] target(s) in 0.03s
Publish finished successfully.
Created new database with address: c91c17ecdcea8a05302be2bad9dd59b3 

Optionally, you can specify a name when you publish the module:

spacetime publish "unique-module-name" 

Currently, all the named modules exist in the same namespace so if you get a message saying that database is not owned by you, it means that someone else has already published a module with that name. You can either choose a different name or you can use the address instead. If you specify a name when you publish, you can use that name in place of the autogenerated address in both the CLI and in the Unity client.

In the BitCraftMini module we have a function called initialize(). This function should be called immediately after publishing the module to spacetimedb. This function is in charge of generating some initial settings that are required for the server to operate. You can call this function like so:

spacetime call "<YOUR DATABASE ADDRESS>" "initialize" "[]" 

Here we are telling spacetime to invoke the initialize() function on our module "bitcraftmini". If the function had some arguments, we would json encode them and put them into the "[]". Since initialize() requires no parameters, we just leave it empty.

After you have called initialize() on the spacetime module you shouldgenerate the client files:

spacetime generate --out-dir ../Client/Assets/_Project/autogen --lang=cs 

Here is some sample output:

$ spacetime generate --out-dir ../Client/Assets/_Project/autogen --lang cs
info: component 'rust-std' for target 'wasm32-unknown-unknown' is up to date
    Finished release [optimized] target(s) in 0.03s
compilation took 234.613518ms
Generate finished successfully. 

If you've gotten this message then everything should be working properly so far.

3. Replace address in BitCraftMiniGameManager

The following settings are exposed in the BitCraftMiniGameManager inspector: Module Address, Host Name, and SSL Enabled.

Open the Main scene in Unity and click on the GameManager object in the heirarchy. The inspector window will look like this:

GameManager-Inspector

Update the module address with the address you got from the spacetime publish command. If you are using SpacetimeDB Cloud testnet, the host name should be testnet.spacetimedb.com and SSL Enabled should be checked. If you are running SpacetimeDB Standalone locally, the host name should be localhost:3000 and SSL Enabled should be unchecked. For instructions on how to deploy to these environments, see the Deployment Section

4. Play Mode

You should now be able to enter play mode and walk around! You can mine some rocks, cut down some trees and if you connect more clients you can trade with other players.

5. Editing the Module

If you want to make further updates to the module, make sure to use this publish command instead:

spacetime publish <YOUR DATABASE ADDRESS> 

Where <YOUR DATABASE ADDRESS> is your own address. If you do this instead then you won't have to change the address inside of BitCraftMiniGameManager.cs

When you change the server module you should also regenerate the client files as well:

spacetime generate --out-dir ../Client/Assets/_Project/autogen --lang=cs 

You may want to consider putting these 2 commands into a simple shell script to make the process a bit cleaner.

Edit On Github