A farm crop usually picks its image from how far it has grown. With 4 images (seed, sprout, young, ripe) over 60 seconds, int(progress * 4) showed the ripe image 15 s before the crop could be harvested in our Godot 4.7 test, and roundi(progress * 3) 10 s before. int(progress * 3) was on time, but its index ran past the last image 20 s after the crop was ready, because growing kept counting while it waited. Show ripe only when the crop can be harvested: 0 s early, never past the last image.
The code
Set the crop's texture to images[crop_stage(grown, grow_time, images.size())], and let the player harvest when grown >= grow_time, the same test the script uses for the ripe image.
extends Node
## Which growth image a crop shows. The ripe image shows only when the crop can
## really be harvested, and every growing image gets the same share of the time.
static func crop_stage(grown: float, grow_time: float, stages: int) -> int:
if grown >= grow_time: # the same test as "can harvest"
return stages - 1
var growing := stages - 1 # images before ripe
return mini(int(grown / grow_time * growing), growing - 1)
What we measured
One crop with 60 s to grow and 4 images at 60 fps, left in the ground until 120 s. Harvest became possible at 60.02 s, one frame late, because 3,600 summed frame times land just under 60. Two runs with the same numbers.
int(progress * 4): ripe image 15 s before harvest, about 15 s for each image, index 4 at 60.02 s.roundi(progress * 3): ripe image 10 s early, the images shown for 10, 20, 20 and 10 s, index 4 at 70.02 s.int(progress * 3): ripe image on time, 20 s for each growing image, index 4 at 80.02 s.- The script: ripe image on time, 20 s for each growing image, never past index 3 in 120 s.
Index 4 is one past the last of 4 images, an out of bounds index on an Array of 4 textures.
Why it happens
The ripe image and the harvest check use different numbers. A formula that rounds up, or multiplies by the full number of images, reaches the last image before progress gets to 1. And progress keeps going past 1 while the crop waits to be picked, so even a formula that is right at 1 runs off the end later.
What to change
- A sprinkler or area spell that should cover a round patch of tiles: tiles within the radius plus half a tile.
- Keeping
grownat mostgrow_timestops the index running past the end, but the first two formulas still show ripe too early. - Save grown (or the time it was planted), not the image number, and the right image comes back on load.
Why a summed timer lands a frame late: a timer that never fires on time. The harvest pop itself: pop an item out in an arc.
Ready-made harvest effects: Farming & Harvest VFX for Godot 4 has 23 ready-made farm effects in Stylized and Retro PSX looks, including harvest bursts for carrots, berries and wheat, a Ready to Harvest sparkle and a Giant Crop reward.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Farming & Harvest VFX for Godot 4 has 23 ready-made farm effects in Stylized and Retro PSX looks, including harvest bursts for carrots, berries and wheat, a Ready to Harvest sparkle and a Giant Crop reward.
More how-tos for Farming & Harvest VFX for Godot 4: Make a harvested crop pop out in an arc
More how-tos: Game systems
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene