Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

With a game like farville, there is both processing during gameplay and when the user is offline (plants are growing, etc.)

So many of the elements on the game map depend on time.

Is there a trick to make this scale? How can you possibly calculate 1 million users maps being modified every x seconds?

Because some of these games to 'poke' you (like on the ipad) that things have changed, so it is not like they calculate everything if/when you login to the system.

share|improve this question

3 Answers 3

Let's take plants growing as an example.

Suppose you have a tomato plant that grows in 4 hours. While the player is in the game you're probably calculating it like this:

tomato_grow_time = 4 * 60 * 60;  /* 4 hours */

function update(seconds_since_last_draw) {
    tomato_plant.age += seconds_since_last_draw / tomato_grow_time;
    if (tomato_plant.age >= 1) {
       /* plant is finished growing */
    }
}

and you're calling update every frame. What would happen if you instead called it only when the player next logged in, instead of every x seconds? It'd still work, except the if condition would occur too late.

Instead of thinking of it in imperative terms, think of it as a function:

 function tomato_plant_age(time) {
      age = (time - tomato_plant.start_time) / tomato_grow_time;
 }

Now you can calculate when the age exceeds 1 (e.g. the plant is finished growing), using simple algebra:

 age = (time - start_time) / grow_time
 solve for time when age = 1
 ...
 time = (age * grow_time) + start_time

You don't really have to update every x seconds. You go through each of the plants and compute the time when they're finished growing. Take the min() of these to figure out the first plant that will trigger a notification. That's the only notification the app has to send out (unless your app repeatedly notifies, in which case you take all of them instead of the min() of them).

On Facebook, Farmville doesn't even notify you, so it's even simpler. They just update the plant ages when you next log in, or if someone visits your farm. If nobody looks at the farm, there's no update.

This approach only works when the updates are simple. It's one of the reasons these types of games tend to be very simple — it makes it easy to scale.

share|improve this answer
    
This is a great point. For notifications, you could use similar functions to query the database and return only records that have changed ("grown") since the last update. –  Richard Marskell - Drackir Feb 23 '12 at 19:58

Well they have many servers but also things have set times, so their progress is based on their creation time.

share|improve this answer

Are you talking about an online game that relies on a server to store the game state, or a game that runs on the users local machine?

Server Based Game

If you are talking about a server based job, then you would create something like a cron job that runs at some given time interval (e.g., every 5 minutes, 1 hour, or whatever your game design dictates). When the job runs, a piece of software iterates through all the active games, updating item states, sending messages as appropriate, etc.

Local Machine Based Game

If you are talking about a game running on the users local machine, you will not necessarily have the luxury of being able to have the game run in the background all the time.

If the local machine has some kind of alarm/calendar/alert ability (e.g., "Run this program at time X" or "Give player message at this time"), then you can calculate ahead of time when events (e.g. "pokes") should happen. For example, player plants a plant, plant will be ready in 8 hours. Program sets system alarm to message player 8 hours from now.

If the local machine doesn't have any kind of alarm/calendar/event system, then you will have to write your software to record when it was last run, and then whenever it is started, it can determine how much time has passed, and then can simulate the game world forward that amount of time before the player begins playing.

share|improve this answer
    
server based, not local. –  codecompleting Feb 23 '12 at 17:44
    
Right. Server based? Then have a cron job run a program task at some time interval dictated by your game design. That program analyzes the current game state, applies "growth" models based on your design, and sends out alerts as appropriate to your design. –  Tim Holt Feb 23 '12 at 18:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.