初めての投稿なのでお手柔らかにお願いします。もちろん、簡単ではありません;)
とにかく、私は Python 3.2.3 を使ってテキスト アドベンチャー ゲームを書いています。Actor の GameItem のサブクラスを含む Location オブジェクトのコレクションを保持するクラス Labyrinth があります。
ターン中、Labyrinth は、Labyrinth.doTurn() によって実行されるパーサー Command オブジェクトの内容をポーリングする各 Locations をポーリングします。通常、コマンドを実行すると、実行中のオブジェクトのアクション コールバックによって Message のサブクラスが返されます。
とにかく、次のコードは Labyrinth からの関連するスニペットです。「結果」変数でメッセージを取得します。
for command in self._pollLocations():
if command.getVerb() == 'quit':
if self._confirmQuit() is True:
return 2 # status 2 == quit w/o win or loss
else:
print( "Whew! I sure am glad for that!" )
else:
print( "^BEFORE" )
print( "command: %s\tperformer: %s" % ( command.getVerb(), command.performer.getName() ) )
result = command.performer.execute( command )
print( "vAFTER" )
print( "Got result: %s" % str(result) )
if result is None:
print( "Got NONE: returning it" )
return None
if isinstance( result, Exception ):
raise( result )
if isinstance( result, Message ):
print( "Found Message instance:" )
result.write()
return None
ただし、呼び出されたメソッドの return ステートメントで何を返しても、上記のresult =
呼び出しは常に None になります。私が呼び出している Actor.take() メソッドは次のとおりです。
def take( self, arg ):
"""
Take an item in an accessible area.
"""
item = self.location.getItemByName( arg )
if isinstance( item, MatchNotFound ):
print( "Actor.take(): Returning: %s" % str( ThatsNotHereMsg() ) )
return ThatsNotHereMsg()
elif isinstance( item, MissingDirectObject ):
print( "Missing DO" )
return TellMeWhatToTakeMsg()
elif isinstance( item, AlgorithmCouldNotDecide):
print( "Algorthm coul dnot decide." )
return IndecisionMsg()
elif isinstance( item, YouShouldNotBeSeeingThisError ):
print( "Returning GenericGameMsg" )
return GenericGameMsg( "You should not be seeing this error. " \
"The programmer has made a serious mistake " \
"and should be beaten for it. Besides, he's " \
"a lazy bum who never delivers software on time." \
)
else:
print( "take: made it past if..elif chain" )
if item is None:
print( "item is None: returning TellMeWhatToMakeMsg()" )
return TellMeWhatToTakeMsg()
if item.isTakeable( by=self ) is True:
print( "item.isTakeable: performing take" )
self.addItem( item )
self.location.delItem( item )
return YouTookItMsg()
print( "take: got to the end. Returning YouJustCantHaveItMsg" )
return YouJustCantHaveItMsg()
最後に、ゲームを実行しているときの出力を次に示します。そこにあるこれらの印刷ステートメントはすべてデバッグ用です。ThatsNotHere の新しいインスタンスを返すはずの MatchNotFound を引き起こすために、存在しないアイテムを取得しようとしていることがわかりますが、そうではありません。
You are in the cavern zone of a natural cave in the East Texas countryside.
You know this cave is unexplored. The deep, dark tunnel lies to the north.
"There's really no turning back now!", you tell yourself.
There is: a shady character (i.e. you) here.
t1 CavernZone ~> take gold coin
^BEFORE
command: take performer: player
Actor.take(): Returning: You don't see that here.
vAFTER
Got result: None
Got NONE: returning it
t2 CavernZone ~>
それで... 私の質問は、ここで何が起こっているのですか? 戻り値が呼び出し元に割り当てられる前に関数が戻るため、新しいインスタンスは GC されていますか? または、他の何か。私には本当にわかりません。どんな助けもいただければ幸いです。
ありがとう、ピーター
編集:
Roland が以下で述べたように、performer.execute() コードを投稿 (およびチェック!) します。その関数で結果を返すのを忘れていたことが判明しました! 額平手打ちをありがとう、私はそれらの多くが必要であることがわかりました! ;)
def execute( self, cmd ):
if cmd.callback is not None:
result = eval( cmd.callback )( cmd.getArgs() )
else:
pass
それは読むべきです:
def execute( self, cmd ):
if cmd.callback is not None:
result = eval( cmd.callback )( cmd.getArgs() )
else:
result = None
return result
それで、助けてくれてありがとう!
計画どおりに動作するプログラムの出力は次のとおりです。
t14 CavePassageOne ~> take something
Yes, but what?
t15 CavePassageOne ~> take computer
You don't see that here.