Can Somebody Help Me With Search Inventory Script? (SOLVED)

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
wagtunes
Posts: 316
Joined: Sun Feb 04, 2018 11:04 pm

Can Somebody Help Me With Search Inventory Script? (SOLVED)

Post by wagtunes »

I'm up to level 9 of my dungeon. I have created some very complex scripts to this point and all work fine. I've been programming computers for over 30 years.

I cannot get this to work no matter what I do. I suspect a couple of things.

1) The script, which I have tied to stepping on a pressure plate, activates as soon as I start the game in the editor. I'm not understanding why this is happening.

2) in adding a test print to the function, in order to see the actual values returned, I am getting null for all results. Even after I pick up the item, put it in inventory, and step on the plate. In fact, stepping on the plate does not trigger the script again. I do have it linked to the script in the editor. So the script executes upon startup, where obviously there is nothing yet in inventory, but never again.

So I suspect that the variable I am using, which was posted here in 2012, has somehow been changed. Thus, why I'm getting null. But again, because the script doesn't execute again after stepping on the pressure plate, I can't be sure.

Here is the modified script after copying from this forum and changing the relevant variables.
function checkForItemParty(item)
local i = 1
repeat
if checkForItem(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)
print "hello world2"
print(itemObj)
if itemObj ~= nil then
if itemObj.name == item
then return true
end
end
i = i+1
until i == 32
return false
end

if checkForItemParty("rock")
then temple_door_portcullis_1:open()

end
You'll notice I have two test prints. The first one is to make sure I'm actually printing the latest modification of the script as it's obvious the buffer doesn't clear from the test run unless you actually exit out of the dungeon editor. I know this because when removing the test prints and running again, the messages still show up on the screen, which means they have to be left over from the last run. So I increase Hello World from 1 to 2 to make sure it's running the current script.

Anyway, when I press the play button on the editor, as soon as the game starts, the script executes. I have no idea why it does this. Stepping on the pressure plate does not activate the script again.

I am totally lost. If anybody has any suggestions, I'm all ears. I have run out of things to try.
Last edited by wagtunes on Sun Feb 18, 2018 3:26 am, edited 1 time in total.
User avatar
Zo Kath Ra
Posts: 931
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: Can Somebody Help Me With Search Inventory Script?

Post by Zo Kath Ra »

The script executes when the game starts (not when you step on the pressure plate), because these lines are not inside a function definition:

Code: Select all

if checkForItemParty("rock") then
	temple_door_portcullis_1:open()
end
This code works if you connect the dungeon_pressure_plate with checkForRock()

Code: Select all

function checkForItemParty(item_name)
	for i = 1, 4 do
		if checkForItem(party:getChampion(i), item_name) then
			return true
		end
	end
	
	return false
end

function checkForItem(champion, item_name)
	for i = 1, 31 do
		local itemObj = champion:getItem(i)
		
		if itemObj and (itemObj.name == item_name) then
			return true
		end
	end
	
	return false
end

function checkForRock()
	if checkForItemParty("rock") then
		temple_door_portcullis_1:open()
	end
end
wagtunes
Posts: 316
Joined: Sun Feb 04, 2018 11:04 pm

Re: Can Somebody Help Me With Search Inventory Script?

Post by wagtunes »

Zo Kath Ra wrote:The script executes when the game starts (not when you step on the pressure plate), because these lines are not inside a function definition:

Code: Select all

if checkForItemParty("rock") then
	temple_door_portcullis_1:open()
end
This code works if you connect the dungeon_pressure_plate with checkForRock()

Code: Select all

function checkForItemParty(item_name)
	for i = 1, 4 do
		if checkForItem(party:getChampion(i), item_name) then
			return true
		end
	end
	
	return false
end

function checkForItem(champion, item_name)
	for i = 1, 31 do
		local itemObj = champion:getItem(i)
		
		if itemObj and (itemObj.name == item_name) then
			return true
		end
	end
	
	return false
end

function checkForRock()
	if checkForItemParty("rock") then
		temple_door_portcullis_1:open()
	end
end
Duh! OMG, now I know I'm just tired. I completely missed that.

Anyway, I found a better script that checks if the item is in a sack or box or something that I like a lot better.

Geez. Sometimes the answer is right in front of you and your stupid eyes just don't see it.

Thanks for pointing out my glaring omission. Sometimes you just need a fresh pair of eyes.

Two more levels to go and this dungeon will finally be finished.
User avatar
Zo Kath Ra
Posts: 931
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: Can Somebody Help Me With Search Inventory Script?

Post by Zo Kath Ra »

wagtunes wrote:Anyway, I found a better script that checks if the item is in a sack or box or something that I like a lot better.
Do you have a link to it?
wagtunes
Posts: 316
Joined: Sun Feb 04, 2018 11:04 pm

Re: Can Somebody Help Me With Search Inventory Script?

Post by wagtunes »

Zo Kath Ra wrote:
wagtunes wrote:Anyway, I found a better script that checks if the item is in a sack or box or something that I like a lot better.
Do you have a link to it?
I can't find the original thread but here's the script copied and pasted exactly as it appeared here. I just made the changes I needed for my specific situation.
function checkGoldOrb()
local goldOrb = false
local n = "golden_orb"

for i = 1, 4 do
for j = 1, 31 do
local item = party:getChampion(i):getItem(j)
if item then
if item.name == "sack" or
item.name == "mortar" or
item.name == "wooden_box" then
for x in item:containedItems() do
if x then
if x.name == "mortar" then
for y in x:containedItems() do
if y then if y.name == n then goldOrb = true end end
end end
if x.name == n then goldOrb = true end
end end end
if item.name == n then goldOrb = true end
end end end

if goldOrb then door1:open() else door2:open() end
end
User avatar
Zo Kath Ra
Posts: 931
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: Can Somebody Help Me With Search Inventory Script? (SOLV

Post by Zo Kath Ra »

Can you use the "Code" instead of the "Quote" button when you post LUA scripts?
"Code" preserves indentation, and you can select the code with a single click.
wagtunes
Posts: 316
Joined: Sun Feb 04, 2018 11:04 pm

Re: Can Somebody Help Me With Search Inventory Script? (SOLV

Post by wagtunes »

Zo Kath Ra wrote:Can you use the "Code" instead of the "Quote" button when you post LUA scripts?
"Code" preserves indentation, and you can select the code with a single click.
Better?

Code: Select all

function checkGoldOrb()
  local goldOrb = false
  local n = "golden_orb"

  for i = 1, 4 do
    for j = 1, 31 do
      local item = party:getChampion(i):getItem(j)
      if item then
         if item.name == "sack" or 
            item.name == "mortar" or 
            item.name == "wooden_box" then
            for x in item:containedItems() do
              if x then
                 if x.name == "mortar" then
                    for y in x:containedItems() do
                      if y then if y.name == n then goldOrb = true end end
                 end end
                 if x.name == n then goldOrb = true end
         end end end
         if item.name == n then goldOrb = true end
  end end end

  if goldOrb then door1:open() else door2:open() end
end
User avatar
Isaac
Posts: 3172
Joined: Fri Mar 02, 2012 10:02 pm

Re: Can Somebody Help Me With Search Inventory Script? (SOLV

Post by Isaac »

This was also answered here:

viewtopic.php?f=14&t=3099&p=116476#p116470
Post Reply