Ask a simple question, get a simple answer

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

Re: Ask a simple question, get a simple answer

Post by Isaac »

The button asset has a dedicated disableSelf option in the editor properties.

....But it doesn't seem to work. :shock:

What you can do instead is the following:

Code: Select all

--Put this in a script_entity, and add a connection to it from any button that you wish disabled.
function disableButton(button)
	local count = button.go.button:getConnectorCount()
	for C = count, 1, -1 do
		local buttonId = findEntity(button.go.id)
		if buttonId then
			buttonId.button:removeConnector(buttonId.button:getConnector(1))
		end	
	end
end
User avatar
Xardas
Posts: 83
Joined: Fri Jul 28, 2017 12:30 am

Re: Ask a simple question, get a simple answer

Post by Xardas »

That's strange. I had the disableSelf option on true. I'm going to chrck that on another connection later.
Thanks for the script. That will make some things easier. :D
In order to get to the other side of the pit you have to get hit by the fireball and die....
Yep.....moving on!
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Isaac wrote:The button asset has a dedicated disableSelf option in the editor properties.

....But it doesn't seem to work. :shock:

What you can do instead is the following:

Code: Select all

--Put this in a script_entity, and add a connection to it from any button that you wish disabled.
function disableButton(button)
	local count = button.go.button:getConnectorCount()
	for C = count, 1, -1 do
		local buttonId = findEntity(button.go.id)
		if buttonId then
			buttonId.button:removeConnector(buttonId.button:getConnector(1))
		end	
	end
end
This has come up before. It's easier to do this (not to mention much more correct, since it allows enabling the button again):

Code: Select all

defineObject{
	name = "wall_button",
	baseObject = "wall_button",
	components = {
		{
			class = "Button",
			sound = "button",
			onActivate = function(self)
				return self:isEnabled()
			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: 3191
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

One certainly can... but the above script was quickly written specifically to not rely on enabled/disabled. I like your conventional method better.

I wonder how it happened that a button's disableSelf setting doesn't disable the button?
User avatar
Xardas
Posts: 83
Joined: Fri Jul 28, 2017 12:30 am

Re: Ask a simple question, get a simple answer

Post by Xardas »

Yes that fixed the Problem perfectly thanks. I have no idea why it didn´t work.

Topic Change :) Is there a simple way how i can make a Monster invulnerable?
In order to get to the other side of the pit you have to get hit by the fireball and die....
Yep.....moving on!
User avatar
Isaac
Posts: 3191
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

Xardas wrote:Is there a simple way how i can make a Monster invulnerable?
Plain Unkillable:

Code: Select all

myMonster.monster:setMonsterFlag("Invulnerable",true)
Shielded from all damage for a certain amount:

Code: Select all

myMonster:createComponent("GoromorgShield"):setEnergy(1000)
User avatar
akroma222
Posts: 1029
Joined: Thu Oct 04, 2012 10:08 am

Re: Ask a simple question, get a simple answer

Post by akroma222 »

The options listed by Isaac are the way to go

Also worth mentioning are the monster component's onDamage & onDie hooks
Returning false in the onDamage hook will prevent the damage
Returning false in the onDie hook will prevent the monster dying
These can be useful in a number of situations
User avatar
Xardas
Posts: 83
Joined: Fri Jul 28, 2017 12:30 am

Re: Ask a simple question, get a simple answer

Post by Xardas »

Thanks for that. I Chose to use Isaacs Option since it´s pretty simple to do. I needed it for some shopkeepers to be invulnerable.
Now that i have the Shops, i also wanted to make a Money changing machine which can choose between 3 states.
1. Change in Gold
2. Change in silver
3. Change in copper
I wrote the following Code for it and i can´t figure out why it won´t work. To mention is that i have made this Thing up out of codes i found here on the Forum. Mostly the stuff Isaac already gave me for some other Problems i had

Code: Select all

 function moneyinfo()
if kindcounter.counter:getValue() == 1 then
hudPrint("changing in gold coins")
end

if kindcounter.counter:getValue() == 2 then
hudPrint("changing in silver coins")
end

if kindcounter.counter:getValue() == 3 then
hudPrint("changing in copper coins")
end

if kindcounter.counter:getValue() >3 then
kindcounter.counter:setValue(0)
end
end

function surfaceContains(surface, item)
 for v,i in surface.surface:contents() do
 if i.go.name == item then return true
 end
end
end
----------------------------------------------------------
--counts coins
function moneychange()
local coinscopper ={}
local coinssilver ={}
local coinsgold ={}
   for v,i in moneychanger.surface:contents() do
 surfaceContains(moneychanger, "dm_coin_copper")
    if i then
      coinscopper[#coinscopper+1] = i 
    end
 surfaceContains(moneychanger, "dm_coin_silver")
    if i then
      coinssilver[#coinssilver+1] = i 
    end
  surfaceContains(moneychanger, "dm_coin_copper")
    if i then
      coinsgold[#coinsgold+1] = i 
    end
   end    
----------------------------------------------------------
if kindcounter.counter:getValue() == 1 then
local copper=#coinscopper%4
local silver=((#coinscopper/4)+#coinssilver)%4
local gold=((silver%4)+#goldcoins)
silver=silver/4
end
----------------------------------------------------------
if kindcounter.counter:getValue() == 2 then
local copper=#coinscopper%4
local silver=#coinscopper/4+#coinssilver+#coinsgold*4
local gold=0
end
----------------------------------------------------------
if kindcounter.counter:getValue() == 3 then
local gold=0
local silver=#coinssilver+#coinsgold*4
local copper=silver*4
silver=0
end
----------------------------------------------------------
for x = 0, #coinscopper do      
     coinscopper[x].go:destroy()
    end
for x = 0, #coinssilver do     
     coinssilver[x].go:destroy()
    end
for x = 0, #coinsgold do       
      coinsgold[x].go:destroy()
    end

for m=1,gold do
moneychanger.surface:addItem(spawn("dm_coin_gold").item)
end 
for m=1,silver do
moneychanger.surface:addItem(spawn("dm_coin_silver").item)
end
for m=1,copper do
moneychanger.surface:addItem(spawn("dm_coin_copper").item)
end
end
Once again it would be nice if you could tell me what´i did wrong here. :)
I hope the mistake(s) i made aren´t to great, so i can use this script afterall.
In order to get to the other side of the pit you have to get hit by the fireball and die....
Yep.....moving on!
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Isaac wrote:I wonder how it happened that a button's disableSelf setting doesn't disable the button?
It does disable the ButtonComponent. But ButtonComponent still triggers its onActivate hook (and therefore its connectors) even when disabled. The hook I posted makes it only trigger its connectors if enabled.
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: 3191
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

minmay wrote:
Isaac wrote:I wonder how it happened that a button's disableSelf setting doesn't disable the button?
It does disable the ButtonComponent. But ButtonComponent still triggers its onActivate hook (and therefore its connectors) even when disabled. The hook I posted makes it only trigger its connectors if enabled.
Well, changing the status of the button component doesn't seem to be the implied intent of a disableSelf option—or at least I hope not. Disabling the component should disable its actions, no?

Yes, I saw that in your script; it seems to be a good work around.
Post Reply