Duck Game
60 oy
Making your own gun!
Killer-Fackur tarafından
In this guide i will be showing how to make a simple weapon for DuckGame.
I will not show how to make a mod and you'll have to read Parils Modding guide to make the mod that we'll be making the weapon for.

You'll need:
basic c# knowledge
visual studio

recommend:
JetBarins DotPeek or ILSpy. (to see how other weapons were made)

   
Ödül
Favorilere Ekle
Favorilere Eklendi
Favorilerden Çıkar
Making the gun
Adding the weapon class
First we'll need to make a class for our new weapon. to do so rightclick on the src folder and choose add then class.










Then you'll have to give the class a name. this time i'll use ExampleGun












You'll need to use DuckGame classes, to do so add: "using DuckGame"










now we can actuallly start coding,
first add
[editorGroup("ExampleMod")]
above you class (not namespace)
ExampleMod can always be replaced with anything you want. It can also be in a Existing EditorGroup like guns.

then extend Gun to your class and add
public YourClassName(float xval,float yval):base(xval,yval){ }

I'll be using a texture from my own mod MISC WEAPONS, wich is just a little edit on the origional netGun.










Download[www.dropbox.com]
The textures need to be in the content folder, dont put it in the src folder.


Since its the same as the NetGun we can be lazy and use its barrelOffset and collisionsize etc.












Copy it into public YourClassName(float xval,float yval){} and add:
this.ammo = 4; //gives the gun 4 shots this.graphic = new Sprite(GetPath("YourTextureFileWithoutExtension")); //GetPath() gives you the path to the content folder, the string is the name of the asset.

and since it will shoot crates and not bullets we need to remove Fire() wich spawn the bullet.
do it with this code below YourClassName(float xval,float yval).
public override void Fire() } }
Dont worry, its supposed to be empty.

Now your code should look kinda like this:













if you start duckgame you'll find your editorgroup in the leveleditor and it contains your gun.

Making It Shoot
Now we'll make the gun shoot, well make it happen in the override OnPressAction().

First well need to set it up, just add the code:
public override void OnPressAction(){ base.OnPressAction(); }
this will get called when a duck presses the fire button and holding your gun.

inside it well first need to check if it have ammo, if it does it will remove one.

if(this.ammo > 0){ this.ammo --; }

Now we need to check if its server side so that it wont create two crates online.
if(this.ammo > 0){ this.ammo --; if(isServerForObject){ } }
Next we'll create the crate instance and set its position with the barrelOffset.
if(this.ammo > 0){ this.ammo --; if(isServerForObject){ Crate crate = new Crate(0,0); crate.position = Offset(this._barrelOffsetTL); } }
if we now save and start duckgame you'll see that it wont spawn any crates, we need to add it to the level with:
Level.Add((Thing)crate);

Now start the game and we can spawn 4 crates!

giving the crates speed
Now were going to give the crate some speed, luckely theres already a variable that returns diffrence from the duck and the barrel, this will make it very easy to give it its speed.

if(this.ammo > 0){ this.ammo --; if(isServerForObject){ Crate crate = new Crate(0,0); crate.position = Offset(this._barrelOffsetTL); crate.vSpeed = this.barrelVector.y; crate.hSpeed = this.barrelVector.x; Level.Add((Thing)crate); } }
if you now start duckgame you'll notice that the crates arent moving very fast, so well just multiply the speed by 7.

if(this.ammo > 0){ this.ammo --; if(isServerForObject){ Crate crate = new Crate(0,0); crate.position = Offset(this._barrelOffsetTL); crate.vSpeed = this.barrelVector.x * 7f; crate.hSpeed = this.barrelVector.y * 7f; Level.Add((Thing)crate); } }

