Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple collision listeners per entity, for all instances, regardless of old ent:PhysicsCollide() #1868

Merged

Conversation

legokidlogan
Copy link
Contributor

Similar to #1865, but allows entities that already have :PhysicsCollide() from glua to still have starfall collision listeners. Also allows for different chip instances to have their own listeners on the same entity.

Bonus side effect due to how addCollisions() was reworked: previously, if two entities had collision listeners, any time either of them made a collision, it would call both listeners, resulting in callbacks being called for the collisions of entities they weren't associated with. This issue is now resolved with the rewrite.

@legokidlogan
Copy link
Contributor Author

@thegrb93 I've left a TODO note commenting on addCollisions(). Do we really need a one-tick delay before any and all processing collision listeners, and if so, why?

I know that using SetVelocity on players can result in no change and creating entities can result in collisions regardless of group, if either are done from inside the collision listener, but is there a crash exploit or some other critical issue the delay fixes? Or is it to guarantee a 1-tick delay so that players don't encounter and fix those aforementioned odd behaviors themselves?

@thegrb93
Copy link
Owner

thegrb93 commented Oct 7, 2024

This needs to be coalesced into a class. The current implementation is way too fragmented and messy.

@thegrb93
Copy link
Owner

thegrb93 commented Oct 7, 2024

Should also co-author @proton0va

@thegrb93
Copy link
Owner

thegrb93 commented Oct 7, 2024

I have time to class-ify it right now, so I can do that.

@thegrb93
Copy link
Owner

thegrb93 commented Oct 9, 2024

It's working now. Mind testing it out?

@legokidlogan
Copy link
Contributor Author

legokidlogan commented Oct 9, 2024

starfall/libs_sv/entities.lua:144: attempt to call method 'exists' (a nil value)
stack traceback:
	starfall/libs_sv/entities.lua:144: in function 'add'
	starfall/libs_sv/entities.lua:525: in function 'addCollisionListener'
	SF:main:14: in main chunk

@legokidlogan
Copy link
Contributor Author

Using these to test:

--@name 1
--@author
--@server

--prop.setPropClean( false )

local ent = prop.create( chip():getPos() + Vector( 0, 0, 50 ), Angle(), "models/hunter/blocks/cube025x025x025.mdl", false )
ent:setColor( Color( 255, 0, 0, 255 ) )

ent:addCollisionListener( function()
    print( "1 listener a" )
end, "a" )

ent:addCollisionListener( function()
    print( "1 listener b" )
end, "b" )

ent:addCollisionListener( function()
    print( "1 listener ?1" )
end )

ent:addCollisionListener( function()
    print( "1 listener ?2" )
end )


timer.simple( 1.5, function()
    print( "1 removed b" )

    ent:removeCollisionListener( "b" )
    ent:getPhysicsObject():setVelocity( Vector( 0, 0, 100 ) )
    
    timer.simple( 1.5, function()
        print( "1 removed all" )
    
        ent:removeCollisionListener()
        ent:getPhysicsObject():setVelocity( Vector( 0, 0, 100 ) )
        
        timer.simple( 1.5, function()
            print( "1 added a" )
        
            ent:addCollisionListener( function()
                print( "1 listener a" )
            end, "a" )

            ent:getPhysicsObject():setVelocity( Vector( 0, 0, 100 ) )
        end )
    end )
end )
--@name 2
--@author
--@server

local ent = find.byModel( "models/hunter/blocks/cube025x025x025.mdl" )[1]
ent:setColor( Color( 0, 255, 0, 255 ) )

ent:addCollisionListener( function()
    print( "2 listener a" )
end, "a" )

ent:addCollisionListener( function()
    print( "2 listener b" )
end, "b" )

ent:addCollisionListener( function()
    print( "2 listener ?1" )
end )

ent:addCollisionListener( function()
    print( "2 listener ?2" )
end )


timer.simple( 1.5, function()
    print( "2 removed b" )

    ent:removeCollisionListener( "b" )
    ent:getPhysicsObject():setVelocity( Vector( 0, 0, 100 ) )
    
    timer.simple( 1.5, function()
        print( "2 removed all" )
    
        ent:removeCollisionListener()
        ent:getPhysicsObject():setVelocity( Vector( 0, 0, 100 ) )
        
        timer.simple( 1.5, function()
            print( "2 added a" )
        
            ent:addCollisionListener( function()
                print( "2 listener a" )
            end, "a" )

            ent:getPhysicsObject():setVelocity( Vector( 0, 0, 100 ) )
        end )
    end )
end )

@thegrb93
Copy link
Owner

thegrb93 commented Oct 9, 2024

Did you 'git pull'? The exists function was added in sflib.lua

@legokidlogan
Copy link
Contributor Author

Yes, I did. You never defined exists in the listener metatable.

@legokidlogan
Copy link
Contributor Author

Never mind, the copy in my addons folder was out of date across the other files.

@legokidlogan
Copy link
Contributor Author

Everything seems to be working except for ent:removeCollisionListener() with no arguments. It correctly removes unnamed listeners, but not the named ones.

@legokidlogan
Copy link
Contributor Author

Also, adding an unnmaed listener will incorrectly override existing unnamed listeners, instead of adding to the list.

@thegrb93
Copy link
Owner

thegrb93 commented Oct 9, 2024

I changed the behavior so that calling without a name argument uses '""' instead adding/removing an array of listeners. This behavior is easier to manage.

@legokidlogan
Copy link
Contributor Author

Ah. Alright then.

@thegrb93 thegrb93 merged commit de11335 into thegrb93:master Oct 10, 2024
1 check passed
github-actions bot added a commit that referenced this pull request Oct 10, 2024
…ardless of old ent:PhysicsCollide() (#1868)

* Allow for multiple collision listeners

---------

Co-authored-by: thegrb93 <[email protected]>
Co-authored-by: UnusualOtter <[email protected]> de11335
@legokidlogan legokidlogan deleted the feature/multiple-collision-listeners branch October 10, 2024 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants