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!
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Davenfal wrote:That worked perfectly, thanks for the help! I do have another question:

Code: Select all

function openNorthGates()
   if NorthGate.onInsertItem(NorthGate, "essence_balance_1") then
         north_gate1.door:open();
		 north_gate2.door:open();
   else
         north_gate1.door:close();
         north_gate2.door:close();
   end
end
For some reason, the field onInsertItem is returning a nil value, am I calling it wrong or is my mistake elsewhere?
The onInsertItem hook is called when an item is inserted in the socket. It returns false if the item should be rejected (not placed in the socket), and something esle if it shouldn't be (generally true or nil). Circumstances under which it makes sense to manually call it yourself are extremely esoteric. Furthermore, "essence_balance_1" is a string, not an item.
Grimfan wrote:Sockets normally only take a single item like a torch (they are not like altars and alcoves). Each of the beacons only accepts a single essence as far as I know.
The beacons accept literally any item in the game. You need to check the item's name:

Code: Select all

function openNorthGate()
       local item = NorthGate.socket:getItem()
       if item and item.go.name == "essence_balance" then
         northgate1.door:open()
         northgate2.door:open()
         end
      end
edit: typo fix
Last edited by minmay on Mon Dec 22, 2014 9:40 am, edited 1 time in total.
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.
Davenfal
Posts: 5
Joined: Mon Dec 22, 2014 4:58 am

Re: Ask a simple question, get a simple answer

Post by Davenfal »

minmay wrote:
Davenfal wrote:That worked perfectly, thanks for the help! I do have another question:

Code: Select all

function openNorthGates()
   if NorthGate.onInsertItem(NorthGate, "essence_balance_1") then
         north_gate1.door:open();
		 north_gate2.door:open();
   else
         north_gate1.door:close();
         north_gate2.door:close();
   end
end
For some reason, the field onInsertItem is returning a nil value, am I calling it wrong or is my mistake elsewhere?
The onInsertItem hook is called when an item is inserted in the socket. It returns false if the item should be rejected (not placed in the socket), and something esle if it shouldn't be (generally true or nil). Circumstances under which it makes sense to manually call it yourself are extremely esoteric. Furthermore, "essence_balance_1" is a string, not an item.
Grimfan wrote:Sockets normally only take a single item like a torch (they are not like altars and alcoves). Each of the beacons only accepts a single essence as far as I know.
The beacons accept literally any item in the game. You need to check the item's name:

Code: Select all

function openNorthGate()
       local item = NorthGate.socket:getItem()
       if item and item.name == "essence_balance" then
         northgate1.door:open()
         northgate2.door:open()
         end
      end
I appreciate the explanations, they helped clear up quite a bit of what was confusing me. The script no long throws me an error!
Last edited by Davenfal on Mon Dec 22, 2014 10:05 am, edited 2 times in total.
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: Ask a simple question, get a simple answer

Post by Grimfan »

Sorry about before Davenfal. I was confusing the visual effects of the beacon with what it actually accepts.

This works in my mod (it opens the door):

Code: Select all

function revealTheWay()
	local e = beacon_air_1.socket:getItem()
	if e.go.name == "essence_air" then
	castle_secret_door_83.door:open()
	end
end
And yes I have 83 secret castle doors in my mod. ;)
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Grimfan wrote:

Code: Select all

function revealTheWay()
	local e = beacon_air_1.socket:getItem()
	if e.go.name == "essence_air" then
	castle_secret_door_83.door:open()
	end
