Skip to content

Commit

Permalink
Rename Utils.create_timer()
Browse files Browse the repository at this point in the history
Update comment
  • Loading branch information
Kvel2D committed Nov 30, 2024
1 parent 801dbaf commit b68d5ba
Show file tree
Hide file tree
Showing 32 changed files with 64 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/creeps/special_buffs/creep_broody.gd
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func on_autocast(event: Event):
# while waiting.
for i in range(0, 5):
var sleep_time: float = EGG_GROW_TIME / 6
await Utils.create_timer(sleep_time, self).timeout
await Utils.create_manual_timer(sleep_time, self).timeout

var egg_grow_progress: float = i / 6.0
var egg_scale: float = EGG_SCALE_MIN + egg_grow_progress * (EGG_SCALE_MAX - EGG_SCALE_MIN)
Expand Down
2 changes: 1 addition & 1 deletion src/creeps/special_buffs/creep_necromancer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func necromancer_aura_bt_on_death(event: Event):

var necromancer: Creep = buff.get_caster()

await Utils.create_timer(RAISE_DELAY, self).timeout
await Utils.create_manual_timer(RAISE_DELAY, self).timeout

if !Utils.unit_is_valid(necromancer):
return
Expand Down
2 changes: 1 addition & 1 deletion src/game_scene/chat_commands.gd
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ func _command_print_ranges_to_towers(player: Player):
for message in message_list:
_add_status(player, message)

await Utils.create_timer(1.0, self).timeout
await Utils.create_manual_timer(1.0, self).timeout


func _command_check_range_helper(player: Player, args: Array, friendly: bool):
Expand Down
2 changes: 1 addition & 1 deletion src/items/item.gd
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func fly_to_stash(_mystery_float: float):
# NOTE: fly duration has to be a constant value, doesn't
# matter if fly animation will finish earlier. This is to
# prevent multiplayer desync.
await Utils.create_timer(FLY_DURATION, self).timeout
await Utils.create_manual_timer(FLY_DURATION, self).timeout

# After item is done flying, we can delete item drop
parent_item_drop.remove_child(self)
Expand Down
2 changes: 1 addition & 1 deletion src/items/item_behaviors/chameleon_glaive.gd
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func on_attack(event: Event):

CombatLog.log_item_ability(item, null, "Launch Glaive!")

await Utils.create_timer(0.1, self).timeout
await Utils.create_manual_timer(0.1, self).timeout

if !Utils.unit_is_valid(tower) || !Utils.unit_is_valid(creep):
return
Expand Down
2 changes: 1 addition & 1 deletion src/items/item_behaviors/chrono_jumper.gd
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func jumper_bt_on_cleanup(_event: Event):

# NOTE: need to call get_tree() on tower because item is
# outside tree during CLEANUP callback
await Utils.create_timer(0.1, self).timeout
await Utils.create_manual_timer(0.1, self).timeout

if !Utils.unit_is_valid(tower):
return
Expand Down
2 changes: 1 addition & 1 deletion src/items/item_behaviors/distorted_idol.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func on_pickup():
if !carrier_is_on_corner:
player.display_floating_text("Distorted Idol carrier must be on corner!", carrier, Color.PURPLE)

await Utils.create_timer(0.1, self).timeout
await Utils.create_manual_timer(0.1, self).timeout

item.drop()
item.fly_to_stash(0.0)
Expand Down
4 changes: 2 additions & 2 deletions src/items/item_behaviors/scroll_of_speed.gd
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func on_autocast(_event: Event):
item.user_int = item.user_int - 1

item.set_charges(item.user_int)
await Utils.create_timer(0.1, self).timeout
await Utils.create_manual_timer(0.1, self).timeout
item.set_charges(item.user_int)


Expand All @@ -85,5 +85,5 @@ func periodic(_event: Event):
item.user_int = 10

item.set_charges(item.user_int)
await Utils.create_timer(0.1, self).timeout
await Utils.create_manual_timer(0.1, self).timeout
item.set_charges(item.user_int)
4 changes: 2 additions & 2 deletions src/items/item_behaviors/scroll_of_strength.gd
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func on_autocast(_event: Event):
item.user_int = item.user_int - 1

