Plug and Play Weapons?

So, I’m having a bit of a knowledge crisis. Being a programmer, and having done a couple games “the hard way”, I know how to make generic weapons and subclass them for specifics. I’m sitting here now wondering how do I make the equivalent of “Weapon” and then subclasses for “Shotgun” and “Baseball Bat” which I’d just use from a “fire()” command?

Maybe I’m just over thinking it or something, but I didn’t want to make the game limited to what I coded in. If later I wanted to add more weapons, it should be as simple as adding it to the level the players run around in, rather than modifying half a dozen files to shoehorn it in.

First, you say you’re a programmer and therefore know OOP and therefore how in C# to define an interface by creating an abstract “Weapon” class with abstract methods such as “fire()”, and then to create derived classes such as “Shotgun” and “Baseball Bat” which actually implement the desired behaviors.

Second, you say you’re having to modify half a dozen source files when you add a new weapon, which the above shouldn’t require at all. Heck, that’s the whole point of an abstract base class! :slight_smile:

I can’t quite resolve your two statements. What problems precisely are you facing?

No no, I’m saying I don’t want to do that. I wasn’t aware though that the concept of subclassing still applied to scripts spread across multiple objects. That, I suppose, is the point I was stuck on.

So I am able to make a Weapon class, and then make a Shotgun subclass, put that script into a shotgun mesh, and it’ll work like it’s suppose to then? Sometimes Unity throws me curve balls as to what does and doesn’t work.

Exactly. Unity3 understands abstract classes. It’s even smart enough to know that abstract base classes cannot be attached to GameObjects directly but derived subclasses can be. :slight_smile:

Nice. I’m trying to figure out the nuances of the system. I’m finding out things as I go, like Raycast and Hit and making a 3rd person camera, but sometimes I want to ask a dozen questions to make life go faster. Knowing that the script inheritence is smart solves a few problems for me.