end
This will crash if the beacon does not have an item in it, since you can't index nil values. That's the purpose of checking that the item exists in the function I posted (which had a different error in it that I've fixed, sorry).
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.
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: Ask a simple question, get a simple answer

Post by Grimfan »

I now know that it causes a crash, but why was it working for me? I could leave the beacon empty and nothing happens (it doesn't crash). Why was that the case?

Well, time to change some scripts around. :roll:
Davenfal
Posts: 5
Joined: Mon Dec 22, 2014 4:58 am

Re: Ask a simple question, get a simple answer

Post by Davenfal »

I'm encountering a really strange oddity. whenever I set an essence onto a beacon whose height is lower than zero, the essence doesn't properly adjust its height to that of the beacon and instead floats at a height of zero. Is there an easy way to change it's position after it's placed?
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Grimfan wrote:I now know that it causes a crash, but why was it working for me? I could leave the beacon empty and nothing happens (it doesn't crash). Why was that the case?
Depends when you were calling that function. If you were only calling it when an item is inserted then it always had an item in it.
Davenfal wrote:I'm encountering a really strange oddity. whenever I set an essence onto a beacon whose height is lower than zero, the essence doesn't properly adjust its height to that of the beacon and instead floats at a height of zero. Is there an easy way to change it's position after it's placed?
You wouldn't find this strange at all if you looked at the definition in the asset pack, specifically the onUpdate hook for the beacon light:

Code: Select all

			onUpdate = function(self)
				local socket = self.go.socket
				if socket:count() > 0 then
					local it = socket:getItem()
					if it.go.name == "essence_fire" then
						it.go:setWorldPositionY(0.85 + math.sin(Time.currentTime()) * 0.05)
					elseif it:hasTrait("essence") then
						it.go:setWorldPositionY(0.75)
					end
				end
			end,
As you can see, it sets the essence's position independently of the beacon's position. You should be able to change this behaviour by doing something like this:

Code: Select all

			onUpdate = function(self)
				local socket = self.go.socket
				if socket:count() > 0 then
					local it = socket:getItem()
					if it.go.name == "essence_fire" then
						it.go:setWorldPositionY(self.go:getWorldPositionY() + 0.85 + math.sin(Time.currentTime()) * 0.05)
					elseif it:hasTrait("essence") then
						it.go:setWorldPositionY(self.go:getWorldPositionY() + 0.75)
					end
				end
			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.
Periodiko
Posts: 19
Joined: Fri Oct 19, 2012 2:45 am

Re: Ask a simple question, get a simple answer

Post by Periodiko »

I'm trying to relearn how the scripting works in this game and I was wondering if you could help me with some simple stuff.

Ya'll know about the horn you blow at the beach to summon the viper roots for the boss battle? Well it works by the following script embedded in it's object definition:

Code: Select all

			onAttack = function(self)
				for e in party.map:entitiesAt(party.x, party.y) do
					if e.script and e.script.blowTheHorn then
						e.script:blowTheHorn()
					end
				end
				playSound("blow_horn")
			end,
How does "in" work?

Also, a maybe slightly more involved question: if I wanted to use a similar function in an object definition to run a level script object, how would I do that? Say I had a horn that when you blew it, it ran a script that opened all the doors in the level and started a boss-fight, for example, but it happened regardless of where you were on the level.
GoldenShadowGS
Posts: 168
Joined: Thu Oct 30, 2014 1:56 am

Re: Ask a simple question, get a simple answer

Post by GoldenShadowGS »

Periodiko wrote:I'm trying to relearn how the scripting works in this game and I was wondering if you could help me with some simple stuff.

Ya'll know about the horn you blow at the beach to summon the viper roots for the boss battle? Well it works by the following script embedded in it's object definition:

Code: Select all

			onAttack = function(self)
				for e in party.map:entitiesAt(party.x, party.y) do
					if e.script and e.script.blowTheHorn then
						e.script:blowTheHorn()
					end
				end
				playSound("blow_horn")
			end,
How does "in" work?

Also, a maybe slightly more involved question: if I wanted to use a similar function in an object definition to run a level script object, how would I do that? Say I had a horn that when you blew it, it ran a script that opened all the doors in the level and started a boss-fight, for example, but it happened regardless of where you were on the level.
Its sort of like a list of everything in a certain container. this part of the script will repeat as many times as the number of objects that the container contains. For this case, e = everything on tile the party is standing at.

Here is an example I did:
I want to see if there is any entity with platform component at a certain spot, at a certain elevation.(bridges, etc)
elevation, hasbridge are my variables, don't worry about them

Code: Select all

			for i in party.map:entitiesAt(myx,myy) do
				if i.elevation == elevation and i:getComponent("platform") then 
					if i.platform:isEnabled() then
						hasbridge = true
					end
				end
			end
User avatar
Thorham
Posts: 80
Joined: Sat May 04, 2013 5:12 pm

Re: Ask a simple question, get a simple answer

Post by Thorham »

How can I set the scale of a custom GUI? By default, elements like fonts always have the same size, regardless of the screen resolution. Any way to change this?
Post Reply