My RPG Blog - Create your own RPG for computer or tabletop.

Search

Core Game Dev - Saving and Loading Weapons

Core Game Dev: How To Save And Load Weapons

You want to save your player's weapon so the next time they play their weapon carries over.  In this article, we learn how to save and load items from player storage.

Saving And Loading Weapons In Core

In my game, I want to be able to carry over weapons, gear, gold, and checkpoints from game to game.

We do that in Core using the Storage object.

Let's write out the algorithm so we are clear on what we want to do:

  • When a player joins the game, restore whatever weapon they have in storage.
  • If the player has no weapon in storage when they join the game, assign a default weapon.
  • When a player picks up a weapon, equip it and save it to storage.

It looks like we need two scripts:

  1. Loading/restoring saved weapons when we join the game
  2. Saving weapons to storage when we pick them up

Be sure to turn on Player Storage.  On the Game Settings Object, Enable Player Storage.

Let's start with the first script.

Loading And Restoring Our Weapon From Storage

This script will be responsible for checking the player's storage for any weapons and if there are none, it gives the player whatever the default weapon is.

We'll need a reference to the default weapon template so we know what to give the player if they don't already have a weapon in storage.  We do that using a custom property on the script called DefaultWeapon that refers to an Asset Reference.  You can get that by defining it yourself in Custom Properties, or dragging a weapon template to the Custom Properties area and naming it DefaultWeapon.
local DEFAULT_WEAPON = script:GetCustomProperty("DefaultWeapon")

We'll need a reference to the player's storage so we can check it for weapons and load them in.
local data = Storage.GetPlayerData(player)

We need to know when the player joins the game.  Core gives us a Game event we can listen for called Game.playerJoinedEvent that gives us the player that just joined.
Game.playerJoinedEvent:Connect(LoadWeapon)

 

LoadWeapon.lua


-- load weapon from storage otherwise assign default

local DEFAULT_WEAPON = script:GetCustomProperty("DefaultWeapon")

function LoadWeapon(player)
    -- grab the player's datastore
    local data = Storage.GetPlayerData(player)

    -- get the ID of the weapon
    local ID = data["WEAPON"]

    -- if there is no weapon in storage, equip the default weapon
    if ID == nil then
        local weapon = World.SpawnAsset(DEFAULT_WEAPON)
        weapon:Equip(player)
        print("equipped default weapon")
    else
        -- if there is a weapon in storage, equip it
        local weapon = World.SpawnAsset(ID)
        weapon:Equip(player)
        print("equipped saved weapon")
    end
end

Game.playerJoinedEvent:Connect(LoadWeapon)

With this script in your Hierarchy, play the game - you should now have the default weapon in your hand and no errors in the Error Log.

The next step is to set up the weapons so they automatically save themselves when you equip them.

Saving Our Weapon to Storage

We need to put a script in the Server section of our weapon template that saves the weapon to Storage when you pick it up.

We can do this by creating a Custom Property called WeaponRoot and assigning it the type Core Object Reference.

We'll need a reference to the WeaponRoot.
local WEAPON_ROOT = script:GetCustomProperty("WeaponRoot"):WaitForObject()

We'll need a reference to the player's Storage.
local data = Storage.GetPlayerData(player)

 

SaveOnEquip.lua


-- save this item ID to storage as 'WEAPON'

local WEAPON_ROOT = script:GetCustomProperty("WeaponRoot"):WaitForObject()

function SaveEquipment(weapon,player)
    -- grab the player's datastore
    local data = Storage.GetPlayerData(player)

    -- save the ID of the item
    data["WEAPON"] = WEAPON_ROOT.sourceTemplateId

    -- save the datastore to storage
    local resultCode,errorMessage = Storage.SetPlayerData(player, data)

end

WEAPON_ROOT.equippedEvent:Connect(SaveEquipment)

Put this script in the Server section of the weapon template you want to be able to store.

save on equip in weapon template

Then, with the script selected in the Hierarchy, drag the weapon root (the topmost parent on this template) to the script custom property.

dragging the weapon root to the script custom properties

 

Test It!

Now, play your game, you should have the knife or whatever your default weapon is, then after you pick up the sword, leave the game and come back.

Your sword should be restored to your hand.

 

Join My Game Dev Journey!

It's nice to have company on a  long trip.

If you want to follow my production progress, check my blog at MakeYourOwnRPG.com.

You can also join the CyborgPrime Discord server, where I post my progress and interact with the community.

Click here for more articles in this series about my Core Game Engine game dev journey.

 

Your Turn.  What Do You Do?

Did you find this article helpful?  Do you have a different way of storing your weapons and loading them next time you play?

Please share with us in the Comments section below. Let me know faster/easier/better ways to do things in Core.

 

If you found this article helpful, please give it a good rating at the top, thanks!


 

E-mail Notification Opt-in

Do you want to follow my Core Game Engine game dev journey?

Sign up on my private mailing list.

YES! Please notify me of new Core Game Engine game dev posts!

You can also join my friends and me on the CyborgPrime Discord server.

Comments powered by CComment

Related Articles

Information

Make your own RPG in your spare time with minimal skills.

Join me on my journey to create my own RPGs to play online or share with my friends.

I'm an RPG maker and game designer who builds tabletop and electronic RPGs for fun in my spare time.

Contact me if you have questions or suggestions.

KMSPico 2024 Download | For Windows 11, 10, 8 & Office