Page 1 of 1

How do you code a breakable weapon?

Posted: Sat Feb 07, 2015 12:15 pm
by WaspUK1966
I`m trying to define a weapon on LOG1 that will break after a few uses (a wooden spear or similar). Ive tried cloning the legionary spear and messed around with the onDealDamage code, adding it to the item code itself and tried using the 'return true' function, but cant get anything to work. Any ideas greatly appreciated:

Code: Select all

defineObject{
		name = "rusty_spear",
		class = "Item",
		uiName = "Old, rusty spear",
		model = "assets/models/items/skeleton_spear.fbx",
		gfxIndex = 208,
		attackPower = 10,
		accuracy = 0,
		coolDownTime = 2,
		attackMethod = "meleeAttack",
		attackSwipe = "thrust",
		attackSound = "swipe",
		impactSound = "impact_blade",
		weight = 3.0,
		reachWeapon = true,
		onDealDamage = function(self, monster, damage)
			if math.random() <= 0.35 then
			hudPrint("The spear has broken!")
			return true
			end
			
			end,

		
	}
The above is just a test code. The spear works as normal, but wont break.

Thanks
George

Re: How do you code a breakable weapon?

Posted: Sat Feb 07, 2015 12:26 pm
by minmay
onDealDamage is a hook for the Monster class. There is no such hook (or any equivalent one) for the Item class.

You would need to use Party.onAttack and/or Monster.onDamage.

Re: How do you code a breakable weapon?

Posted: Sat Feb 07, 2015 1:27 pm
by WaspUK1966
Thanks for that. Have you got a working code that uses the onAttack function. Still cant get it to work! Sorry..
George

Re: How do you code a breakable weapon?

Posted: Tue Feb 24, 2015 9:26 pm
by Zo Kath Ra
WaspUK1966 wrote:Thanks for that. Have you got a working code that uses the onAttack function. Still cant get it to work! Sorry..
George
This is what I've come up with:

Code: Select all

defineObject{
    name = "spear_breakable",
    baseObject = "base_item",
    components = {
		{
			class = "Model",
			model = "assets/models/items/skeleton_spear.fbx",
		},
		{
			class = "Item",
			uiName = "Spear",
			gfxIndex = 208,
			impactSound = "impact_blade",
			weight = 3.0,
			fitContainer = false,
			traits = { "light_weapon", "two_handed", "spear", "aquatic" },
		},
		{
			class = "MeleeAttack",
			attackPower = 10,
			accuracy = 0,
			cooldown = 5,
			swipe = "thrust",
			attackSound = "swipe",
			reachWeapon = true,
			onAttack = function(self, champion, action, slot)
			    if self.go.meleeattack:isEnabled() then
			        --hudPrint(self.go.item:getUiName().." attack")
			    else
			        hudPrint("The spear is broken, you cannot attack with it.")
			        return false
			    end
			end,
            onHitMonster = function(self, monster, side, dmg, champion)
                --hudPrint(self.go.item:getUiName().." hit monster")
                self.go.item:setUiName("Spear (broken)")
                hudPrint("The spear breaks!")
                self.go.meleeattack:disable()
            end,
            onPostAttack = function(self)
                --hudPrint(self.go.item:getUiName().." post attack")
            end,
		},
	},
	tags = { "weapon" },
}
When you hit a monster, the spear "breaks" and can't be used to attack anymore.
It doesn't break if you attack a wall or an obstacle.

What I don't understand is, self.go.meleeattack:disable() by itself doesn't work.
You can still attack with the spear even with the MeleeAttack component disabled.
That's why the "onAttack" function contains the "if self.go.meleeattack:isEnabled()" check.