# Creating an update “Tick” on tiles for simulating growth of plants

In my 2D tile based platformer I have added plants to be able to farm such as the simplest, grass, to other things like corn. Now each of these speacial tiles must be updated to show how much it has grown (Simple states such as 0 = New, 1 = Mature, 2=Blooming)

My question is, what would be the best way to do this?

So far I belive I could:

1. Update every tile every few seconds, and if a random chance is met, make it grow
2. Choose a few tiles every frame to update (minecraft does this, same thing in 3D basicly)
3. ???

Now the timeframe for the updates would be something like, a flower blooms every game day (10 minutes in real life), grass spreads every in game hour, etc

Thanks for your help, I think I could do this, I'd just like a little advice if any of these are the right path.

-
Have a look at notch's minicraft 2D game source code. He does this with a few of the tiles ludumdare.com/compo/ludum-dare-22/?action=preview&uid=398 – Savlon Apr 7 '13 at 0:03
I never knew the source was availible for it, thanks for the link. – Cyral Apr 7 '13 at 2:48

## 3 Answers

For some types of objects, you can invert the logic.

Instead of every N ticks, grow by X, you can store the timestamp T1 since the last growth, and then when the player next sees that object at time T2, you can grow the plant by X * (T2 - T1) / N.

That way, any plants not on screen don't take any CPU to update. Note that this only works for things that don't have any effects outside of the plant growth (e.g. it won't work if plants affect neighboring tiles).

-
What type should I store the timestamp as? I need something small enough that wont get to large on all my tiles that need it. (Datetime?) – Cyral Apr 21 '13 at 1:36
It has to be granular enough to be able to represent the smallest simulation tick. Note that you can limit the range if you want to use a smaller integer type. For example if you set the beginning of the game to be 0 and you have 10 simulation ticks per second then a single game would have to run for 6 years before overflow. However unless space is a premium a 64 bit int or DateTime is probably fine :) – amitp Apr 27 '13 at 2:37
Okay thanks, Ill try this today. – Cyral Apr 27 '13 at 2:42
Works like a charm! – Cyral Apr 27 '13 at 20:47

Consider implementing a system that is designed to actively manage your tiles, taking the individual responsibility out of the tiles. The system would maintain its own timers relative to the game itself. A single tile manager would act as the central point for updating tiles based on your own constraints.

For instance, your tile manager could act as a singleton class, and processes "tile events" each frame. A tile event could be an abstract class that is extended to provide flexible behavior, and acts on a set of tiles passed in by the tile manager. This would decouple the tiles from the actual activity, making it easier to extend and maintain this system.

-

i dont know XNA, but you should think to use cooroutine\action instead putting all the code inside your update method (but i dont see your code, so maybe you already work in this way). Basically u should create a listener and when the action is called (for exemple, when a plant pass from his state "new" to "mature") starting the cooroutine.Also a behaviour tree should be pretty efficent in situations like this.

-