Hi, I want to be able to plant in the field areas (click the Pot button and only be able to place it in the fields not just anywhere on the screen), the Pot and Water buttons belong to the HUD scene, while fields belong to Garden scene, which HUD is instantiated in, I’m new to Godot so any tips is appreciated.
I already managed to achieve this effect but I feel like its not the right approach because I need to emit the mouse_entered and mouse_exited from each field individually into the Pot button script…
This project is as messy as it gets because I’m just doing everything in it, just to learn new stuff
Good job getting it to work. Any approach that works is the right approach when you’re starting out.
If I were to code this I’d probably check the Area2D only on button release (based on position) and check if it’s in a group that I’d call something like “ArableLand”. Than make sure that the shapes are in that group. That feels slightly easier to scale when you add new textures and content I think and you don’t need to wire signals back and forth.
I had 6 area2Ds each for a individual field, I removed all but one and added collisions of previous areas all under a single area2D, idk if this is the best approach but it’s definitely better than the one before
yeah honestly tried learning from gpt/deepseek and after couple of week I found myself amongst a pile of codes that didn’t even work, and I learned NOTHING, absolutely nothing, decided to go in blind and google/YT along the way, and it works way better
If you want to stick with this approach, you can DRY it up by moving the Area2D to a scene and write the code once (I’m assuming you have this code snippet repeated 6 times for each field). You’ll get a warning when you save it without a collision shape, but that’s fine here because we add it later so we can resize it. You can then instance a field scene, add a collision shape and you’re good to go.
If you ever change or expand on the field scene, it’ll be applied to all your fields.
I don’t have access to PC rn but I don’t think there’s actually any script attached to the Area2D node
I mean it shows there is but that’s probably the result of me trying everything and not cleaning up
That could be just an empty script lol
I’d like to know how you would approach it tho I really need to learn to code clean eventually
I’d make a new scene that’s called arable_zone. Just an Area2D without a collision shape. Add a script to the Area2D. Wire the mouse enter and exit signals to the Area2D. Put in the logic you have here, but emit a signal instead of changing the bool (because it’s declared outside the scope of Area2D). Rule of thumb here is to use signals when communicating with parents or siblings (and method calls or variable references down).
#wire signals pseudocode
signal arable
signal barren
#you can also wire the mouse entered and exited through code here if you prefer
func mouse_entered():
#emit arable signal
func mouese_exited():
#emit barren signal
The benefit here is that you can scale the number of arable land within your main scene without having to redo the signals and logic because you can just reuse this scene. You’ll still have to add a collision shape to each though.
Another benefit is that you can add new functionality to your arable land without having to change things up in the arable zone (seperation of concern).
I think it’s good practice for you if you wire the signals yourself and play around with this modular design, but feel free to reach out if you’re getting stuck somewhere.