#Planting System

1 messages · Page 1 of 1 (latest)

copper falcon
#

Hi, I am trying to create a garden simulator and i'm stuck on a problem, I have a basic point and click planting system but I don't know how to prevent from these plants from stacking ontop of eachother. Here are the photos of my basic setup:

south tide
#

you could check whether currentseed is null and return if its not prior to the planting logic

golden walrus
#

If raycast is hitting a plant return

copper falcon
#

How would I do any of that

copper falcon
#

Sorry I’m kinda new

golden walrus
#

Ask chatgpt brother, use the tool to your disposal

copper falcon
#

I guess but i want to learn how to do it alone first

trail wharf
#

Please refrain from doing this again

copper falcon
#

ok but how do i do this 😭

sonic drift
golden walrus
#

Oh sht sorry didn't know

copper falcon
#

and when I click, i check the cords of the other plants within the list and if its like 10 pixels away from the plant it will be placed?

sonic drift
copper falcon
#

its not on a grid based system so its basically how many you can fit

sonic drift
#

aah, in that case you may want to make the plants themselves be an area, and when you place down the plant, cancel the attempt if it results in an overlap with another plant.

copper falcon
#

so an area2d?

sonic drift
#

Yeah.
There are other ways to check for overlap, but that's the most automatic kind.

copper falcon
#

ok last question what would be the method for that

#

as the plot itself is an area2D

sonic drift
# copper falcon as the plot itself is an area2D

Basically when the plant is placed down. You can use get_overlapping_areas() on _ready() to get an Array of all other areas it overlaps with.
Then check if any of them are plants. If they are, delete the plant and report that the placement failed.

#

You could also check BEFORE placing the plant trough other methods. But this saves you from creating new objects or using PhysicsServer methods.

copper falcon
#

Ok thanks!

copper falcon
#

Im doing it like so

#

var placeable = NewPlant.get_overlapping_areas()

sonic drift
copper falcon
#

oh

sonic drift
#

That is why i am saying that you have to place it first and then make the plant delete itself.

If you where to do a different check, from the plot for example, then you could prevent the placement altogheter. But it would be a bit more complicated.

copper falcon
#

so I would have to create a seperate script?

#

oh ok I see now

#

so is there any way you could communicate the destruction of the new plant back to the plot script

sonic drift
copper falcon
#

later on I would like to limit the amount of plants

#

so if the plant is destroyed i want the plant to be refunded

sonic drift
#

When creating the plant, you can connect it like so:

  NewPlant.cancelled.connect(refund)
  add_child(NewPlant)


func refund():
  plants_remaining += 1
copper falcon
#

and I assume I should make a tag for plants

#

right

sonic drift
#

You can either "tag" them with a group, or just check for their class (in this case i am assuming your plant script has class_name Plant in it, and using the is operator to check that)

copper falcon
copper falcon
#

after debugging i think there is something wrong with this piece of code

#
        
        print_debug(area)
        
        if area is Plant:
            cancelled.emit()
            queue_free()
            return```
sonic drift
copper falcon
#

btw its inside the _onready() function and it seems to not get past the "for area" part

sonic drift
copper falcon
#

i meant _ready() sorry

#

here is the script if you need it

sonic drift
#

it must mean that it did not find anything during ready.
Or that none of what it found was a Plant

If you use breakpoints (just type breakpoint in the line in which you wish to stop) you can check the tree and the relevant values while the game is frozen.
You can then navigate with these buttons

copper falcon
#

Okay so what do I check for

copper falcon
#

Nvm I just figured it out, the ready function was somehow happening before the node was added as a child in the tree

#

Even when I reorganize the code for the Placement check to happen

#

I still get 0 results for overlaping areas

#

contrary to when I put overlapping layers into proccess i get the desired output

#

please help i have no idea what is happening

copper falcon
#

please someone help me

sonic drift
# copper falcon please someone help me

Try enabling Project>Visible Collision Shapes and use breakpoints to better check what is running when.

I at least can't help much more with this context.

leaden crane
#

Make a signal that will be emitted only after the get_overlapping_areas() function is finished
Before validate_placement() function, add await function, so that the validation_placement() function will run only after get_overlapping_areas() function is finished

copper falcon
#

I created a new signal called "Resume"

leaden crane
copper falcon
#

I followed what you said and it still doesn't work

#

for somereason the area function still outputs "[]"

leaden crane
#

Is the get_overlapping_areas() function working properly?

copper falcon
leaden crane
#

So get_overlapping_areas() doesn't work in validate_placement()?

copper falcon
#

No it returns an empty array

#

idk why its called after the node is put inside the node tree

leaden crane
#

Try using breakpoints or print statements to determine which functions run first
I don't think I can be of much help other than that

copper falcon
#

when get_overlapping_areas() is called inside the validate_placement() function

#

it doesn't appear on the scene despite being inside the node tree

leaden crane
#

What doesn't appear on the scene?

copper falcon
#

The seed scene doesn't appear on the game screen

#

its not hidden and without the breakpoint after get_overlapping_areas() it appears no problem

leaden crane
#

Your problem may be related to that then

copper falcon
#

Alright ill try to debug further

copper falcon
#

I have no idea what is going on

#

does get_overlapping_areas() only work when the node is rendered

warped epoch
#

Did you mess with masks or layers any?

copper falcon
#

there is something wrong with the way I'm calling the function atleast I think

#

Ok so I figured out the problem

#

get_overlapping_layers occurs during a physic update while _ready() and _on_input_event() runs before the physics update

#

but how do I fix this?

leaden crane
#

Maybe call it from somewhere else?

#

Or put
"await ready"
so that, it will continue only after ready function is run

sonic drift
#

i think this got a bit too out of hand.
It is probably easier to just use global variables to keep track of everything important and use that for checks and stuff.