Trouble with timers?
Posted: Tue Aug 23, 2016 5:52 am
Disclaimer: Yes, this code is not optimized and needs to be rewritten
I'm having trouble getting a functional shield that recharges out of combat, similar to a shield from Halo (XBox). It appears that it is constantly recharging, and I do not want that to happen. The message does tell me that the timer was stopped, meaning the timer was definitely found in the 'combat timers' key.
I'm having trouble getting a functional shield that recharges out of combat, similar to a shield from Halo (XBox). It appears that it is constantly recharging, and I do not want that to happen. The message does tell me that the timer was stopped, meaning the timer was definitely found in the 'combat timers' key.
Syntax: Select all
@Event('player_spawn')
def player_spawn(ev):
index = index_from_userid(ev['userid'])
if getClass(index) == 'Cleric':
#level is between 1-20
shield = getLevel(index) * 3 + 15
dndPlayers[index]['max shield'] = shield
dndPlayers[index]['shield'] = shield
playerMessage(index, 'Your shield will block %s damage and recharges out of combat'%shield)
shieldDelay = TickRepeat(changeShield, index, getLevel(index) * 1.5)
shieldDelay.start(1,0)
dndPlayers[index]['combat timers'].append(shieldDelay)
def restartCombatTimers(index):
for x in dndPlayers[index]['combat timers']:
x.resume()
@Event('player_hurt')
def player_hurt(ev):
if ev['attacker'] != ev['userid'] and ev['attacker'] != 0:
vindex = index_from_userid(ev['userid'])
aindex = index_from_userid(ev['attacker'])
victim = Player(vindex)
attacker = Player(aindex)
damage = int(ev['dmg_health'])
addPlayer(vindex)
addPlayer(aindex)
if victim.team != attacker.team:
for index in [vindex, aindex]:
if dndPlayers[index]['combat']:
if dndPlayers[index]['combat'].running:
dndPlayers[index]['combat'].cancel()
for x in dndPlayers[index]['combat timers']:
x.pause()
if getClass(index) == 'Cleric':
playerMessage(index, 'Timer was stopped')
combatTimer = Delay(3, restartCombatTimers, index)
dndPlayers[index]['combat'] = combatTimer
if dndPlayers[vindex]['shield'] > 0:
if dndPlayers[vindex]['shield'] >= damage:
dndPlayers[vindex]['shield'] -= damage
victim.health += damage
playerMessage(vindex, 'Your shield blocked %s damage'%damage)
else:
victim.health += dndPlayers[vindex]['shield']
playerMessage(vindex, 'Your shield blocked %s damage and is now depeleted'%dndPlayers[vindex]['shield'])
dndPlayers[vindex]['shield'] = 0
def changeShield(index, amount):
if not Player(index).dead:
dndPlayers[index]['shield'] += amount
if dndPlayers[index]['shield'] > dndPlayers[index]['max shield']:
dndPlayers[index]['shield'] = dndPlayers[index]['max shield']
@Event('round_end')
def round_end(ev):
stopTimers()
def stopTimers():
for x in globalTimers:
x.stop()
for x in dndPlayers:
for y in dndPlayers[x]['timers']:
y.stop()
if dndPlayers[x]['combat']:
if dndPlayers[x]['combat'].running:
dndPlayers[x]['combat'].cancel()
for y in dndPlayers[x]['combat timers']:
y.stop()