How to query sprites

Hello! I'm trying to figure out methods of collision detection (using LUA. I can't seem to find any examples of "querySpritesInRect". Can anyone tell me how this function is used? I'm not sure what return values I'm getting. The documentation says it should be a lit of sprites but I just keep getting nil.

Have you added collision rects to your sprites?

Thanks for the reply Matt! As a little more context, I currently have a player class that's extending the sprite class and uses the general collision from that.

I'm trying to add an ability where the player can change size. Before changing size I want to check if there's space to do so. I thought I could use the rect query to check for space. It generally works if I just check the size of the returned array but I'd like to know how to check the data in detail to know the collision type of what's returned since some objects are overlapable.

I was looking through the documentation but I couldn't find detail on what format the query data is returned in. It does say an array of sprites but I get nil if I try to print any elements of the array.

My backup plan is to create a second class with an invisible sprite that I use to check box collisions but I thought that might be less efficient.

What I meant is that you need to add collision rect to your sprite or it won't collide. Did you do this?

https://sdk.play.date/inside-playdate/#m-graphics.sprite.setCollideRect

Without this, querySpritesInRect will return nil.

Ahh, Yup I have done that! It isn't that the whole array is Nil, I just don't know what format the results are in. If I access an array element they return nil but I still get an array of length 2.

image

The reason you are getting nil here is because arrays (tables) in lua don't start at zero, but at one. So if you change it to hit[1] it should work

Oh jeez sorry about that! Turns out that was a bit of a dumb mistake on my part. Thank you very much Benzalo, that was it! Also thanks again, Matt!

2 Likes