After that everything should look a bit like this:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DuckGame; namespace MyMod.src { [EditorGroup("ExampleMod")] class ExampleGun : Gun { public ExampleGun(float xval,float yval) : base(xval, yval) { this.graphic = new Sprite(GetPath("crateGun")); this.ammo = 4; this.center = new Vec2(16f, 16f); this.collisionOffset = new Vec2(-8f, -4f); this.collisionSize = new Vec2(16f, 9f); this._barrelOffsetTL = new Vec2(27f, 14f); } public override void Fire() { } public override void OnPressAction() { if (this.ammo > 0) { this.ammo--; if (this.isServerForObject) { Crate crate = new Crate(0, 0); crate.position = Offset(this._barrelOffsetTL); crate.hSpeed = this.barrelVector.x * 7f; crate.vSpeed = this.barrelVector.y * 7f; Level.Add((Thing)crate); } } base.OnPressAction(); } } }
64 Yorum
govnovoz karaoke 7 Haz 2021 @ 14:11 
@lukethekillrr The mod should be placed in %USERPROFILE%\AppData\Roaming\DuckGame\Mods.
You can download the mod template from this guide: https://steamcommunity.com/sharedfiles/filedetails/?id=484818341 .
You need to put the .png into the template's content folder.
I recommend the dnSpy for reading the source code since it's a bit more capable than ILSpy. In dnSpy you can just open the DuckGame.exe and see all the classes. I'm currently also learning to mod the game, but if you need any help i can send the piss gun mod's source code that i made.
coolhandluke 14 Nis 2020 @ 16:30 
what content folder do i put it in?
coolhandluke 14 Nis 2020 @ 12:57 
pls help i cant see the texture
coolhandluke 13 Nis 2020 @ 20:15 
how do i see how other weapons were made?
doormatt 25 Nis 2019 @ 13:05 
{
Crate crate = new Crate(0, 0);
crate.position = Offset(this._barrelOffsetTL);
crate.hSpeed = this.barrelVector.x * 7f;
crate.vSpeed = this.barrelVector.y * 7f;
Level.Add((Thing)crate);
}
}
base.OnPressAction();
}
}
}
doormatt 25 Nis 2019 @ 13:05 
{
public CrateGun(float xval,float yval) : base(xval, yval)
{
this.graphic = new Sprite(GetPath("crateGun"));
this.ammo = 4;
this.center = new Vec2(16f, 16f);
this.collisionOffset = new Vec2(-8f, -4f);
this.collisionSize = new Vec2(16f, 9f);
this._barrelOffsetTL = new Vec2(27f, 14f);
}

public override void Fire()
{
}

public override void OnPressAction()
{
if (this.ammo > 0)
{
this.ammo--;
if (this.isServerForObject)
doormatt 25 Nis 2019 @ 13:05 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DuckGame;

namespace FS_Items_Mod.src
{
[EditorGroup("testing")]
class CrateGun : Gun
doormatt 25 Nis 2019 @ 13:04 
oops
didn't copy the whole thing
doormatt 25 Nis 2019 @ 13:04 
public override void Fire()
{
}

public override void OnPressAction()
{
if (this.ammo > 0)
{
this.ammo--;
if (this.isServerForObject)
{
Crate crate = new Crate(0, 0);
crate.position = Offset(this._barrelOffsetTL);
crate.hSpeed = this.barrelVector.x * 7f;
crate.vSpeed = this.barrelVector.y * 7f;
Level.Add((Thing)crate);
}
}
base.OnPressAction();
}
}
}
doormatt 25 Nis 2019 @ 13:03 
this is what my code looks like :using System.Text;
using DuckGame;

namespace FS_Items_Mod.src
{
[EditorGroup("testing")]
class CrateGun : Gun
{
public CrateGun(float xval,float yval) : base(xval, yval)
{
this.graphic = new Sprite(GetPath("crateGun"));
this.ammo = 4;
this.center = new Vec2(16f, 16f);
this.collisionOffset = new Vec2(-8f, -4f);
this.collisionSize = new Vec2(16f, 9f);
this._barrelOffsetTL = new Vec2(27f, 14f);
}