Duck Game

Duck Game

View Stats:
꧁Kritzy꧂ 29 Mar, 2018 @ 7:48am
Flare gun code not working. halp.
Hey, i'm trying to make a flare gun for Duck game, but the code gives an error...

using DuckGame; namespace MyMod.src { [EditorGroup("guns|shotguns")] class FlareShot : Gun { public FlareShot(float xval, float yval) : base(xval, yval) { this.ammo = 6; this.graphic = new Sprite(GetPath("textures/FlareShot")); this.center = new Vec2(16f, 16f); this.collisionOffset = new Vec2(-8f, -4f); this.collisionSize = new Vec2(16f, 9f); this._barrelOffsetTL = new Vec2(27f, 14f); this._ammoType = new ATShotgun(); this._fireSound = GetPath("sounds/FlareShot_shoot.wav"); this._fireWait = 5; this._numBulletsPerFire = 5; this._ammoType.accuracy = 1.5f; Level.Add(SmallSmoke.New(x + Rando.Float(-8f, 8f), y + Rando.Float(-8f, 8f))); } public override void OnPressAction() { if (this.ammo > 0) { --this.ammo this.ApplyKick(); if (this.receivingPress || !this.isServerForObject) return; Vec2 vec2 = this.Offset(this.barrelOffset); Flare flare = new Flare(vec2.x, vec2.y, this, 8); //Here is the error this.Fondle((Thing)flare); Vec2 vec = Maths.AngleToVec(this.barrelAngle + Rando.Float(-0.2f, 0.2f)); flare.hSpeed = vec.x * 14f; flare.vSpeed = vec.y * 14f; Level.Add((Thing)flare); } } } }

The error it's giving me:
"Argument 3: Cannot convert from MyMod.src.FlareShot to DuckGame.FlareGun"

(Also, yes. I copied the code from Duck Game)


What's wrong with it, and how do i fix it?
Originally posted by Killer-Fackur:
Constructor for the Flare class:
public Flare(float xpos, float ypos, FlareGun owner, int numFlames = 8)

The parameter owner expects a variable that can be interperated as a FlareGun, so anything that extends or is a FlareGun will work.
this refers to the current instance, and if used in the FlareGun class it will be a FlareGun, and when you use this in your FlareShot class it will be a FlareShot.

so to fix it you have to pass a value that can be Interperated as a FlareGun.

Since the owner variable isnt actually used anywhere in the flare you can set it to the default value (i.e null), this would crash the game in most instances but not now since its not used for some reason.

Flare flare = new Flare(vec2.x, vec2.y, null, 8);

Another way of doing it is to make this convertable to a FlareGun. To do so you just extend your FlareShot to FlareGun.
class FlareShot : FlareGun

I recommend reading/watching some c# tutorials as it will help greatly in the long run.

http://csharp.net-tutorials.com/classes/introduction/ --this explains how class inheritance and object orientation work so that you can get a more in depth knowledge of why you got the error
< >
Showing 1-5 of 5 comments
The author of this thread has indicated that this post answers the original topic.
Killer-Fackur 2 Apr, 2018 @ 9:47am 
Constructor for the Flare class:
public Flare(float xpos, float ypos, FlareGun owner, int numFlames = 8)

The parameter owner expects a variable that can be interperated as a FlareGun, so anything that extends or is a FlareGun will work.
this refers to the current instance, and if used in the FlareGun class it will be a FlareGun, and when you use this in your FlareShot class it will be a FlareShot.

so to fix it you have to pass a value that can be Interperated as a FlareGun.

Since the owner variable isnt actually used anywhere in the flare you can set it to the default value (i.e null), this would crash the game in most instances but not now since its not used for some reason.

Flare flare = new Flare(vec2.x, vec2.y, null, 8);

Another way of doing it is to make this convertable to a FlareGun. To do so you just extend your FlareShot to FlareGun.
class FlareShot : FlareGun

I recommend reading/watching some c# tutorials as it will help greatly in the long run.

http://csharp.net-tutorials.com/classes/introduction/ --this explains how class inheritance and object orientation work so that you can get a more in depth knowledge of why you got the error
꧁Kritzy꧂ 2 Apr, 2018 @ 1:52pm 
Originally posted by Killer-Fackur:
Constructor for the Flare class:
public Flare(float xpos, float ypos, FlareGun owner, int numFlames = 8)

The parameter owner expects a variable that can be interperated as a FlareGun, so anything that extends or is a FlareGun will work.
this refers to the current instance, and if used in the FlareGun class it will be a FlareGun, and when you use this in your FlareShot class it will be a FlareShot.

so to fix it you have to pass a value that can be Interperated as a FlareGun.

Since the owner variable isnt actually used anywhere in the flare you can set it to the default value (i.e null), this would crash the game in most instances but not now since its not used for some reason.

Flare flare = new Flare(vec2.x, vec2.y, null, 8);

Another way of doing it is to make this convertable to a FlareGun. To do so you just extend your FlareShot to FlareGun.
class FlareShot : FlareGun

I recommend reading/watching some c# tutorials as it will help greatly in the long run.

http://csharp.net-tutorials.com/classes/introduction/ --this explains how class inheritance and object orientation work so that you can get a more in depth knowledge of why you got the error
Ok, thanks for the help! I'll definatly be checking that out. :senpai:
꧁Kritzy꧂ 3 Apr, 2018 @ 7:45am 
Cool, it worked!

Now there is only one last question i have:

Since we're using OnPressAction() to fire the flare, the "this._fireWait" doesn't apply.
Is there a way to add a cooldown to the gun? Or even better, can you add the reload animation, from the sniper or Old Pistol? _wait doesnt work sadly.?
Killer-Fackur 3 Apr, 2018 @ 8:01am 
My approach: Have a variable that decrease every frame, and make it so that the gun only can fire when its below zero and then reset it back to the start number.

pseudo code (try coding it for yourself before you copy/paste, trial and error is good for you):

private float fireCooldown = 0; //variable used for cooldown public override void Update(){ fireCooldown--; base.Update(); } public override void OnPressAction() { if (this.ammo > 0 && fireCooldown < 0){ fireCooldown = _fireWait; //this makes it so that fireWait is used. .... //rest of the code
꧁Kritzy꧂ 3 Apr, 2018 @ 9:36am 
Originally posted by Killer-Fackur:
My approach: Have a variable that decrease every frame, and make it so that the gun only can fire when its below zero and then reset it back to the start number.

pseudo code (try coding it for yourself before you copy/paste, trial and error is good for you):

private float fireCooldown = 0; //variable used for cooldown public override void Update(){ fireCooldown--; base.Update(); } public override void OnPressAction() { if (this.ammo > 0 && fireCooldown < 0){ fireCooldown = _fireWait; //this makes it so that fireWait is used. .... //rest of the code
Got it, but i did it a bit different.

if (this.ammo > 0 && fireCooldown < 0) { --this.ammo; this.ApplyKick(); if (this.receivingPress || !this.isServerForObject) return; SFX.Play(GetPath("sounds/Detonator_shoot"), 1f, 0f, 0f, false); Vec2 vec2 = this.Offset(this.barrelOffset); Flare FlareTF = new Flare(vec2.x, vec2.y, this, 8); //Here is the error this.Fondle((Thing)FlareTF); Vec2 vec = Maths.AngleToVec(this.barrelAngle + Rando.Float(-0.2f, 0.2f)); FlareTF.hSpeed = vec.x * 14f; FlareTF.vSpeed = vec.y * 14f; Level.Add((Thing)FlareTF); fireCooldown = 50; } else if (this.ammo > 0 && fireCooldown > 0) { this.DoAmmoClick(); } else { this.DoAmmoClick(); }

Instead of making it the same as _fireWait, i made a separate number and it worked out nicely.

Thanks for all the help :senpai:
< >
Showing 1-5 of 5 comments
Per page: 1530 50