item.set_charges(item.user_int)
await Utils.create_timer(0.1, self).timeout
await Utils.create_manual_timer(0.1, self).timeout
item.set_charges(item.user_int)


Expand All @@ -85,5 +85,5 @@ func periodic(_event: Event):
item.user_int = 10

item.set_charges(item.user_int)
await Utils.create_timer(0.1, self).timeout
await Utils.create_manual_timer(0.1, self).timeout
item.set_charges(item.user_int)
2 changes: 1 addition & 1 deletion src/player/team.gd
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func _do_game_win():
Effect.set_color(effect, color)
Effect.set_animation_speed(effect, speed)

await Utils.create_timer(0.5, self).timeout
await Utils.create_manual_timer(0.5, self).timeout


func _do_game_lose():
Expand Down
2 changes: 1 addition & 1 deletion src/singletons/effect.gd
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func set_lifetime(effect_id: int, lifetime: float):

set_auto_destroy_enabled(effect_id, false)

var timer: ManualTimer = Utils.create_timer(lifetime, self)
var timer: ManualTimer = Utils.create_manual_timer(lifetime, self)
timer.timeout.connect(_on_lifetime_timer_timeout.bind(effect_id))


Expand Down
37 changes: 28 additions & 9 deletions src/singletons/utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,35 @@ func convert_time_to_string(time_total_seconds: float):
return time_string


# NOTE: you must use this instead of
# get_tree().create_timer() because timers created using
# get_tree().create_timer() do not handle game pause and
# game restart.
# NOTE: you MUST use create_manual_timer() instead of
# get_tree().create_timer() for gameplay code.
#
# NOTE: you must not use this for things which are not part
# of the synchronized multiplayer client. If you
# create_timer() for one player but not the others, you will
# mess up the order of updating timers and cause desync.
func create_timer(duration: float, parent: Node) -> ManualTimer:
# - create_timer() uses Godot Timer class which runs based
# on real life time.
# - create_manual_timer() uses ManualTimer runs based on
# game time which takes into account game pause and
# adjusting game speed.
#
# Using Godot Timers in gameplay code also causes desyncs
# in multiplayer.
#
# Example: if you were to mistakenly use Godot Timer from
# create_timer() to add delay to a tower spell, then the
# spell would not function as expected and would cause
# desyncs in multiplayer.
#
# NOTE: another caveat is that you MUST NOT use
# create_manual_timer() for things which are not part of the
# synchronized multiplayer client. If you
# create_manual_timer() for one player but not the others,
# you will mess up the order of updating timers and cause a
# multiplayer desync.
# - Use create_manual_timer() only for code which runs for
# all players in multiplayer.
# - Use create_timer() for UI and visual code. Also for code
# which specifically doesn't run in multiplayer, for
# example title screen code.
func create_manual_timer(duration: float, parent: Node) -> ManualTimer:
var timer: ManualTimer = ManualTimer.new()

var parent_is_active: bool = parent.is_inside_tree() && !parent.is_queued_for_deletion()
Expand Down
2 changes: 1 addition & 1 deletion src/tests/playtest_bot.gd
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static func run(build_space: BuildSpace):
while true:
run_cycle()

await Utils.create_timer(TIME_PER_SET, build_space).timeout
await Utils.create_manual_timer(TIME_PER_SET, build_space).timeout


static func run_cycle():
Expand Down
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/afflicted_obelisk.gd
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func on_damage(event: Event):
parasite_bt.apply_custom_timed(tower, target, 0, 3.0 / tower.get_prop_buff_duration())
target.modify_property(Modification.Type.MOD_DMG_FROM_NATURE, _stats.vuln_value + level * _stats.vuln_value_add)

await Utils.create_timer(3.0, self).timeout
await Utils.create_manual_timer(3.0, self).timeout

if Utils.unit_is_valid(target):
tower.do_custom_attack_damage(target, tower.get_current_attack_damage_with_bonus(), tower.calc_attack_multicrit(0, 0, 0), AttackType.enm.DECAY)
Expand Down
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/cloud_warrior.gd
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func on_damage(event: Event):
else:
tower.user_int = 0

