## [Solved] Querying with a rectangle in Bump library

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
ilovelove2019
Citizen
Posts: 78
Joined: Wed Sep 11, 2019 10:38 am

### [Solved] Querying with a rectangle in Bump library

Hello everyone! It's me again! Let go to the main problem. I want to make some trees for my game. At first I could only make one tree grow straight, nothing. Then I plan to add leaves. After a while of thinking, I came up with this way. In the tree initialization function, I add the input parameter as leafNum. I divided the length of the tree into 16 parts and assigned the variable averageHeight. If leafNum == 2 then I will query 2 points (I use the coordinates self.x + self.width, averageHeight * 10 to make the tree and leaves look more balanced, this is the only part of the effect, let's comeback), similar to leafNum == 3, == 4, ... (in these cases I'll add the following code) I use the Bump library to check for collisions. I use a rectangular query. If within 2 places I query that, if there is no leaves and the number of leaves has not exceeded the limit then add more leaves (I also plan to make more time between sprays but I have only tested to see if what I have done is successful or not so I have not written down). I thought I would succeed but the problem started from here. Please take a look at this link to know my problem (https://github.com/kikito/bump.lua#quer ... -rectangle).

Code: Select all

function Plant:update(dt)
--[[local function checkOneFilter(item, other)
if other.isLeaf then return 'cross' end
end]]--

local items, len = world:queryRect(self.width + self.x, self.averageHeight * 10, 30, 30)
local count = world:countItems()
for i = 1, count do
local other = items[i].other
if other.isLeaf == false and self.totalLeaf < self.leafNum then
table.insert(listOfLeaf, Leaf(self.x + self.width, self.averageHeight * 10, 7.5, 4, 30, 16))
end
print("a")
end
........

At first, I followed the instructions from the link I just wrote above. I let loop i run from 1 to len (len is the length of items, items is a table). But when I do that, it goes like this, because the rectangle I query does not collide with any object, the items become an empty table, and in my opinion so len will be equal to 0, the loop do not run. I tried inserting print ("a") and it was true. Then I use another method, which is also in the other link, scroll down a bit and you'll see. I declare the local variable count as the number of all objects in the world and run the loop i from 1 to there. Then the error kept happening, it said Error: plant.lua: 30: attempt to index a nil value (in items .other) (I will attach my file below for your convenience). I used a guard (if items .other ~ = nil then ....) but it still says plant.lua: 30: attempt to index a nil value. I was bewildered not knowing what to do. I've made a little progress in making games, this project is better than my bad projects before, I don't want to have to stop here. Hope you could take some of your valuable time to help me. How to fix that? Is there a better way to create my leaves? Hope you can help me as soon as possible, I'm too eager to continue my work. Thank you very much. If you need any further information on the issue I'm having, just ask me and I'll give you more. This is my .love file. Specific problems in plant.lua.
Attachments
block2.love
Last edited by ilovelove2019 on Fri Oct 25, 2019 10:44 pm, edited 1 time in total.

pgimeno
Party member
Posts: 1946
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

### Re: Querying with a rectangle in Bump library

len is the length of the 'items' table. If len is 0, it means that 'items' is empty, and therefore items[i] is nil for any value of i. It basically means that your rectangle does not intersect any objects. Obviously, trying to access items[i].other will cause the error, because then you're doing the equivalent of (nil).other.

ilovelove2019
Citizen
Posts: 78
Joined: Wed Sep 11, 2019 10:38 am

### Re: Querying with a rectangle in Bump library

Oh. So do you have any alternative? A more optimal way to implement my ideas? Thank you for your interest.

pgimeno
Party member
Posts: 1946
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

### Re: Querying with a rectangle in Bump library

I'm not sure what you're after. If you want a method for limiting the maximum amount of leaves, what you can do is query the rectangle where leaves can grow, filtering (in the call to queryRect) only those objects that are leaves. Based on the returned length, you can choose whether to add leaves or not. For example (in pseudocode):

Code: Select all

-- Filter function that returns true only for items that are leaves
local function filterLeaves(item)
return item.isLeaf
end

...
local items, len = world:queryRect(rectangle where leaves can grow, filterLeaves)
if len < maxLeaves then
add a leaf somewhere in the rectangle where leaves can grow
end


ilovelove2019
Citizen
Posts: 78
Joined: Wed Sep 11, 2019 10:38 am

### Re: Querying with a rectangle in Bump library

Yay! Thank for your help! It so useful, I try and it works! You so professional! Thank you and thank you!

### Who is online

Users browsing this forum: No registered users and 7 guests