re - usable lock?

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
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

re - usable lock?

Post by bongobeat »

Hello,

is that possible to someone to script a lock / socket that can acccept multiple items (different items)?

Then, after an item is inserted, it reset the lock and the lock can be re used with the same item or another one.

(The idea is something similar to a floppy drive, where you put all disks in the device, in a lock in that case, to print journal log.)

I got the device, I got a basic lock, but ... I need a script, someone can help please?
SpoilerShow
Image
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
kelly1111
Posts: 349
Joined: Sun Jan 20, 2013 6:28 pm

Re: re - usable lock?

Post by kelly1111 »

What kind of dungeon are you making?
very interrested in your project !
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: re - usable lock?

Post by Isaac »

What you do, is add an onAcceptItem() hook to your [socket] "lock". In the hook, you can detect the inserted item, and cause specific results.

If you are making a holo-tape /disk interface, then check for item.go.id for specific floppy disks, (via table), and use the id as a table key (variable name) for the contents of the disk. You can use the item.go.name to detect floppies, and refuse all other items; and more than one.

example:

Code: Select all

			onAcceptItem = function(self, item)
				print(item.go.name,item.go.id)
				local disks = { ["blueberry_pie_1"]= "This pie has specs of dirt on it!",
								["blueberry_pie_2"]= "This pie has been nibbled at already!",
								["blueberry_pie_3"]= "This pie is rotten!", }
				if disks[item.go.id] then
					hudPrint(disks[item.go.id])
				end				
								
				return item.go.name == "blueberry_pie" and self.go.socket:count() == 0
			end
		},
*Unless you are planning something else ~not apparent from your description.

**Updated example.

Of course, you could change it to play cutscenes, or audio files instead, or as well as hudPrint text messages.
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: re - usable lock?

Post by minmay »

To make a lock reusable just re-enable it in its onActivate hook.

Code: Select all

{
	class = "Lock",
	onActivate = function(self)
		self:enable()
	end,
},
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: re - usable lock?

Post by Isaac »

minmay wrote:To make a lock reusable just re-enable it in its onActivate hook.

Code: Select all

{
	class = "Lock",
	onActivate = function(self)
		self:enable()
	end,
},
There is that too. :mrgreen:
(And that one eats keys; and it needs to be self.go.lock:enable())

I suppose this could be used with a counter, to ask for disk1, disk2, and disk3, for instance.
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: re - usable lock?

Post by minmay »

Isaac wrote:it needs to be self.go.lock:enable().
No it doesn't, what do you think self is in this hook?
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: re - usable lock?

Post by Isaac »

minmay wrote:
Isaac wrote:it needs to be self.go.lock:enable().
No it doesn't, what do you think self is in this hook?
Well... When I tried it as [I mistakenly thought] you typed it (assuming by default that it was surely the correct way; as usual)... It did not work, and the game crashed... but when I added .go. it did work; and still does.. :?
So I posted it.

Image
SpoilerShow
*But only after I posted (as usual), did I see that I had misread what you typed, and that of course it works also.
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: re - usable lock?

Post by bongobeat »

thanks all for your answers, I'm gonna study this
kelly1111 wrote:What kind of dungeon are you making?
very interrested in your project !

Its a large world to explore, following a main story, with sci fi levels (for end levels).
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: re - usable lock?

Post by bongobeat »

Thanks to Isaac, here is a full script:

object:
SpoilerShow

Code: Select all

defineObject{
	name = "indus_wall_biodome_log_console_lock",
	components = {
		{
			class = "Socket",
			offset = vec(-0.005, .895, -0.1),
			rotation = vec(0, 18, 0),
		--	debugDraw = true,
        	onAcceptItem = function(self, item) 
	            local disks = {   ["disklog_1"]= {"Drakes have destroyed the Biodome Sector 1 with most repair droids in it!",
	            								  "Things are getting worse, where is our goddam fleet?","Colonel Rangson."},

	                              ["disklog_2"]= {"A long list of things to do, repairing console N29, Security Door 18 malfunction,",
	                              				  "Changing access codes to the command center and alpha center, current 97887,",
	                              				  "leak in the cooling system sector 5..."},
	                             
	                              ["disklog_3"] = {"This pie is rotten!"}, 
	                          }

		            if disks[item.go.id] then
		            playSound("ogre_rush_begin")	            
			            -- Place your own events here.|

			            			-- If you don't want the hudPrinted text, you can remove the hudPrint statements and loops.
									-- If you like, you could make each disklog_x value [in 'disks'] be a function() instead of a string;
									-- and call them like this:  
									--	 disks[item.go.id]()
									-- Each disklog inserted would call its own function in disks.	
									-- Example:
											--|-------------------------------------------------------------|-- 
													disks["disklog_3"] = function(self)
																			my_door.door:open()
																			for line = 1, 5 do hudPrint("") end --clears hud text
																			hudPrint("Security Door is "..iff(my_door.door:isOpening(),"","already ").."Unlocked!")
																		 end

													if item.go.id == "disklog_3" then
														disks[item.go.id]()
														return
													end
											--|-------------------------------------------------------------|--
									--
									--
							--hudPrint code		
			            	 for line = 1, 5 do hudPrint("") end
			            	 for line = 1, #disks[item.go.id] do
				                hudPrint(disks[item.go.id][line])
				             end   
									
			            -- ========================================|

		            end
	            return item.go.name == "disklog" and self.go.socket:count() == 0
	        end,
		},
	    {
	        class = "Clickable",
	        offset = vec(0, 1, 0),
	        size = vec(0.3, 0.3, 0.4),
	       -- debugDraw = true,
	    },
	    {
	        class = "Model",
	        model = "assets/models/env/wall_lock.fbx",
	        offset = vec(0, 1, 0),
	        staticShadow = true,
	        enabled = true,
	    },
		{
			class = "Occluder",
			model = "assets/models/env/dungeon_wall_01_occluder.fbx",
		}
	},
	replacesWall = false,
	placement = "wall",
	editorIcon = 8,
}
item to use (example):
SpoilerShow

Code: Select all

defineObject{
   name = "disklog",
   baseObject = "pellet_box",
   components = {
	   		{
			class = "Item",
			uiName = "Pellets",
			gfxIndex = 334,
			gfxIndexInHand = 335,
			stackable = false,
			multiple = 10,
			weight = 0.05,
			onInit = function(self) self.go.item:setUiName("Disk Log "..string.sub(self.go.id, -1)) end,
			},
		}
}
and an example dungeon: https://www.dropbox.com/s/z4pz87th6cug3 ... k.zip?dl=0

edit : I've modified the sound into a well known more versatile build-in sound 8-)
Last edited by bongobeat on Mon Jul 18, 2016 10:58 pm, edited 1 time in total.
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: re - usable lock?

Post by Isaac »

In the script, playSound("lab_alarm"), should probably be commented out unless one has defined a sound by that name.

*One could also include two functions to play 'disk-insert & removal' sound effects, connecting them to the socket-'lock', and they would play when the disk was placed or pulled.

** This script is was meant as a display of options, with the custom disk-keyed function call being slipped in as an afterthought. It's meant more as a guide for writing your own [better] script, than to be used as-is;
though it works as-is. ;)

***Also The UiName setting script for the disklog item, was a quick convenience for the example project... It only supports 10 disks, and the 10th will be misnamed as disklog_0; and with subsequent disks repeating 1-9.
Post Reply