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!
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Ask a simple question, get a simple answer

Post by Azel »

Your bandagetimer addConnector call has 4 parameters, but I thought it only uses 3?

Component:addConnector(event, target, action)
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Ask a simple question, get a simple answer

Post by Azel »

cromcrom wrote: then, when it activates, it calls this function:

Code: Select all

function destroyCaller(caller)
  if findEntity(caller) then
    findEntity(caller):destroyDelayed()
   end
   print(findEntity(caller), 'destroyed')
end
However, I have a "could not look up class name " error. Any help, please? I know I could go the rough way, it does work, but I would like to have a broader destroying function, where I can pass the id as an argument.
I was able to get it to work by updating the destroyCaller function as follows:

Code: Select all

function destroyCaller(caller)
	local thisCaller = findEntity(caller.go.id);
	
	if thisCaller then
		thisCaller:destroyDelayed();
	end
	print(thisCaller.id, 'destroyed')
end

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

Re: Ask a simple question, get a simple answer

Post by cromcrom »

It works really great, thanks a lot for taking the time to look into this.
cheers.
A trip of a thousand leagues starts with a step.
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

cromcrom wrote:

Code: Select all

function destroyCaller(caller)
  if findEntity(caller) then
    findEntity(caller):destroyDelayed()
   end
   print(findEntity(caller), 'destroyed')
end
However, I have a "could not look up class name " error. Any help, please? I know I could go the rough way, it does work, but I would like to have a broader destroying function, where I can pass the id as an argument.
FindEntity expects a string as input.

Code: Select all

function destroyCaller(caller)
  if findEntity(caller.go.id) then
    findEntity(caller.go.id):destroyDelayed()
   end
   print(findEntity(caller.go.id), 'destroyed')
end
Azel wrote:I was able to get it to work by updating the destroyCaller function as follows:
local thisCaller = findEntity(caller.go.id);
Indeed; and it is because with the string id, findEntity ~finds the entity.
________________________
*I would recommend keeping the timer checks from the first example. It means that only timers get destroyed. If you accidentally connect a floor_trigger or another script to this function ~~it gets destroyed... with no errors to trace; once you comment out the print() statement.
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Ask a simple question, get a simple answer

Post by Azel »

True, Isaac. I would go one step further to clarify (for other fellow Mod newbies reading this) that the findEntity() method is expecting the Unique ID given to an in-game object. Which is either the name the Modder uniquely assigned the object when placing it in the Mod, or the name the Editor tool auto-assigned (which is typically just the general name with an auto-increment number at the end).

In this case, crom was passing the entire component/object in to the findEntity() method; so all I did was add the syntax to pass that objects Unique ID instead. Fin. :mrgreen:
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

In this particular case... ~caller is [usually] passed to the function; so it [usually] exists. There is not really a need to use findEntity() in this case, as one [usually] already has this entity.

findEntity(caller.go.id) returns either the caller object or nil; but the caller object is already available in the function.

if findEntity(caller.go.id) then is equivalent to if caller then in the sense that if the object does not exist, we get nil, but if it does exists ~we do not get nil. So this can work with an IF block without calling findEntity at all.

Code: Select all

if caller and caller.go and caller.go.timer then 
--do something
end
This checks first for a non-nil caller, then for the .go field, then for the .go.timer fields, and finding them all, considers the object a timer.
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Ask a simple question, get a simple answer

Post by cromcrom »

Actually, in the first connector call, I thought the 4th argument ("bandage_applied"), as a the id I created this timer with, would be considered as an id. But I was wrong, and, like you did, azel, had to identify it as an id again. I tryed many things combinations, actually. Iterative intuitive coding at its worst :-(.
But it works great now. I might change some other parts of my systems, because this seems more robust. Sigh. Thanks again anyways.
A trip of a thousand leagues starts with a step.
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Ask a simple question, get a simple answer

Post by Azel »

It's all good crom. Fumbling through the process and making repeated mistakes is the best way to become an expert at something :mrgreen:
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Isaac wrote:findEntity(caller.go.id) returns either the caller object or nil; but the caller object is already available in the function.
Actually it will either return the GameObject or it will crash, since if the object doesn't exist, you will get an error when you try to index caller (since it doesn't exist).

Also you guys need to be careful about conflating the GameObject and its components. Component.go will always give you the parent GameObject, but GameObject.go will give you nil (unless it has a component named "go", in which case it would obviously give you that, but that probably isn't what you want either).
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.
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Ask a simple question, get a simple answer

Post by Azel »

Right so that should mean...

When a trigger has a connector to a Script Function, and that function is accepting (as a Parameter) the calling Component (trigger component); then the following will work: caller.go

However, this will not work (it will return NIL) :

local trigger = findEntity("floor_trigger_1");
trigger.go

I think :shock:
Post Reply