bad argument #-1 to 'newJoint' (Proxy expected, got nil)

Hi all,

I’m having trouble implementing the joining of two objects upon a collision. 

The tutorial i’m using can be found here: http://www.coronalabs.com/blog/2013/02/19/more-physics-tricks-explained/

Essentially I’m using the following code from it. 

function resolveColl()
local weldJoint = physics.newJoint( “weld”, self, event.other, self.x, self.y )
end

timer.performWithDelay( 10, resolveColl, 1 )

However, no matter what I do, i’m getting this error - bad argument #-1 to ‘newJoint’ (Proxy expected, got nil)

Within the function, i’ve tried printing both self and event.other to the console which is returning a table for both objects.

Does anyone know why I’m getting this issue? 

Any help or suggestions would be greatly appreciated.  

Thanks

Hi Edward,

This is probably a scoping issue. Did you pull this function (to weld) outside of the collision function? If so, it won’t know what “self” and “event.other” is. Can you post your code bit here?

Thanks,

Brent

Hi Brent,

Thanks for getting back to me. 

I suppose it is kind of outside as I’m putting the physics.newJoint in a separate function (within the collision function) so that I can use  timer.performWithDelay. 

Here’s the code:

function Character2:collision( event )

    if event.phase == “began” then

        local function resolveColl()

        local weldJoint = physics.newJoint( “weld”, self, event.other, self.x, self.y )

        print (“WELDJOINT HAS RUN”)

        end

    

        timer.performWithDelay( 10, resolveColl, 1 )

      

    end

end

Over the weekend I came across this:

http://forums.coronalabs.com/topic/35508-resolved-error-proxy-expected-got-nil/

and modified the code accordingly :

function Character2:collision( event )

    if event.phase == “began” then

        

        local function resolveColl()

        for i = 1, #event.other do

        local weldJoint = physics.newJoint( “weld”, self, event.other[ii], self.x, self.y )

        print (“WELDJOINT HAS RUN”)

        end

        end

    

        timer.performWithDelay( 10, resolveColl, 1 )

      

    end

end

In this instance, I am no longer getting the error. However, I am also not getting “WELDJOINT HAS RUN” printing to the console. It’s clear that the issue is with it not being able to see event.other within the function and my quick work around to get it working although I’m unsure of any other methods at this point. 

Thanks

Ed

Hi Ed,

You can pass item references along with timers using a little “trick”. 

[lua]

local t = timer.performWithDelay( 10, resolveColl ) ; t.otherObj = event.other

[/lua]

Then in the function that is called by the timer:

[lua]

local obj = event.source.otherObj

[/lua]

This should be the ID of the object… you can confirm so by printing the value before the timer, and then in the function the timer calls.

Brent

That’s brilliant! I’ll most certainty be able to use this technique, thanks Brent. 

Unfortunately it does not solve my problem. After a lot more testing I’ve narrowed it down to self causing the error but am not sure why? 

You could pass “self” along with the timer in the same method… would that solve the issue?

Brent

Hi Edward,

This is probably a scoping issue. Did you pull this function (to weld) outside of the collision function? If so, it won’t know what “self” and “event.other” is. Can you post your code bit here?

Thanks,

Brent

Hi Brent,

Thanks for getting back to me. 

I suppose it is kind of outside as I’m putting the physics.newJoint in a separate function (within the collision function) so that I can use  timer.performWithDelay. 

Here’s the code:

function Character2:collision( event )

    if event.phase == “began” then

        local function resolveColl()

        local weldJoint = physics.newJoint( “weld”, self, event.other, self.x, self.y )

        print (“WELDJOINT HAS RUN”)

        end

    

        timer.performWithDelay( 10, resolveColl, 1 )

      

    end

end

Over the weekend I came across this:

http://forums.coronalabs.com/topic/35508-resolved-error-proxy-expected-got-nil/

and modified the code accordingly :

function Character2:collision( event )

    if event.phase == “began” then

        

        local function resolveColl()

        for i = 1, #event.other do

        local weldJoint = physics.newJoint( “weld”, self, event.other[ii], self.x, self.y )

        print (“WELDJOINT HAS RUN”)

        end

        end

    

        timer.performWithDelay( 10, resolveColl, 1 )

      

    end

end

In this instance, I am no longer getting the error. However, I am also not getting “WELDJOINT HAS RUN” printing to the console. It’s clear that the issue is with it not being able to see event.other within the function and my quick work around to get it working although I’m unsure of any other methods at this point. 

Thanks

Ed

Hi Ed,

You can pass item references along with timers using a little “trick”. 

[lua]

local t = timer.performWithDelay( 10, resolveColl ) ; t.otherObj = event.other

[/lua]

Then in the function that is called by the timer:

[lua]

local obj = event.source.otherObj

[/lua]

This should be the ID of the object… you can confirm so by printing the value before the timer, and then in the function the timer calls.

Brent

That’s brilliant! I’ll most certainty be able to use this technique, thanks Brent. 

Unfortunately it does not solve my problem. After a lot more testing I’ve narrowed it down to self causing the error but am not sure why? 

You could pass “self” along with the timer in the same method… would that solve the issue?

Brent