Page 1 of 1

How to save a pickup and how to save ammo of a character.

Posted: Tue May 24, 2016 1:09 am
by Azola21
I am using realistic FPS prefab and I would like to know how I could get the dialogue system to save a gun that I drop and also how to get the save system to save the amount of ammunition I have for my weapon.

For the weapon I would like it to save a weapon for example, if I am swapping weapons with a dead NPC's weapon I want it to save the weapon I just dropped so that next time i launch the game I can then pick it up again.

For the ammo count I want it to save how much ammo i have in the current magazine and also how much ammo I have in reserve.

Re: How to save a pickup and how to save ammo of a character.

Posted: Tue May 24, 2016 10:20 am
by Tony Li
Hi,

To save and load player-held weapons and ammo, import the Realistic FPS Prefab Support package and set it up using these steps: Realistic FPS Prefab Support. This will keep track of what weapons the player is holding, the amount of ammo in the magazines, and the amount of ammo in reserve.

For more advanced inventory and item management, the Dialogue System has support for S-Inventory and a special support package that allows RFPS and S-Inventory to work together.

However, in either case, keeping track of items that have been dropped into the scene will require some scripting. You'll need to write a script that maintains a list of objects that have been newly instantiated in the scene. When the player drops an item into the scene, add it to the list. When the player picks up an item, remove it from the list. You'll probably want to keep track of the type of item, its world position, and (if it's a weapon or clip) how much ammo it contains.

Then add two methods to this script: OnRecordPersistentData() and OnApplyPersistentData(). This will turn the script into a Persistent Data Component.

In OnRecordPersistentData(), record the list into the Dialogue System's Lua environment. One way to do this is to use Unity's JsonUtility class to convert your list into a string. Then use DialogueLua.SetVariable() to record the string into the Lua environment. Since the Lua environment is included in saved game data, this will save your list.

In OnApplyPersistentData(), retrieve the string from the Lua environment using DialogueLua.GetVariable(). Then use JsonUtility to convert it back into a list. Finally, go through that list and re-instantiate all of the objects.

If you get stuck on any of this, let me know and I'll try to help.