Do X every 4 turns.

Zubric
14 Feb 2020, 01:22is there a way to use a Turn script to trigger something every four turns without having to do something like
game.turn=game.turn+1
if game.turn=4 then script and of course resetting turns to 0 at the end of the script.
or is that the only way to do it?
In my example, it basically every 4 turns = a day to pass
mrangel
14 Feb 2020, 02:40I'd say that's the simplest way to do it. Although I'd probably use a modulo rather than resetting the count to zero.
Something like:
if (not HasInt (this, "turns")) this.turns = 0
this.turns = this.turns + 1
if (this.turns % 4 = 0) {
// code goes here
}
(I used this
rather than game
because it seems that the turn counter only matters to this particular turnscript. Putting attributes on the object they refer to makes your code more structured, easier for debugging)

Zubric
14 Feb 2020, 04:15Still confused how THIS works though. So bit confused how the code works. where do I add increase the turn number?
mrangel
14 Feb 2020, 14:07In a turnscript, this
refers to the turnscript itself. It's a convenient place to put attributes that won't be used elsewhere.
(The same applies to commands; this
can be used to store data related to the command)
Somehow I missed the +1 line; easy to make mistakes while typing on my phone. Sorry about that.