I need to implement a system into my game, and it requires some more advanced for of programming than I have dealt with before. I am hoping someone could explain to me how to create this system.
Here’s the final result: The game has different types of throwing weapons. Each type consumes a different amount of a universal ammo, and allows the player to throw a different projectile item. The player can only have only one type of throwing weapon at any given time.
So here’s what I’m getting stuck on: I need to program a system whereby the Player Character script can designate what type of throwing weapon the player has, and from that variable be able to access different values such as how much ammo to consume and what prefab to instantiate when called for.
Off the top of my head I could create an enumerator for the weapon type, have a variable saving that enum, and then go through a series of switches to determine the various effects of using this particular weapon. But that creates some very jumbled, ugly, and unprofessional code that only gets bigger and harder to keep track of the more types I create.
I know there is better way.
In my mind’s eye I can see some way to use a custom class for my variable. I simply create a class that designates what the variables and functions are, and then create extensions of that class that set those variables and functions.
Then in my player character’s script I can have a variable that saves what class it is using, and then calls variables from that class. Like I could say “WeaponType.AmmoCost” and it would grab the designated ammo cost from the weapon type that is currently set. Ideally I could call functions from that class too, so if one type uses a completely different method for how it handles throwing the weapon, like calling different animations or spawning multiple projectiles, that is still called from the same “WeaponType.ThrowWeapon()” command.
But I don’t know how to do this, neither do I know the correct terms, so I can’t even look it up.
I started making a WeaponType script and making some classes that extend that script with their own properties, but I don’t know how to change the default values of particular variables.
I also am unsure how I would declare the variable used in my player’s class. And I’ve never worked with scripts that are not attached to game objects, but intended to be accessed independently like I am thinking.