Page 1 of 1

Checking contents of champions' inventory

Posted: Mon Nov 03, 2014 1:58 am
by Arodin
Hello

I'm trying to make a script that checks if the champions have a particular item in inventory. There is a script for this in the "Useful scripts repository" thread for Grimrock 1, but it doesn't work in Grim 2 as is. Here's what I have so far:

First, I have the two functions from the post above.
To fix an error, I modified party:getChampion(i) to party.party:getChampion(i)

Code: Select all

function checkForItemParty(item)
	local i = 1
	repeat
		if checkForItem(party.party:getChampion(i), item) then
			return true
		end
		i = i + 1
	until i == 5
	return false
end

function checkForItem(champion, item)
	local i = 1
	repeat
		local itemObj = champion:getItem(i)
		if itemObj ~= nil then
			if itemObj.name == item then 
				return true
			end
		end
	i = i+1
	until i == 32
	return false
end
Then I want to check if the party is holding the skeleton figurine and if so set a variable to false, so I have:

Code: Select all

if checkForItemParty("figure_skeleton") then
      ok_to_pass = false
end
This all runs now without any error, but the function never returns true. I used some print statements, and found that itemObj.name is always "nil"
My guess is that champion.getItem() doesn't work in Grim 2 but I don't know how to fix it.

Re: Checking contents of champions' inventory

Posted: Mon Nov 03, 2014 3:35 am
by Zanzer
You need to use getUiName()

Code: Select all

party.party:getChampion(1):getItem(13):getUiName()

Re: Checking contents of champions' inventory

Posted: Mon Nov 03, 2014 3:42 am
by Arodin
Hmm. That is giving me an error "attempt to index a nil value"

Re: Checking contents of champions' inventory

Posted: Mon Nov 03, 2014 3:44 am
by Zanzer
You must first check that getItem(#) returned a non-nil value (because the slot was empty).

Re: Checking contents of champions' inventory

Posted: Mon Nov 03, 2014 10:08 am
by JohnWordsworth
There is a new method in the scripting reference that might do what you want without any funny stuff. "PartyComponent:isCarrying(string)". If I were you, I would try doing...

Code: Select all

if party.party:isCarrying("item_id") then ... end
If that doesn't do what you're after, then your script above looks close - but I see a couple of potential problems.

1. I assume that champion:getItem(..) actually returns the item component of the object. So, if you want to check it's name, you will probably have to do:

Code: Select all

if (itemObj ~= nil) and (itemObj.go.name == item) then
  return true
end
2. Where there is "until i == 32", you might need to change that number. I don't know exactly how many inventory slots there are in the new game, but I presume that the two "alternative" hand configuration slots are just slots there too. Anyway, that number should match the total number of slots for all of the character's inventory.

Re: Checking contents of champions' inventory

Posted: Tue Nov 04, 2014 5:41 am
by Zanzer
Out of curiosity, how did you find out that item has the "go" property and "go" has the "name" property?

Re: Checking contents of champions' inventory

Posted: Tue Nov 04, 2014 10:19 pm
by JohnWordsworth
@Zanzer: So, each entity (otherwise known as a game object) is basically a very light entity now with a big bunch of components (bits) that give the entity features. It's like a game entity is a PC case I guess, and inside it are all of the components that make it work.

When you have an entity, you can use the name of a component to get that component...

entity.light:doSomething()

When you have a component, you can get back to the parent entity, using the .go property (go = gameobject). So, you could theoretically write...

print(entity.light.go.light.go.light.go.name)

Because an entity has a light, which has a reference back to the parent entity, and so on. At the end of the day, this is equivalent to writing entity.name, but if your code doesn't know what the entity is, and has just been passed a "light" component, then you can find out the parent entity's name using light.go.name.