Best Way to Implement an Object Oriented Approach to a Weapon System?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BiLLz

Hey all!

So I literally downloaded Godot yesterday and have been trying to learn it. I have a strong background in C/C++ and Java, so I have been using C# as my scripting language of choice for Godot.

I am trying to implement a system where a player can pick up various guns, each with different damage and fire rate properties. In my mind and using an OOP approach, I would want to approach this by having a weapon class, then have each gun extend that class, each with their own individual properties, and have each weapon be its own class, thus they have their own properties.

My problem is that I do not know how to implement this system in Godot. I realize that this is quite open-ended, but I am just looking for someone that can point me in the right direction of how to implement a system like this.

(in the future, I wish to add multiple players each using a different gamepad, so I don’t know if this info will affect your answer)

Thank you for the help!

:bust_in_silhouette: Reply From: supper_raptor

Make a base scene gun. Add properties like gun_name, gun_type, recoil , damage, mag size, rate of fire etc.
Then add nodes like Sprite, AudioStreamPlayer2D (for gun sound) etc.
Then code firing mechanism.

If you want to add lets say AK47
create a new node and instance gun
set gun_name = "AK47", gun_type = "rifle", set recoil and other things.
Then set sprite and audio

:bust_in_silhouette: Reply From: hammeron

Scenes are inheritable in Godot. Create a base Weapon.tscn scene with common nodes like collision detection, sprites, sound, etc; and Weapon.gd script attached. Right-click on the scene file, choose “New Inherit Scene”, change the root node name for the weapon you want. You can then change any node parameters, add other nodes or extend Weapon.gd (right-click on root node, choose “Extend Script”.

I use this method to create complex hierarchies, for example:

# scene with mesh, animation clips, sounds
WarriorAssets
MageAssets
SlimeAsset
SkeletonAsset

# Hierarchy
Character - collision detection setup, movement functions, and other helpers functions.
    Player - calls movement functions based on player's input
        Warrior - specific configurations/artwork, add WarriorAssets as child node
        Mage - specific configurations/artwork, add MageAssets as child node
    Enemies - calls movement functions based on AI
        Slime - add SlimeAsset as child node
            Red Slime - use RedSlimeMaterial
            Blue Slime - use BlueSlimeMaterial
        Skeleton - add SkeletonAsset as child node
        BanditWarrior - add WarriorAssets as child node
        BanditMage - add MageAssets as child node
    NPC - player interaction and calls movement functions based on AI
        PetSlime - add SlimeAsset as child node
        CitySkeleton - add SkeletonAsset as child node
        CommonFolk - ...
        Shopkeeper - ...
            InnReceptionist - ...
            PotionMerchant - ...
            WeaponMerchant - ...
        Guards - ...

Ok, sorry for the late response, I’ve been busy with school. Here is what I have so far:

I have a weapon scene with all the base variables, physics, and firing functions coded into it. I then made an inherited-scene called “Glock.” The question I now have is, how do I change the properties of each individual weapon, such as “damage” or something like that?

BiLLz | 2020-04-29 00:21

Use export vars to expose parameters in the editor. Then change them in the child scene.

hammeron | 2020-05-04 22:26