await Utils.create_timer(0.4, self).timeout
await Utils.create_manual_timer(0.4, self).timeout

if tower.user_int == 1 && Utils.unit_is_valid(creep):
CombatLog.log_ability(tower, creep, "Lightning Strike")
Expand Down
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/garden_of_eden.gd
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func on_autocast(_event: Event):
Effect.set_lifetime(boom, 2.0)
Effect.set_color(boom, effect_color)

await Utils.create_timer(0.5, self).timeout
await Utils.create_manual_timer(0.5, self).timeout

if Utils.unit_is_valid(tower):
var aoe_damage: float = 15 * lifeforce_stored * current_spawn_level
Expand Down
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/gryphon_rider.gd
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,4 @@ func line_damage(origin_pos: Vector2, direction: float):

damage *= dmg_multiplier
i += 1
await Utils.create_timer(0.15, self).timeout
await Utils.create_manual_timer(0.15, self).timeout
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/healing_obelisk.gd
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func on_damage(event: Event):
if loop_count == 0:
break

await Utils.create_timer(1.0, self).timeout
await Utils.create_manual_timer(1.0, self).timeout

if Utils.unit_is_valid(tower) && Utils.unit_is_valid(target):
target.set_health(target.get_health() + healing)
Expand Down
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/helicopter_zone.gd
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func on_damage(event: Event):
func on_autocast(event: Event):
var target: Unit = event.get_target()

await Utils.create_timer(1.0, self).timeout
await Utils.create_manual_timer(1.0, self).timeout

if !Utils.unit_is_valid(tower) || !Utils.unit_is_valid(target):
return
Expand Down
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/icy_spirit.gd
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ func on_attack(_event: Event):

slow_bt.apply(tower, aoe_target, level)

await Utils.create_timer(0.1, self).timeout
await Utils.create_manual_timer(0.1, self).timeout
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/plagued_crypt.gd
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func periodic(_event: Event):
buff.set_displayed_stacks(buff.get_level())

var stack_duration: float = (20.0 + 0.4 * tower.get_level()) * tower.get_prop_buff_duration()
await Utils.create_timer(stack_duration, self).timeout
await Utils.create_manual_timer(stack_duration, self).timeout

# NOTE: after sleep
if !Utils.unit_is_valid(tower):
Expand Down
4 changes: 2 additions & 2 deletions src/towers/tower_behaviors/planar_gate.gd
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ func on_autocast(_event: Event):
var effect1: int = Effect.create_colored("res://src/effects/voodoo_aura.tscn", effect_pos, 0, 1, Color8(1, 255, 255, 255))
Effect.set_z_index(effect1, Effect.Z_INDEX_BELOW_TOWERS)

await Utils.create_timer(0.3, self).timeout
await Utils.create_manual_timer(0.3, self).timeout

var effect2: int = Effect.create_colored("res://src/effects/voodoo_aura.tscn", effect_pos, 0, 2, Color8(1, 255, 255, 255))
Effect.set_z_index(effect2, Effect.Z_INDEX_BELOW_TOWERS)

await Utils.create_timer(0.3, self).timeout
await Utils.create_manual_timer(0.3, self).timeout

