- Home /
How to deal with Classes and Scriptables objects ?
Hi everyone, I'm a game design student, currently trying to make a clone of nuclear throne to improve my skill in programming.
I'm currently struggling with an architecture problem. I want to use scriptable objects to easily create and modify weapons. My goal is for now to have a weapon scriptable object class, where i can manage every parameters i could possibly need to define a weapon. One of thoses parameters is of course the type of weapon, wich include for now, melee, hitscan and projectiles weapons.
I've tried several architectures, and the closest solution i found was to create a weapon class, wich refer to any kind of weapon, and a child class wich refers to the type of weapon that has been defined in the scriptable object (for instance, a melee class that inherit from the weapon class). But this lead to some confusing code because i don't know exactly where to put the function wich detect the type of weapon and create a construct with the correct type of weapon class.
For now, i tried to create a system based on this :
I'm currently struggling trying to recreate that system and have several questions :
- Can i change in game of scriptable object in my inventory and set a new construct of weapon ?
Can i avoid always specifying wich of type of weapon i refer to ? I don't want that everytime i hit attack button, code has to find what is the current type of weapon and call the appropriate function.
I'm pretty sure there's a way better and elegant solution to thoses kind of problematics but for now, i lack of imagination and probably knowledge ! And i don't really know how to address my needs on internet. Anyway, thanks to everyone who took the time to read me and have a nice day !
Answer by Bunny83 · Jan 20, 2018 at 06:07 PM
I don't really understand your first question. The sentence seems a bit messed up to me ^^. To answer your second question, yes you can and should avoid this. The actual strength of inheritance is not that you can reuse existing / common properties but that you can actually abstract the weapon itself. The key is to define a virtual or abstract method in your weapon base class and have each weapon subclass override and implement their own behaviour. You just have to make sure the method signature gets everything you will ever need.
ScriptableObjects are not associated with any actual gameobject. However when you actually use your weapon you need some kind of gameobject which you use as anchor / spawnpoint. Your Weapon base class could define a method like this:
public abstract bool Fire(Transform aWeaponGO);
Now in a derived class you would override this method and implement the weapon specific behaviour. So a hitscan weapon would do something like
public override bool Fire(Transform aWeaponGO)
{
if (ammo > 0)
{
ammo--;
RaycastHit hit;
if (Physics.Raycast(aWeaponGO.position, aWeaponGO.forward, out hit))
{
// check if you hit something that can take damage and apply damage
}
return true; // signal that you successfully fired a shot
}
else
return false;
}
A projectile weapon would do something like
public override bool Fire(Transform aWeaponGO)
{
if (ammo > 0)
{
ammo--;
Rigidbody projectile = Instantiate(projectilePrefab, aWeaponGO.position, aWeaponGO.rotation);
projectile.AddForce(aWeaponGO.forward * projectileSpeed, ForceMode.Impulse);
return true; // signal that you successfully fired a shot
}
else
return false;
}
Of course you usually want to implement some sort of cooldown / fire delay.
Now you can simply declare a variable of type Weapon (your base class) and assign any of the derived cclasses. When you call Fire on that variable you will actually execute the class specific code even though you don't have to know what type of weapon you're using.
public Weapon weapon;
//
weapon.Fire(characterHandTransform);
Thank you for your amazing answer ! Indeed, as i was a bit confused, my questions weren't crystal clear. I know what to look for now, and that's great. Have a nice day ! :)
Answer by UnityCoach · Jan 20, 2018 at 06:12 PM
You probably want to deal with abstraction (inheritance and polymorphism) so that you can call "Attack" on any Weapon, and each Weapon handle the "Attack" in its own way.
Here are two courses I touch on the subject with :
using the model-view-controller design pattern with Unity (includes ScriptableObject)
SOLID Design principles with Unity
And a free video on my Youtube page touching on abstraction
Hope this helps.
@UnityCoach I don't want to mean any disrespect regarding your suggestion for using the model-view-controller design pattern, but I$$anonymous$$HO this is a very good example for using the Strategy design pattern, which is the basis of @Bunny83 answer.
No offense taken. I didn't mean to advise using the $$anonymous$$VC pattern here. I simply said I touch on ScriptableObjects in this course, and a lot on abstraction in the following chapter touching on SOLID principles. I totally agree with you and @Bunny83 on using abstraction.
Your answer
![](https://koobas.hobune.stream/wayback/20220612150918im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is it ok to use nested classes, to cache and access components? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Effects architecture akin to Starcraft 2 0 Answers
How to Find gameObject child. 3 Answers