Checking contents of champions' inventory

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
Arodin
Posts: 5
Joined: Wed Apr 18, 2012 6:13 pm

Checking contents of champions' inventory

Post 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.
Zanzer
Posts: 8
Joined: Sun Nov 02, 2014 4:38 pm

Re: Checking contents of champions' inventory

Post by Zanzer »

You need to use getUiName()

Code: Select all

party.party:getChampion(1):getItem(13):getUiName()
User avatar
Arodin
Posts: 5
Joined: Wed Apr 18, 2012 6:13 pm

Re: Checking contents of champions' inventory

Post by Arodin »

Hmm. That is giving me an error "attempt to index a nil value"
Zanzer
Posts: 8
Joined: Sun Nov 02, 2014 4:38 pm

Re: Checking contents of champions' inventory

Post by Zanzer »

You must first check that getItem(#) returned a non-nil value (because the slot was empty).
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Checking contents of champions' inventory

Post 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.
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
Zanzer
Posts: 8
Joined: Sun Nov 02, 2014 4:38 pm

Re: Checking contents of champions' inventory

Post by Zanzer »

Out of curiosity, how did you find out that item has the "go" property and "go" has the "name" property?
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Checking contents of champions' inventory

Post 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.
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
Post Reply