var effect3: int = Effect.create_colored("res://src/effects/voodoo_aura.tscn", effect_pos, 0, 3, Color8(1, 255, 255, 255))
Effect.set_z_index(effect3, Effect.Z_INDEX_BELOW_TOWERS)
Expand Down
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/portal_to_swine_purgatory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func do_rampage_of_pigs(target: Creep, target_pos_3d: Vector3, target_is_air, re
remaining_pig_count -= 1

if remaining_pig_count > 0:
await Utils.create_timer(0.25, self).timeout
await Utils.create_manual_timer(0.25, self).timeout

var update_target_pos_3d: Vector3
if Utils.unit_is_valid(target):
Expand Down
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/princess_of_light.gd
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func on_spell_target(event: Event):
buff.set_level(buff.get_level() + buff_level)
buff.set_displayed_stacks(buff.user_int)

await Utils.create_timer(stack_duration, self).timeout
await Utils.create_manual_timer(stack_duration, self).timeout

if Utils.unit_is_valid(tower):
buff = tower.get_buff_of_type(channel_bt)
Expand Down
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/rundown_iron_sentry.gd
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func on_unit_in_range(event: Event):
var alert_duration: float = _stats.alert_duration
alert_bt.apply_custom_timed(tower, next, 0, alert_duration)

await Utils.create_timer(_stats.awareness_duration, self).timeout
await Utils.create_manual_timer(_stats.awareness_duration, self).timeout

if Utils.unit_is_valid(tower):
tower.modify_property(Modification.Type.MOD_DAMAGE_BASE_PERC, -mod_damage_value)
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/scales.gd
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func on_autocast(_event: Event):

lightmare_is_active = true

await Utils.create_timer(10.0, self).timeout
await Utils.create_manual_timer(10.0, self).timeout

lightmare_is_active = false

Expand Down
4 changes: 2 additions & 2 deletions src/towers/tower_behaviors/spell_collector.gd
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func on_attack(event: Event):
projectile.user_real = missile_crit_chance * missile_number
projectile.user_real2 = missile_crit_dmg * missile_number

await Utils.create_timer(delay_between_missiles, self).timeout
await Utils.create_manual_timer(delay_between_missiles, self).timeout


func on_tower_details() -> MultiboardValues:
Expand Down Expand Up @@ -169,7 +169,7 @@ func spell_gathering_bt_on_spell_casted(event: Event):
var autocast: Autocast = event.get_autocast_type()
var autocast_cooldown: float = autocast.get_cooldown()

await Utils.create_timer(autocast_cooldown, self).timeout
await Utils.create_manual_timer(autocast_cooldown, self).timeout

if Utils.unit_is_valid(caster):
stacks_buff = caster.get_buff_of_type(missile_stacks_bt)
Expand Down
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/the_omnislasher.gd
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func on_attack(event: Event):
p.set_speed(5000)

for i in range(0, attack_count):
await Utils.create_timer(time_between_attacks, self).timeout
await Utils.create_manual_timer(time_between_attacks, self).timeout

if !Utils.unit_is_valid(tower):
break
Expand Down
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/timevault.gd
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func on_damage(event: Event):
var effect: int = Effect.create_simple_at_unit_attached("res://src/effects/mass_teleport_caster.tscn", creep, Unit.BodyPart.ORIGIN)
Effect.set_auto_destroy_enabled(effect, false)

await Utils.create_timer(3.0, self).timeout
await Utils.create_manual_timer(3.0, self).timeout

Effect.destroy_effect(effect)

Expand Down
4 changes: 2 additions & 2 deletions src/towers/tower_behaviors/tiny_storm_lantern.gd
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func new_attack(num_shots: int, creep: Creep):
var it: Iterate = Iterate.over_units_in_range_of_unit(tower, TargetType.new(TargetType.CREEPS), creep, 300)
var next: Creep

await Utils.create_timer(0.2, self).timeout
await Utils.create_manual_timer(0.2, self).timeout

next = it.next_random()

Expand All @@ -70,7 +70,7 @@ func new_attack(num_shots: int, creep: Creep):
if num_shots == 0:
return

await Utils.create_timer(0.2, self).timeout
await Utils.create_manual_timer(0.2, self).timeout


func tower_init():
Expand Down
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/village_witch.gd
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func on_damage(event: Event):

soul_split_buff.set_displayed_stacks(current_soul_split_stacks)

await Utils.create_timer(10.0 * multiplier, self).timeout
await Utils.create_manual_timer(10.0 * multiplier, self).timeout

if Utils.unit_is_valid(tower):
tower.modify_property(Modification.Type.MOD_ATTACKSPEED, -_stats.mod_attack_speed * multiplier)
Expand Down
2 changes: 1 addition & 1 deletion src/towers/tower_behaviors/wild_warbeast.gd
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func on_attack(event: Event):

var devour_stack_duration: float = 6.0 * tower.get_prop_buff_duration()

await Utils.create_timer(devour_stack_duration, self).timeout
await Utils.create_manual_timer(devour_stack_duration, self).timeout

if !Utils.unit_is_valid(tower):
return
Expand Down

0 comments on commit b68d5ba

Please sign in to comment.