Page 96 of 396

Re: Ask a simple question, get a simple answer

Posted: Thu Oct 08, 2015 12:49 am
by minmay
Yes.

Re: Ask a simple question, get a simple answer

Posted: Thu Oct 08, 2015 1:12 am
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.

Re: Ask a simple question, get a simple answer

Posted: Thu Oct 08, 2015 2:20 am
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.

Re: Ask a simple question, get a simple answer

Posted: Thu Oct 08, 2015 4:44 am
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.

Re: Ask a simple question, get a simple answer

Posted: Thu Oct 08, 2015 5:00 am
by minmay
Of course.

Re: Ask a simple question, get a simple answer

Posted: Thu Oct 08, 2015 7:06 am
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:

Re: Ask a simple question, get a simple answer

Posted: Thu Oct 08, 2015 8:45 am
by minmay
no it wasn't...

Re: Ask a simple question, get a simple answer

Posted: Thu Oct 08, 2015 9:35 am
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-)

Re: Ask a simple question, get a simple answer

Posted: Thu Oct 08, 2015 11:56 am
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.

Re: Ask a simple question, get a simple answer

Posted: Thu Oct 08, 2015 11:02 pm
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.