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 »

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

Re: Ask a simple question, get a simple answer

Post by Isaac »

minmay wrote:
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).
I haven't seen a crash with this ...yet :mrgreen:

Code: Select all

    function destroyCaller(caller)
       if caller and caller.go and caller.go.timer then
          caller.go:destroyDelayed()
       end
       print(caller, 'destroyed')
    end
When I call script_entity_4.script.destroyCaller() I get 'nil destroyed' as the expected output.
When I call script_entity_4.script:destroyCaller() I get 'table: <whatever the name> destroyed' as the expected output.
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Yes, that won't crash, because you check for the existence of caller before you try to index it.

Actually I guess findEntity(caller.go.id) could return nil in the very specific cases that:
1. caller is a component that exists, but its parent GameObject is not on a map (perhaps it's an item inside an inventory)
or 2. caller is not a component, but is a table with a "go" field that is a table with an "id" field that contains a string that is not the id of any GameObject that exists on a map

But if caller or caller.go don't exist (or they aren't tables), or caller.go.id is not a string, then you're just going to get an error when you try to index them/pass a non-string.
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: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

minmay wrote:Yes, that won't crash, because you check for the existence of caller before you try to index it.
But of course, that's the reason for it. :D
But if caller or caller.go don't exist (or they aren't tables), or caller.go.id is not a string, then you're just going to get an error when you try to index them/pass a non-string.
Aha. This I can certainly see, but it seems probably impossible to happen without the modder knowing about it in advance; an issue only for second party modders that are using large scripts from someone else, and/ or they don't know how they work. How does anyone get an object like that in their mod... one that would have that structure... if they didn't add it themselves. One would have to create the thing and setup the call to the destroy function to get the error.
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Of course.
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 »

minmay wrote:Yes, that won't crash, because you check for the existence of caller before you try to index it.
Which is exactly what was happening from the very beginning of this particular discussion :shock:
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

no it wasn't...
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 »

It most certainly was, crom's original code to make the call is,"bandagetimer.timer:addConnector("onActivate", "destroy_timer", "destroyCaller","bandage_applied" )

Which means that the Timer Component is ALWAYS passed as the caller. There is no need to say, "if caller" in this scenario. If the Timer Component didn't exist, then the onActivate method for that very timer would never trigger.

*EDIT*

Case in Point:

In the Nexus Campain, the official script on the Barren Dessert to handle a Golemn's Death is:

Code: Select all

function golemDie(sender)
	sender.go:spawn("power_gem")
end
And this is attached to the Golem with a simple connecter. As you can see, there is no need to say "if sender" before Almost Human typed "sender.go" 8-)
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

I was specifically commenting on Isaac's statement here, which is why I quoted it:
Isaac wrote:findEntity(caller.go.id) returns either the caller object or nil; but the caller object is already available in the function.
Of course you do not need to check for the parameter's existence if you are always passing it in the first place. Like, duh. Sorry for any confusion.
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
AndakRainor
Posts: 674
Joined: Thu Nov 20, 2014 5:18 pm

Re: Ask a simple question, get a simple answer

Post by AndakRainor »

Is there any way to deactivate the throwing of mouse item ? I made a small window where I would like to drag and drop items, but when I click on it the mouse item is thrown.
Post Reply