1

I'm working on a emulator for Star Wars Galaxies and the scripting language that we're using is Jython. I'm having trouble figuring out how I define a variable outside the def. Here is the error..

UnboundLocalError: local variable 'actorFunds' referenced before assignment

And the code (cut out a lot that wasn't necessary... There is no class)

commandArgs = ""
commandLength = 0
globalTarget = SWGObject
globalActor = CreatureObject
tipAmount = 0
actorFunds = 0
totalLost = 0
bankSurcharge = 0

def run(core, actor, target, commandString):
    global globalTarget
    global globalActor
    global commandArgs
    global commandLength
    global tipAmount
    global bankSurcharge
    global actorFunds
    global totalLost

    globalTarget = target
    globalActor = actor
    commandArgs = commandString.split(" ")
    commandLength = len(commandArgs)
    tipAmount = commandArgs[0]
    bankSurcharge = int(0.05) * int(tipAmount)
    actorFunds = actor.getBankCredits()
    totalLost = int(tipAmount) + bankSurcharge

     .......
return

def handleBankTip(core, owner, eventType, returnList):

if eventType == 0:
    if int(totalLost) > globalActor.getBankCredits():
        globalActor.sendSystemMessage('You do not have ' + str(totalLost) + ' credits (surcharge included) to tip the desired amount to ' + globalTarget.getCustomName() + '.')
        return
    if int(tipAmount) > 0 and int(actorFunds) >= int(totalLost):
        return
return

When I get to actorFunds it provides the error...

I've searched for a long time and can't figure out why. I wish it was as simple as java with a "this"...

4

1 回答 1

0

将声明的变量放在等式的左侧。

def run(core, actor, target, commandString):

    target = globalTarget 
    actor = globalActor 

通过写作:

globalTarget = target

您正在为 globalTarget 分配目标值。而不是创建一个名为 target 的变量。

于 2013-07-28T22:48:24.023 回答