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
Isaac
Posts: 3188
Joined: Fri Mar 02, 2012 10:02 pm

Re: Script: Make Multiple Buttons/Levers

Post by Isaac »

The script (at least the copy I have) connects all generated buttons to its own buttonPress() function; where the logic there determines what to do with each press. You can wrap a condition around all logic in buttonPress(), to have it simply do nothing. The buttons can still be pressed, but none trigger the door.

Example: —Replacement buttonPress()—

Code: Select all

activeButtons = true
function buttonPress(my)
     if activeButtons then   
		--=|-------------------- Compare/or replace with your script's button logic, if different ---

		  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
end
	
function setActive(bool) --call with false, when you break/disable the door.
   if type(bool) == "boolean" then	
		activeButtons = bool
   end	
end	
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: Script: Make Multiple Buttons/Levers

Post by bongobeat »

thank you for answering
I have deleted the message here, because I wanted to sent a pm first, what am I doing..... :roll:

Not sur if I have do this correctly: Can you check please?
the " --* " mark the changes
SpoilerShow

Code: Select all

--calcul = 97887
--*
activeButtons = true
--*
offset_num = 0
drast_combination_lock.script.setCombination(self.go.id,{33,31,32,32,31}, function() indus_biodome_moongate_door_security_2.door:open() end)

function rune_offset(amount)
	offset_num = offset_num + amount
	return offset_num
end  


	 function makeControls(my, what, base, size, range, omit)
      local isLever = what == "lever"
      if size == nil then
        if what == "lever" then
          size = {3, 6}
        else
          size = {6, 6}
          end
        end
      local area
      local rows, cols = size[1], size[2]
      if isLever then area = {.9, -.6, 1, -1}
        else area = {1.1, -.7, 1, -1}
        end
      if range ~= nil then
        for i = 1, 4 do
          if range[i] ~= nil then
            area[i] = range[i]
            end
          end
        end
     
      local drop = {}
      if omit ~= nil then
        for i = 1, # omit do
          drop[tostring(omit[i])] = true
          end
        end

      local r1, r2, c1, c2 = area[1], area[2], area[3], area[4]
      if rows == 1 then rdiv = 1 else rdiv = rows - 1 end
      if cols == 1 then cdiv = 1 else cdiv = cols - 1 end
      local dr, dc = (r1 - r2) / rdiv, (c1 - c2) / cdiv
      local i, k = self.go.facing % 2 * 2 + 1, (self.go.facing + 1) % 2 * 2 + 1
      local ar, ac = getForward(self.go.facing)
      if self.go.facing == 1 or self.go.facing == 2 then sign = 1
        else sign = -1
        end
      local adj = 0.0001 * (ar - ac)

      for row = 1, rows do
        local r = r1 - (row - 1) * dr
        for col = 1, cols do
          if not drop[row..col] then
            local c = c1 - (col - 1) * dc
            local ctl = spawn(what, my.go.level, my.go.x, my.go.y, my.go.facing, my.go.elevation, base..row..col)
            local vec = ctl:getWorldPosition()
            vec[2] = vec[2] + r
            vec[i] = vec[i] + c * sign
            vec[k] = vec[k] - adj
            ctl:setWorldPosition(vec)
            if isLever then ctl.lever:addConnector("onToggle", my.go.id, "leverFlip")       
              else ctl.button:addConnector("onActivate", my.go.id, "buttonPress")
              end
            end
          end
        end
      end

    function buttonPress(my)
--*
 		if activeButtons then 	
--*
			local pushed = tonumber(string.sub(my.go.id, string.len(my.go.id) - 1))
--			hudPrint("button pressed:  "..pushed)
   		   	drast_combination_lock.script.buttonPress(self.go.id, pushed)
--*
		end
--*
    end

    function leverFlip(my)
      local lev = findEntity(my.go.id)
      local flipped = tonumber(string.sub(my.go.id, string.len(my.go.id) - 1))
      drast_combination_lock(self.go.id,pushed)
      end

    makeControls(self, "numpad_button", self.go.id .."_button_", {4, 3}, {.525, -.36, .315, -.315})
--*
function setActive(bool) --call with false, when you break/disable the door.
   if type(bool) == "boolean" then   
      activeButtons = bool
   end
activeButtons = false
end
--*
Is there any other way, to simply destroy the spawned buttons of the script?
I'm worried about the number of buttons that become of no use after that the activebuttons get false.
I've 3 scripts like that, for 3 doors, that make 36 buttons (3x12), that are saved every savegame, well I suppose they are saved like any other buttons?
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
Post Reply