0 votes

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!

in Engine by (21 points)

2 Answers

0 votes

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

by (740 points)
0 votes

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 - ...
by (148 points)

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?

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.