Page 1 of 1

No max function...?

Posted: Wed May 30, 2018 6:57 am
by arawra

Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File "..\addons\source-python\plugins\rpgo\rpgo.py", line 619, in _pre_take_damage
    lifesteal = max(1, damage * (attacker.getLevel() * .01))

UnboundLocalError: local variable 'max' referenced before assignment


Last section under attacker.getClass() == 'Warlock':

Syntax: Select all

@EntityPreHook(EntityCondition.is_bot_player, 'on_take_damage')
@EntityPreHook(EntityCondition.is_human_player, 'on_take_damage')
def _pre_take_damage(stack_data):
take_damage_info = make_object(TakeDamageInfo, stack_data[1])
attacker = Entity(take_damage_info.attacker)

if attacker.classname == 'player':
weapon = Weapon(take_damage_info.weapon)
if weapon.classname != 'worldspawn':


victim = make_object(Player, stack_data[0])
victim = rpgPlayer(victim.index)
attacker = rpgPlayer(attacker.index)


damage = take_damage_info.damage
p1 = victim.origin
p2 = attacker.origin
distance = math.sqrt(abs(p2[0]**2 - p1[0]**2) + abs(p2[1]**2 - p1[1]**2) + abs(p2[2]**2 - p1[2]**2))

# Only allow knife damage
#if weapon.classname != 'weapon_knife':
# return

# Increase the damage
#take_damage_info.damage += 100

# Change the type of damage
#take_damage_info.type = DamageTypes.BURN

# Add a damage type
#take_damage_info |= DamageTypes.POISON

if attacker.getClass() == 'Warrior':
take_damage_info.damage += damage * (CLASS_DAMAGE['Warrior bonus'] * attacker.getLevel())
if attacker.data.baseSpeed > 1.0:
if distance < CLASS_DAMAGE['Warrior charge distance']:
take_damage_info.damage += damage * CLASS_DAMAGE['Warrior charge']

if attacker.getClass() == 'Rogue':
if (abs(victim.get_view_angle()[1] - attacker.get_view_angle()[1]) < 45 or
abs((360- victim.get_view_angle()[1]) - attacker.get_view_angle()[1]) < 45 or
abs(victim.get_view_angle()[1] - (360 - attacker.get_view_angle()[1])) < 45):
if distance < CLASS_DAMAGE['Rogue sneak attack distance']:
take_damage_info.damage += damage * (CLASS_DAMAGE['Rogue sneak attack'] * attacker.getLevel())

if attacker.getClass() == 'Druid':
speed = attacker.velocity
if abs(speed[0]) > 300 or abs(speed[1]) > 300 or abs(speed[2]) > 300:
take_damage_info.damage += damage * (CLASS_DAMAGE['Druid pounce damage'] + float(attacker.getLevel() / 10))

if attacker.getClass() == 'Mage':
max = 100 * attacker.getLevel()
if(random.randint(1,100+1)) > 90:
for player in PlayerIter():
if not player.playerinfo.is_dead():
if player.playerinfo.team != attacker.playerinfo.team:
distance = player.origin - victim.origin
if abs(distance[0]) < max and abs(distance[1]) < max and abs(distance[2]) < max:
burnTimer = Repeat(burn, args=(attacker.index, player.index))
timers.append(burnTimer)
burnTimer.start(.1, attacker.getLevel()-1)
player.ignite_lifetime(.1)

if attacker.getClass() == 'Warlock':
lifesteal = max(1, damage * (attacker.getLevel() * .01))
message(lifesteal)
if weapon.classname in ['weapon_m3', 'weapon_xm1014']:
if random.randint(1,100) in range(1,13+1):
attacker.health += lifesteal
else:
attacker.health += lifesteal

Re: No max function...?

Posted: Wed May 30, 2018 7:19 am
by L'In20Cible
You are redefining it on that line:

Syntax: Select all

max = 100 * attacker.getLevel()

Use a different name for your variable.

Re: No max function...?

Posted: Thu Sep 03, 2020 9:37 am
by hlipperjohn
An UnboundLocalError is raised when a local variable is referenced before it has been assigned. In most cases this will occur when trying to modify a local variable before it is actually assigned within the local scope. Python doesn't have variable declarations, so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local.

Python has lexical scoping by default, which means that although an enclosed scope can access values in its enclosing scope, it cannot modify them (unless they're declared global with the global keyword). A closure binds values in the enclosing environment to names in the local environment. The local environment can then use the bound value, and even reassign that name to something else, but it can't modify the binding in the enclosing environment. UnboundLocalError happend because when python sees an assignment inside a function then it considers that variable as local variable and will not fetch its value from enclosing or global scope when we execute the function. However, to modify a global variable inside a function, you must use the global keyword.