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!
gmsantashelper
Posts: 3
Joined: Thu May 04, 2017 1:58 am

Re: Ask a simple question, get a simple answer

Post by gmsantashelper »

Interesting. The readme for alpha008 of grimtk says:
3. Open your dungeon in the Dungeon Editor (or reload it) and find the object "grimtk" in the asset
browser. Add a single instance of this to your dungeon (anywhere). This will add the required scripts
to your dungeon.
In reality it must be placed at (0,0,0) or I get the crash. I had it placed in the starting location, which was not actually 0,0,0. Moved it to 0,0,0 and now it works just fine again. Fairly certain it used to work where it was. Very odd. Guess this ended up being a simple problem after all. :roll:
Thanks for the help everyone.
ColdDeadStone
Posts: 24
Joined: Sat Nov 26, 2016 12:12 pm

Re: Ask a simple question, get a simple answer

Post by ColdDeadStone »

I have a simple question and would be happy about a simple answer and maybe a solution! Why is this always false? a and b are Equal!

Code: Select all

function vectorcheck()
local a = vec(6.289999, 2.240000, 9.000000, 0.000000)
local b = vec(6.289999, 2.240000, 9.000000, 0.000000)

	if a == b then
		print("true")
	else
		print("false")
	end
end
Thanks for reading!
Sorry for my poor english... hope you understand what i try to say! :)
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 »

ColdDeadStone wrote:I have a simple question and would be happy about a simple answer and maybe a solution! Why is this always false? a and b are Equal!

Code: Select all

function vectorcheck()
local a = vec(6.289999, 2.240000, 9.000000, 0.000000)
local b = vec(6.289999, 2.240000, 9.000000, 0.000000)

	if a == b then
		print("true")
	else
		print("false")
	end
end
Thanks for reading!
Im not a vector savvy person, however, if you change your function to this:
SpoilerShow

Code: Select all

function vectorcheck()
local a = {6.289999, 2.240000, 9.000000, 0.000000}
local b = {6.289999, 2.240000, 9.000000, 0.000000}

   if a == b then
      print("true")
   else
      print("false")
   end
end
(a and b are now just tables) ... then it works as you wanted

Also read up on the vector functions here
viewtopic.php?f=22&t=13925

Akroma
Last edited by akroma222 on Tue May 23, 2017 5:02 pm, edited 1 time in total.
User avatar
Leki
Posts: 550
Joined: Wed Sep 12, 2012 3:49 pm

Re: Ask a simple question, get a simple answer

Post by Leki »

ColdDeadStone wrote:I have a simple question and would be happy about a simple answer and maybe a solution! Why is this always false? a and b are Equal!

Code: Select all

function vectorcheck()
local a = vec(6.289999, 2.240000, 9.000000, 0.000000)
local b = vec(6.289999, 2.240000, 9.000000, 0.000000)

	if a == b then
		print("true")
	else
		print("false")
	end
end
Thanks for reading!
There is no built-in function for comparing tables by contents. You'll have to write your own. Smth like (untested, but you see the point I guess):

Code: Select all

function is_table_equal(t1,t2,ignore_mt)
   local ty1 = type(t1)
   local ty2 = type(t2)
   if ty1 ~= ty2 then return false end
   -- non-table types can be directly compared
   if ty1 ~= 'table' and ty2 ~= 'table' then return t1 == t2 end
   -- as well as tables which have the metamethod __eq
   local mt = getmetatable(t1)
   if not ignore_mt and mt and mt.__eq then return t1 == t2 end
   for k1,v1 in pairs(t1) do
      local v2 = t2[k1]
      if v2 == nil or not is_table_equal(v1,v2) then return false end
   end
   for k2,v2 in pairs(t2) do
      local v1 = t1[k2]
      if v1 == nil or not is_table_equal(v1,v2) then return false end
   end
   return true
end
I'm the Gate I'm the Key.
Dawn of Lore
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 »

Another way to do the comparing...
SpoilerShow

Code: Select all

