Trouble with timers?

Please post any questions about developing your plugin here. Please use the search function before posting!
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Trouble with timers?

Postby arawra » 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.

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()
Last edited by arawra on Tue Aug 23, 2016 6:35 am, edited 1 time in total.
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Re: Trouble with timers?

Postby arawra » Tue Aug 23, 2016 6:10 am

Tested this with a bot in the server and it seemed to work ok...

Syntax: Select all

@ClientCommand('damage')
def cDamage(command, index):
if getClass(index) == 'Cleric':
playerMessage(index, 'before %s %s'%(dndPlayers[index]['shield'], time.strftime('%H:%M %S')))
damage = float(command.arg_string)
for player in PlayerIter('alive'):
if player.team != Player(index).team:
take_damage_info = TakeDamageInfo()
take_damage_info.attacker = player.index
take_damage_info.damage = damage
take_damage_info.type = DamageTypes.ENERGYBEAM
Player(index).on_take_damage(take_damage_info)
break
playerMessage(index, 'after %s %s'%(dndPlayers[index]['shield'], time.strftime('%H:%M %S')))


http://images.akamai.steamusercontent.c ... 0581FF80B/

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 34 guests