help needed: triggering only with specific item in mouse

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
User avatar
THOM
Posts: 1281
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

help needed: triggering only with specific item in mouse

Post by THOM »

Well - again one of my strange posts...

I want a script to be started if geting up a specific object from a floortrigger - only if it's this object.

What I have tried is connecting the trigger "by item only" "on deactivate" with

Code: Select all

function checkOnUp1()
	if getMouseItem("torch") then
		counter_tree_1.counter:decrement()
	end
end
Sadly the script runs even it is any item in the mouse. What to do?
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: help needed: triggering only with specific item in mouse

Post by minmay »

getMouseItem() doesn't take any arguments. It returns the item held. What you wanted is:

Code: Select all

if getMouseItem() and getMouseItem().go.name == "torch" then
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
THOM
Posts: 1281
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: help needed: triggering only with specific item in mouse

Post by THOM »

ah - thanx.

sigh, scripting is not easy these days...

why do I need

Code: Select all

if getMouseItem() and getMouseItem().go.name == "torch" then
and not

Code: Select all

if getMouseItem().go.name == "torch" then
?
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: help needed: triggering only with specific item in mouse

Post by minmay »

Because if the player is not holding an item in the mouse, getMouseItem() will return nil. You can't index nil. You can only index tables.
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
Zo Kath Ra
Posts: 944
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: help needed: triggering only with specific item in mouse

Post by Zo Kath Ra »

Since we have to use getMouseItem().go.name instead of getMouseItem().name, I guess that means getMouseItem() returns a component. Not an entity like you would expect, and like it did in LoG1:
viewtopic.php?f=14&t=3472&p=35426&hilit ... tem#p35426

Which component does it return, and why doesn't it return the entity?
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: help needed: triggering only with specific item in mouse

Post by minmay »

It returns the ItemComponent held by the mouse. It does so because it's named "getMouseItem" and you'd expect a function named "get...item" to return an item.
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
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: help needed: triggering only with specific item in mouse

Post by Isaac »

THOM wrote: why do I need

Code: Select all

if getMouseItem() and getMouseItem().go.name == "torch" then
and not

Code: Select all

if getMouseItem().go.name == "torch" then
?
Just to further clarify what it does... getMouseItem() can return nil; if it does, then getMouseItem().go will cause a halt. By first checking for nil, the second check never happens unless getMouseItem() returned other than nil.
Post Reply