Tycoon game. Using "ticks" to progress through time and logic?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

Hi all,

I am making a tycoon game. I have a basic early part of my prototype up and am at the point where I’m about to start the real time aspect of the game.
In other words, all the pre conditions have been selected, added to lists etc and I’m ready to start the actual money making.

I’ve been reading a lot on the theory of tycoon games and there’s a general feeling that they work on “ticks” to move through time. At each tick a whole range of functions are performed by each entity in the system.

What do you think is the best way to acheive this in Godot? Should I be running a _process(delta) and then on each pass through do my processing? How is a tick defined? I’m really interested in this so I can progress as best I can without having to re-write everything in a month as I chose the wrong way to advance.

Thanks a tonne…

Tick is usually defined as an amount of time defined by who ever uses it(for example in minecraft the redstone works in ticks, where if you use a repeater you can define for how long it will hold - in ticks - it will hold the signal)… you can do so by making a timer of some sort, where every time it exceeds the time limit it will call a function/do all it needs to do and will reset itself

rustyStriker | 2017-11-28 11:58

:bust_in_silhouette: Reply From: Michael Paul

As Rusty commented, a tick is a short, but abstract period of time. For turn-based games, you need not worry much about it. For example, as soon as the player has made his or her choices and ends his/her turn, the AI will make its moves as quickly as possible(except maybe some real-time animations) and then hand control back to the player for the next turn. An RTS game in contrast (like your Tycoon game), will rely on real-time passage for continuous moves to happen at a constant rate. It’s important to keep a real-time clock for 2 reasons:

  1. Not all CPUs/OS run at the same speed, and
  2. As more units come into play, it will take longer to perform the needed functions for all the entities.

It’s easy enough to use delta to do this. Here is a simple example using a 2 second timer. That is probably longer than you would choose in practice, but it will demonstrate better.

extends Node2D

var my_timer = 2 # set to 2 seconds

func _process(delta):
	my_timer -= delta # take the time passed and subtract from our timer
    if my_timer <= 0: # 2 seconds have gone by... do stuff
    	print ("Tick") # just to see that it prints every 2 seconds
    	my_timer = 2  # reset timer for next time

func _ready():
	set_process(true)

Great description, Michael.

Ertain | 2017-11-28 20:45

Note that you can use the built-in Timer node for this as well.

func _ready():
    var timer = Timer.new()
    add_child(timer)
    timer.set_wait_time(2)
    timer.connect("timeout", self, "_on_Timer_timeout")
    timer.start()

func _on_Timer_timeout():
    print("Tick")

kidscancode | 2017-11-28 21:32

In your example, adding 2 to the timer variable instead of resetting it will give better timing accuracy.

func _process(delta):
    my_timer -= delta
    if my_timer <= 0:
        my_timer += 2
#my_timer will most likely be slightly off from 0 (ex. -0.01)
#
#if my_timer is set to 2 then the slight amount of "drift" will be lost
#causing the next tick to come ~0.01 seconds later than expected
#
#adding 2 instead causes the drift to be retained and compensated for

clarence112 | 2023-04-04 00:32