Advertisement

how to do a building timer?

Started by April 22, 2017 03:22 AM
11 comments, last by Alberth 7 years, 5 months ago

Let me make the question simpler.

Since I don't seem adequately familiar with developing online multiplayer stuff does anyone know a beginners tutorial for networking? Possibly for smartfoxserver(?) + unity + mysql(?).

I tried checking the networking forums but the sticky just has a broken link...

What many people do is a variation on 1. They check every X time units (could even be once a second), but they don't really check. To understand, let's say we have 2 armies, one is due in 4 units of time, and one in 7 units of time.

So we check now, and notice that one army is due in 4 units of time, and one in 7 units of time. Nothing to do, let's wait one time unit.

We check again after 1 time unit, and notice that one army is due in 3 units of time, and one in 6 units of time. Nothing to do, let's wait one time unit.

We check again after 1 time unit, and notice that one army is due in 2 units of time, and one in 5 units of time. Nothing to do.

 

What you see that after each time we wait, all armies arrive 1 time unit sooner. This is of course obvious, while we waited, 1 time unit passed! Since for each army, the amount of passed time is the same, it means that the second army (with 5 units to go) will not arrive before the first army (with 2 remaining time units). In other words, we don't need to check all armies, just the one with the smallest remaining time is sufficient.

A second thing to note is that for the first army, 2 units of time remain. We don't need to check it each time, we can also find that number once, and then remember it. Instead of checking the army, we just decrement the remembered number. If it's not 0, the army hasn't arrived, and we wait one more time unit.

Alternatively, instead of repeatedly waiting a single time unit, we can wait also wait "number" time units, doing nothing (since we know nothing is going to happen until the army arrives). After that amount of time, one army arrived (or more, if several armies arrive at the same time), and there is interesting stuff to do. In our example, we'd wait 2 time units, and then we know an army has arrived. We scan the list armies to find them again, and deal with it. (If you keep them sorted on arrival time, it's even simpler.)

After handling the arrived armies, you again scan all armies that still have not arrived, and find the next soonest arriving army, you wait for it to arrive, and  ..., etc etc.

This topic is closed to new replies.

Advertisement