Using an alcove to "combine" two items...

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
apendrag0n3
Posts: 6
Joined: Wed Jan 25, 2017 5:48 am

Using an alcove to "combine" two items...

Post by apendrag0n3 »

I am still learning LUA and scripting/modding for GrimRock in general. Here's what I trying to do...

I have an alcove
I want 2 specific items to be placed in the alcove (item_1 and item_2) by the party
Once both items are in the alcove - I want to destroy those items and replace them with item_3

I am not sure what the best way to accomplish this is. Any suggestions/help would be appreciated!


Thanks in Advance!
User avatar
Isaac
Posts: 3172
Joined: Fri Mar 02, 2012 10:02 pm

Re: Using an alcove to "combine" two items...

Post by Isaac »

Updated (thanks minmay):
(it's trickier in LoG1) This requires that you paste this object definition script into one of the lua files found in your mod folder; usually into the objects.lua file.
*In Windows, this is usually found at: "[your documents folder]/almost human/legend of grimrock/dungeons/[name of your mod]/mod_assets/scripts/objects.lua"

When this is done, you need to reload the project, and replace your alcove with the appropriate alcove_combiner assets (that should appear in the editor's object list).

Code: Select all

for x = 1, 3 do
	local name = {"dungeon", "temple", "prison"}

	defineObject{
		--name = "dungeon_alcove_combiner",
		name = string.format("%s_alcove_combiner", name[x]),
		class = "Alcove",
		--model = string.format("assets/models/env/dungeon_wall_alcove.fbx",
		model = string.format("assets/models/env/%s_wall_alcove.fbx", name[x]),
		replacesWall = true,
		anchorPos = vec(0, 0.85, 0.2),
		targetPos = vec(0,1.3,0),
		targetSize = vec(0.6, 0.5, 0.6),
		placement = "wall",
		editorIcon = 8,
		onInsertItem = function(self, item)
						   local first_itemName  = "pitroot_bread"                  
						   local second_itemName = "dagger"
						   local prize = "blueberry_pie"
						   local soundEffect = "item_drop"  				--Defaults to item_drop, but can be changed to suit
						   if item.name == first_itemName or           		--excludes all other item names
						       item.name == second_itemName then
						      
						      for item2 in self:containedItems() do  		--having found one of the two names, it checks the surface for the other one

						         if item2.name ~= item.name then     		--ensures that it excludes duplicate items fed to the next check.  '~='  means "not equal"

						            if item2.name == first_itemName or    	--if it finds a match, it destroys it, and the other one, then adds the prize to the shelf
						               item2.name == second_itemName then
						               print(item.name, item2.name)
						                  setMouseItem()					--must clear mouseItem ~else it remains a bad object on the mouse.	
						                  item2:destroy()
						                  self:addItem(spawn(prize))
						                  playSound(soundEffect)			 --provide sound effect 
						                  					 		
						                  return false                       --must return false to cancel Insert, and MUST exit the loop at first run, or it will compare with destroyed item [==crash]
						            end
						            
						         end 
						      end
						   end
						return true   
	end
	}

end
SpoilerShow
Previous Grimrock2 code

Code: Select all

function combineItems(self, item)            
   local first_itemName  = "pitroot_bread"                  
   local second_itemName = "dagger"
   local prize = "blueberry_pie"
   
   if item.go.name == first_itemName or            --excludes all other item names
       item.go.name == second_itemName then
      
      for _,item2 in self.go.surface:contents() do   --having found one of the two names, it checks the surface for the other one

         if item2.go.name ~= item.go.name then      --ensures that it excludes duplicate items fed to the next check.  '~='  means "not equal"

            if item2.go.name == first_itemName or    --if it finds a match, it destroys it, and the other one, then adds the prize to the shelf
               item2.go.name == second_itemName then
                  item.go:destroy()
                  item2.go:destroy()
                  self.go.surface:addItem(spawn(prize).item)
                  return                        --MUST exit the loop at first run, or it will compare with destroyed item [==crash]
            end
            
         end 
      end
   end
end
Last edited by Isaac on Wed Jan 25, 2017 12:17 pm, edited 6 times in total.
minmay
Posts: 2768
Joined: Mon Sep 23, 2013 2:24 am

Re: Using an alcove to "combine" two items...

Post by minmay »

Isaac wrote:

Code: Select all

function combineItems(self, item)				
	local first_itemName  = "pitroot_bread"						
	local second_itemName = "dagger"
	local prize = "blueberry_pie"
	
	if item.go.name == first_itemName or				--excludes all other item names
		 item.go.name == second_itemName then
		
		for _,item2 in self.go.surface:contents() do	--having found one of the two names, it checks the surface for the other one

			if item2.go.name ~= item.go.name then		--ensures that it excludes duplicate items fed to the next check.  '~='  means "not equal"

				if item2.go.name == first_itemName or 	--if it finds a match, it destroys it, and the other one, then adds the prize to the shelf
					item2.go.name == second_itemName then
						item.go:destroy()
						item2.go:destroy()
						self.go.surface:addItem(spawn(prize).item)
						return								--MUST exit the loop at first run, or it will compare with destroyed item [==crash]
				end
				
			end 
		end
	end
end
This is Grimrock 2 code. You're in the Grimrock 1 subforum.
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.
apendrag0n3
Posts: 6
Joined: Wed Jan 25, 2017 5:48 am

Re: Using an alcove to "combine" two items...

Post by apendrag0n3 »

Thanks - will try this tonight when I get back home... :-)
apendrag0n3
Posts: 6
Joined: Wed Jan 25, 2017 5:48 am

Re: Using an alcove to "combine" two items...

Post by apendrag0n3 »

And the LoG1 script worked great! Thanks for the help! :D
Post Reply