How do you code a breakable weapon?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
WaspUK1966
Posts: 135
Joined: Sat Aug 09, 2014 9:17 am

How do you code a breakable weapon?

Post 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
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: How do you code a breakable weapon?

Post 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.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
WaspUK1966
Posts: 135
Joined: Sat Aug 09, 2014 9:17 am

Re: How do you code a breakable weapon?

Post by WaspUK1966 »

Thanks for that. Have you got a working code that uses the onAttack function. Still cant get it to work! Sorry..
George
User avatar
Zo Kath Ra
Posts: 944
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: How do you code a breakable weapon?

Post 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.
Post Reply