Runtime error - when using body:setActive

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
obey
Prole
Posts: 16
Joined: Sun Jun 09, 2013 3:18 pm

Runtime error - when using body:setActive

Post by obey » Sun Jun 30, 2013 8:57 pm

Here is the part of the code that triggers the error (it is used for bullet collision with enemies):

Code: Select all

function beginContact(a, b, coll)
	if a:getCategory()==2 and b:getCategory()==2 then
		if a:getUserData() ~= b:getUserData() then
	    	a:getBody():setActive(false)-- This produces the error
When I comment out the line of the error, I get no error. What might cause this?

BTW:
I'm not uploading a love file because my game is relatively big (with plenty of assets).

User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Runtime error - when using body:setActive

Post by Plu » Sun Jun 30, 2013 9:04 pm

What kind of runtime error?

obey
Prole
Posts: 16
Joined: Sun Jun 09, 2013 3:18 pm

Re: Runtime error - when using body:setActive

Post by obey » Sun Jun 30, 2013 9:08 pm

Plu wrote:What kind of runtime error?
Attached image shows the exact error.
Attachments
blabla.png
Error picture
blabla.png (9.26 KiB) Viewed 2426 times

User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Runtime error - when using body:setActive

Post by Boolsheet » Sun Jun 30, 2013 9:20 pm

This is an uncaught exception. It's fixed in 0.9.0, I think.

You're not supposed to change the world during an update/timestep. To my knowledge, setting a body active or inactive counts as that.
Shallow indentations.

obey
Prole
Posts: 16
Joined: Sun Jun 09, 2013 3:18 pm

Re: Runtime error - when using body:setActive

Post by obey » Sun Jun 30, 2013 9:22 pm

Boolsheet wrote:This is an uncaught exception. It's fixed in 0.9.0, I think.

You're not supposed to change the world during an update/timestep. To my knowledge, setting a body active or inactive counts as that.
So how would you suggest to make the bullet disappear and the enemy's body to change to dead?
I need some kind of flag, and the active property sounded good for that.

User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Runtime error - when using body:setActive

Post by Boolsheet » Sun Jun 30, 2013 9:42 pm

obey wrote:the active property sounded good for that.
Yeah, I wanted to use it like that too.

I guess you have to store the flag somewhere in a table. Haven't touched Box2D in quite a while and never did something big with it so I can't really go into the design of it, sorry..
Shallow indentations.

obey
Prole
Posts: 16
Joined: Sun Jun 09, 2013 3:18 pm

Re: Runtime error - when using body:setActive

Post by obey » Sun Jun 30, 2013 9:48 pm

Boolsheet wrote:
obey wrote:the active property sounded good for that.
Yeah, I wanted to use it like that too.

I guess you have to store the flag somewhere in a table. Haven't touched Box2D in quite a while and never did something big with it so I can't really go into the design of it, sorry..
Yea I thought about creating a table, but how would I know which object to flag out? the collisions are referencing the fixture (you can't attach user data on it), so even that doesn't help.
I need a property that Is bound to the fixture/body/shape that I can change in order to flag it out.

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Runtime error - when using body:setActive

Post by Robin » Mon Jul 01, 2013 9:31 am

You can use a single table that is globally accessible and has the bodies as values:

Code: Select all

function beginContact(a, b, coll)
	if a:getCategory()==2 and b:getCategory()==2 then
		if a:getUserData() ~= b:getUserData() then
	    	inactive_bodies[#inactive_bodies + 1] = a:getBody()
in love.load:

Code: Select all

function love.load()
    -- ...
    inactive_bodies = {}
end
And in love.update:

Code: Select all

function love.update(dt)
    -- ...
    world:update(dt)
    for i, body in ipairs(inactive_bodies) do
        body:setActive(false)
    end
    inactive_bodies = {}
    -- ...
end
Hope that helps.
Help us help you: attach a .love.

obey
Prole
Posts: 16
Joined: Sun Jun 09, 2013 3:18 pm

Re: Runtime error - when using body:setActive

Post by obey » Mon Jul 01, 2013 6:57 pm

Robin wrote:You can use a single table that is globally accessible and has the bodies as values:

Code: Select all

function beginContact(a, b, coll)
	if a:getCategory()==2 and b:getCategory()==2 then
		if a:getUserData() ~= b:getUserData() then
	    	inactive_bodies[#inactive_bodies + 1] = a:getBody()
in love.load:

Code: Select all

function love.load()
    -- ...
    inactive_bodies = {}
end
And in love.update:

Code: Select all

function love.update(dt)
    -- ...
    world:update(dt)
    for i, body in ipairs(inactive_bodies) do
        body:setActive(false)
    end
    inactive_bodies = {}
    -- ...
end
Hope that helps.
Thanks,
I fixed it by using all these 3 :

Code: Select all

		zombies[i].fixture:setCategory(2) 
		zombies[i].fixture:setGroupIndex(3) --zombie group
		zombies[i].fixture:setUserData("1") --alive
And:

Code: Select all

function beginContact(a, b, coll)
	if a:getCategory()==2 and b:getCategory()==2 then
		if a:getGroupIndex() ~= b:getGroupIndex() then
	    	a:setUserData("0")
	    	b:setUserData("0")
	    end
    end
end

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 8 guests