crash on the mouseitem

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

crash on the mouseitem

Post by bongobeat »

hello all,

I got a crash when breaking a breakable object and wonder if someone can help about that:
I was holding an item on the mouse which use a special script while tryin to break the object, here is the screen error:
SpoilerShow
Image
I guess this is because the kind of item has some conditions when used and equipped in both hands. and I suppose that I need to add a condition for the mouse item things. Can anybody help about that?

this is the script used by the item:
SpoilerShow

Code: Select all

		onAttack = function(self, champion, slot, chainIndex)
----
       local ammo1 = champion:getItem(1)
       local ammo2 = champion:getItem(2)
	if ammo1 and ammo2 then
       if ammo1.go.name == "ion_gun" and ammo2.go.name == "power_recharge"
       or ammo2.go.name == "ion_gun" and ammo1.go.name == "power_recharge" then
          ----
          local ion = spawn("ion_object",party.level,party.x,party.y,party.facing,party.elevation)
          ion.projectile:setIgnoreEntity(party)

          -- Set cast by champion so that experience is awarded for kills.
          local ord = champion:getOrdinal()
           ion.projectile:setCastByChampion(ord)

          -- Simulate the position of a player-cast spell.
          local left = nil
          for i = 1,4 do
             if party.party:getChampion(i):getOrdinal() == ord then
                left = i == 1 or i == 3
                break
             end
          end
          local wpos = party:getWorldPosition()
          local dx = nil
          local dz = nil
          if party.facing == 0 then
             dx = left and -0.1 or 0.1
             dz = -1
          elseif party.facing == 1 then
             dz = left and 0.1 or -0.1
             dx = -1
          elseif party.facing == 2 then
             dx = left and 0.1 or -0.1
             dz = 1
          else -- party.facing == 3
             dz = left and -0.1 or 0.1
             dx = 1
          end

          ion:setWorldPosition(vec(wpos[1]+dx,wpos[2]+1.35,wpos[3]+dz))
       end
    end
end
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: crash on the mouseitem

Post by minmay »

Why are you posting an onAttack hook instead of the onComputeCooldown hook that the error actually occurred in? Nobody can help you if you don't post the onComputeCooldown hook.
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.
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: crash on the mouseitem

Post by bongobeat »

sorry I didnt thought about the onComputeCooldown, since I thought that this error comes from the items definition:


here is the onComputeCooldown I use in a trait:

Code: Select all

defineSkill{
	name = "faster",
	uiName = "Toorum's Disciple",
	priority = 190,
	icon = 108,
	description = "As a fighter you know that speed and dexterity are crucial. You are following Toorum's teachings to learn an incredible speed. Each level spent increase your dexterity by 1. At 3rd level your cooldown are decreased by 25%. At 5th level you learn the thunderstruck trait.",
	onRecomputeStats = function(champion, level)
		if level > 0 then
		champion:addStatModifier("dexterity", 1)
		end
	end,
	traits = { [3] = "faster3", [5] = "faster5" },
}

defineTrait{
	name = "faster3",
	uiName = "Quick as the wind",
	icon = 108,
	description = "Your are teached into the Rite of the Wind.  Your cooldown is decreased by 25%.",
	onComputeCooldown = function(champion, weapon, attack, attackType, level)
		if level > 0 then return 0.8 end
	end,
}

defineTrait{
	name = "faster5",
	uiName = "Thunderstruck",
	icon = 108,
	description = "Your have reached the grandmastery and gain the Thunderstruck trait. All cooldowns are halved.",
	onComputeCooldown = function(champion, weapon, attack, attackType, level)
		if level > 0 then return 0.5 end
	end,
}
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: crash on the mouseitem

Post by minmay »

Those are not the relevant onComputeCooldown hooks because they never try to index a variable called "weapon"...please post ALL your onComputeCooldown hooks in mod_assets/scripts/traits.lua. Or, better, post the one that includes line 149.
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.
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: crash on the mouseitem

Post by bongobeat »

aargh!
I forgot this one! :roll:

ok, here is the whole trait, the line 149 starts at "icon = 43," :
SpoilerShow

Code: Select all

defineTrait{
	name = "mweapon_mastery",
	uiName = "Ancient Weapons Master",
	icon = 43,
	description = "As a master in ancient weapons, you know how to recharge them faster. All attacks with that type of weapon are 25% faster.",
    onComputeCooldown = function(champion, weapon, attack, attackType, level)
		if level > 0 then
          if weapon:hasTrait("modern") or weapon:hasTrait("modern") then return 0.8 end
	end
end,

}
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: crash on the mouseitem

Post by Isaac »

It should probably be written: weapon.item:hasTrait("modern")

Code: Select all

if weapon.item:hasTrait("modern") or weapon.item:hasTrait("modern") then return 0.8 end
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: crash on the mouseitem

Post by minmay »

No, that's not the issue, weapon is always either an ItemComponent or nil. The issue is that you are not checking whether weapon is nil; you'll get a crash with unarmed attacks. It should be:

Code: Select all

onComputeCooldown = function(champion, weapon, attack, attackType, level)
  if weapon and level > 0 then
    if weapon:hasTrait("modern") then return 0.8 end
  end
end,
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.
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: crash on the mouseitem

Post by bongobeat »

thanks a lot, works very well :D
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
Post Reply