Introducing Scriptable Objects for creating weapons

In this video we introduced scriptable objects and have started the process of using them for storing our weapon data. If you have any questions or observations, here’s the place to add them!

How will I ever get past these troublesome guards with my new SO sword…hmm

Doesn’t look like its performing as you’d hope!

Finished following the video to the letter, and Unity informs me that “the type or namespace name ‘Weapon’ could not be found (are you mikssing a using directive or an assembly reference?)”, and sends me to the line in the fighter script: " [SerializeField] Weapon weapon = null;". The “Weapon” script is a carbon copy of what Rick wrote - I even copied it from the Github Commit.
Any advice?

What namespace is the Weapon script in? Are you using the proper “Using” statement to include that namespace so you can reference it? Pop a link to the code in here so we can review it and or a paste bin link.

Weapon is written in namespace RPG.Combat, and the Using statements I have in Fighter are for UnityEngine, RPG.Movement and RPG.Core

So I have been working with SOs in my other games I used visual scripting in as data containers once I learned how to access them with bolt, but I had no idea that you could run code from them as well.

Yes, they are extremely versatile!

1 Like

So what exactly is a Scriptable Object? The video doesn’t really explain this properly.

From the docs it seems that it’s a data container, typically read-only to the Game, that can be used to aggregate data (such as Weapon info in this case) into an object that can be assigned to a Field.

From what I can gather, it’s possible to write code that the Editor will run that can create such Scriptable Objects, a bit like the “Make New Weapon” function did, although I assume there’s a way to do it programmatically so that it can query the user or contain default data?

Is there a good example of a Scriptable Object that we’ve been using until this point without knowing what it truly is?

This article helped a little - so it’s really just a data store in your Assets folder, but defined in code?

One thing I don’t understand from the video is why we couldn’t achieve the same thing by simply adding a MonoBehaviour component (with an appropriate Spawn function) to the Equipped Sword prefab? What advantage does the Scriptable Object provide in this case?

2 Likes

You actually have a fairly good handle on the Scriptable Objects from your descriptions…

The advantage using a Scriptable Object has over using a MonoBehavior on the Equipped Sword Prefab (by the way, we’ll be doing that later, so that the sword can emit sounds effects!) will become clearer when we start swapping weapons, as well as loading and saving the current weapon.

2 Likes

What if we create empty game objects for each type of weapon and add a script to them ( which will have the serialized fields and spawn method, etc) and save them as prefab? Also to make them of type Weapon , we can add a script called Weapon on each of them. Will that be different than a SO?

It would, and you would need to have a mechanism for tracking which weapon the player is using during loading and saving.
With the ScriptableObjects, we can save the name (after the Inventory course, a unique id) for the item when we save the weapon, and load the data from the ScriptableObject.