Script: Make Multiple Buttons/Levers

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!
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Script: Make Multiple Buttons/Levers

Post by cromcrom »

Great Scripting Lark, thanks a lot :-)

I have a question, what calls makeControls(caller, control, base, size, space, omit) if I want the buttons or things like that to appear somewhere? Could you provide a quick short installation guide please ?
A trip of a thousand leagues starts with a step.
User avatar
Lark
Posts: 178
Joined: Wed Sep 19, 2012 4:23 pm
Location: Springfield, MO USA

Re: Script: Make Multiple Buttons/Levers

Post by Lark »

Sorry I wasn't clear on that - here's what you do:
  • Create a LUA script entity in the square that is to have the buttons/levers. Let's say buttons for the examples. The buttons will appear in the same square as the LUA script. The wall on which the buttons appear will depend upon which direction the script is facing.
  • Three functions and one initialization call were included in the original example.
    • makeControls - actually places the controls
    • buttonPress - gets called when any button is pressed; button ID is passed as a parameter
    • leverFlip - gets called when any lever is flipped; lever ID is passed as a parameter
  • The buttons are actually created by the last line in the script: makeControls(self, "wall_button", self.go.id .."_button_", {3, 3}, {.4, -.4, .4, -.4}, {21, 23, 12, 32}) is one included example.
So, in summary, just create a LUA script in the square where you want the buttons, copy and paste the script I wrote into your script entity. Change the script's facing direction to get your controls on the wall you desire, change the very last line to define your buttons/levers. Duplicate this line and alter it for every group of controls you want on the same wall - see my button and lever example at the beginning. Then alter the buttonPress and leverFlip routines to do whatever you want when the controls are activated. Alternately, you can alter the addConnector statements in the main function.

Does this answer your questions? Take care, -Lark
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Script: Make Multiple Buttons/Levers

Post by cromcrom »

You sure did. This is amazing, and so helpfull. Hello, crafting station :-)

Thank you so much for sharing :-)

Oh, and I credited you oc :-)
A trip of a thousand leagues starts with a step.
User avatar
THOM
Posts: 1280
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Script: Make Multiple Buttons/Levers

Post by THOM »

Well, I am not very smart in scripting, so this is some kind of miracle for me, Lark. :idea:

I could get this running in my mod and I could alter the appearance. But I do not understand how to make the buttons to do something.

So - what I have are three buttons in a vertical row and I want button A to trigger script A, button B to trigger script B and so...

Can someone give me a clue how to script that?
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
User avatar
THOM
Posts: 1280
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Script: Make Multiple Buttons/Levers

Post by THOM »

Bump.. :?:
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
User avatar
Isaac
Posts: 3188
Joined: Fri Mar 02, 2012 10:02 pm

Re: Script: Make Multiple Buttons/Levers

Post by Isaac »

THOM wrote:So - what I have are three buttons in a vertical row and I want button A to trigger script A, button B to trigger script B and so...

Can someone give me a clue how to script that?
The function buttonPress() is where the button or lever's action originates. The variable *pressed* is a local numerical id derived for the button; use it to script a block in the buttonPress() function, that either contains or calls your scripted action for each button.

Here is one method:
SpoilerShow
Replace Lark's buttonPress(), and the call to makeControls() [at the very end of the script] with these snippets. It will setup three [centered] buttons, and will call the function myFunc() in script_A, script_B, and script_C; which have to exist on the map.

Or... You can replace either or each *script_#.script.myFunc(pushed)* call with something like my_door_1.door:toggle() to have the button open and close the my_door object.

Code: Select all

   function buttonPress(my)
      local pushed = tonumber(string.sub(my.go.id, string.len(my.go.id) - 1))
--=|--------------------------------------------------------------------------------------------------------	 
	 local buttonAction = {	[12] = function() script_A.script.myFunc(pushed) end,
							
						[22] =  function() script_B.script.myFunc(pushed) end,
														
						[32] =  function() script_C.script.myFunc(pushed) end,
					    }
      buttonAction[pushed]() 
--=|--------------------------------------------------------------------------------------------------------	
      end

Code: Select all

--=|----place below leverFlip()------------------------------------------------------------------------------------
    makeControls(self, "wall_button", self.go.id .."_button_", {3, 3}, {.4, -.4, .4, -.4}, {11,21,33,13,23,31,33})
--=|--------------------------------------------------------------------------------------------------------
A simple template for script_A:

Code: Select all

--name object  script_A
function myFunc(pushed)
	playSound("secret") hudPrint("Button Pressed "..pushed)
end
If you want to change the arrangement of the buttons, look at the last parameter in the call to makeControls() [omit]; this removes buttons that you don't want. In the above script... the {11,21,33,13,23,31,33} table lists all the buttons not down the center in the 3x3 grid; seen as {3, 3} in the script. The numbers in the omit table are the rows & columns; so the first [top left] button is 11 ~for column 1, row 1.
Last edited by Isaac on Sun Jan 11, 2015 10:56 pm, edited 1 time in total.
User avatar
THOM
Posts: 1280
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Script: Make Multiple Buttons/Levers

Post by THOM »

thanks, Isaac. That helps a lot.
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
User avatar
Isaac
Posts: 3188
Joined: Fri Mar 02, 2012 10:02 pm

Re: Script: Make Multiple Buttons/Levers

Post by Isaac »

Image

It's a neat script; I'm still looking at it. I've not tried using levers yet. The hoops one had to jump through for multiple levers in LoG1 were insane.
User avatar
Duncan1246
Posts: 404
Joined: Mon Jan 19, 2015 7:42 pm

Re: Script: Make Multiple Buttons/Levers

Post by Duncan1246 »

Lark wrote: makeControls(self, "wall_button", self.go.id .."_button_", {3, 3}, {.4, -.4, .4, -.4}, {21, 23, 12, 32})
Hi Lark,
I use your script in my mod, it works fine, thanks you for this nice work!
I needs to use runes-buttons (same as "wall_button" but with a rune instead of the LOG1 logo, named "wall_button_fire", ..., etc. I understand that I have to modify the function "makeControls", but I don't know if it's actually possible without a more complex script. What do you think?
Duncan
The Blue Monastery (LOG1)
download at:http://www.nexusmods.com/grimrock/mods/399/?

Finisterrae(LOG2)
download at:http://www.nexusmods.com/legendofgrimrock2/mods/61/?
User avatar
Isaac
Posts: 3188
Joined: Fri Mar 02, 2012 10:02 pm

Re: Script: Make Multiple Buttons/Levers

Post by Isaac »

Duncan1246 wrote:I needs to use runes-buttons (same as "wall_button" but with a rune instead of the LOG1 logo, named "wall_button_fire", ..., etc.
The best way to have the runes, is to change the texture on the material... but that's not the easiest way. The second best , is to edit the model in blender an add the rune as a plane, one unit in front of the button face. It will cover the logo, rather than replace it.

To alter the textures, you could use Blender to bake the changes to the material(s); or you could use Photoshop/Gimp along with the normal map plug-in; to fake the baked results.
Post Reply