function vectorcheck()

	local v1 = vec(6.289999, 2.240000, 9.000000, 0.000000)
	local v2 = vec(6.289999, 2.240000, 9.000000, 0.000000)

	if vec.isvec(v1) and vec.isvec(v2) then
		for i = 1,4 do
			if vec.__index(v1, i) ~= vec.__index(v2, i) then
	      		print("false")
				return false
			end
		end   		
		print("true")
		return true
	else
		print(iff(vec.isvec(v1),"v2","v1").." is not a vector")
		return
	end
		
end

OR if you want it to be a general function that can compare 2 vectors (passed as v1 and v2)...
SpoilerShow

Code: Select all

function vectorcheck(v1, v2)
	
	if not v1 or not v2 then return end
	
	if vec.isvec(v1) and vec.isvec(v2) then
		for i = 1,4 do
			if vec.__index(v1, i) ~= vec.__index(v2, i) then
	      		print("false")
				return false
			end
		end   		
		print("true")
		return true
	else
		print(iff(vec.isvec(v1),"v2","v1").." is not a vector")
		return
	end
		
end
Last edited by akroma222 on Tue May 23, 2017 5:03 pm, edited 1 time in total.
ColdDeadStone
Posts: 24
Joined: Sat Nov 26, 2016 12:12 pm

Re: Ask a simple question, get a simple answer

Post by ColdDeadStone »

Uhh, that's a lot of info's! Thank you very much for answering me @akroma222 & @Leki! I will work through these Examples! Is not as easy as I thought but anyway, thanks again and have a nice Day!
Sorry for my poor english... hope you understand what i try to say! :)
minmay
Posts: 2768
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

The most concise way to check if two vectors are equal is to check if vec.length(a-b) == 0.

Code: Select all

local a = vec(6.289999, 2.240000, 9.000000, 0.000000)
local b = vec(6.289999, 2.240000, 9.000000, 0.000000)

   if vec.length(a-b) == 0 then
      print("true")
   else
      print("false")
   end
Alternatively, if you want to use the == operator to compare vectors for equality, run this in your init.lua or a file imported by it (NOT from a ScriptComponent):

Code: Select all

vec.__eq = function(a,b) return a[1]==b[1] and a[2]==b[2] and a[3]==b[3] and a[4]==b[4] end
(This method is a little more efficient than calling length).
You can then use a == b to check whether vectors a and b have the same contents.
Note that this change will persist until the game is closed, so it can carry into other dungeons.
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.
Torquemada
Posts: 25
Joined: Fri Apr 05, 2013 10:52 pm

Re: Ask a simple question, get a simple answer

Post by Torquemada »

Here's my simple question, I hope the answer is as simple
I want a door to open when a monster group is dead. It's simple with a single monster but the group doesn't have onDie hook
User avatar
Isaac
Posts: 3175
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

Torquemada wrote:Here's my simple question, I hope the answer is as simple
I want a door to open when a monster group is dead. It's simple with a single monster but the group doesn't have onDie hook
A quick—but not scrupulously accurate way, is to track the monsters with a set, and consider them dead as you mark them off the list.
(This doesn't actually check to see if they're dead, but they shouldn't be alive either. There are cases in Grimrock where a dead monster can still exist.)

Code: Select all

--List the monsters in the set here
set = {	"zarchton_1",
		"zarchton_2",	
		"zarchton_3",
		"zarchton_4",
	}

--Adds onDie hooks at load
for x = 1, #set do
	local monster = findEntity(set[x])
	if monster then
	   monster.monster:addConnector("onDie", self.go.id, "deadCheck")
	end
end

--Gets called by each monster as they die
function deadCheck(caller)
	
	--No entries, means they are all dead
	if #set <= 1 then
		dungeon_door_wooden_1.door:open()
		return
	end
	
	--removes the dying monster from the set
	for k,v in pairs(set) do
		if v == caller.go.id then
			table.remove(set, k)
			break
		end
	end	
end
Fill the list at the top with your own monster id strings, and change the id of the door to match yours.

*I just did a very minor update, that bypasses the table operation for the last monster. You can use the updated script, but the previous script would still work fine.
minmay
Posts: 2768
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Pretty sure by "monster group" they meant a MonsterGroupComponent, not several different manually placed monsters.
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.
Post Reply