@@ -223,23 +223,30 @@ class emulates that at the base class.
223
223
hook_func: A function that's called when the node is executed.
224
224
If the function returns a non-None value, the next
225
225
node will be skipped.
226
- fromOp: Unknown
227
- __oldNext : The original next node before hooking was done
226
+ from_op: The original node before hooking
227
+ old_next : The original next node before hooking was done
228
228
"""
229
- def __init__ (self , loc , hook_func = None , from_op = None ):
230
- #TODO: Understand fromOp and __oldNext
229
+ _serial = 1
230
+
231
+ def __init__ (self , loc , hook_func_ = None , from_op_ = None ):
231
232
super (ASTHook , self ).__init__ (loc )
232
233
233
- self .hook_func = hook_func
234
- self .from_op = from_op
234
+ self .hook_func = hook_func_
235
+ self .from_op = from_op_
235
236
self .old_next = None
236
237
238
+ # Create a unique name
239
+ self .name = "AWSWModOp_" + str (ASTHook ._serial )
240
+ ASTHook ._serial += 1
241
+ renpy .game .script .namemap [self .name ] = self
242
+
237
243
def execute (self ):
238
244
"""Execute hook after node is called"""
239
245
ast .statement_name ("hook" )
240
246
ret = None
241
247
if self .hook_func :
242
248
ret = self .hook_func (self )
249
+
243
250
if not ret :
244
251
self .exec_continue ()
245
252
@@ -270,26 +277,45 @@ def __init__(self, menu_, base_):
270
277
# Copy the menu.items list, not a reference to it
271
278
self .old_items = menu_ .items [:]
272
279
273
- def delete_item (self , item ):
274
- """Delete an item from the menu"""
275
- # TODO: Describe what the hell is happening here.
276
- self .get_items ()[:] = [(lab , cond , block ) for _ , (lab , cond , block )
277
- in enumerate (self .get_items ()) if lab != item ]
278
- return None
280
+ def delete_item (self , label ):
281
+ """Delete a choice from the menu
282
+
283
+ Args:
284
+ label (str): The label to search for
285
+ """
286
+ # choice[0] is the choice's label
287
+ self .get_items ()[:] = [choice for choice in self .get_items () if choice [0 ] != label ]
279
288
280
- def get_item (self , item ):
281
- """Get an item from the menu"""
282
- for obj in self .get_items ():
283
- if obj [0 ] == item :
284
- return obj
289
+ def get_item (self , label ):
290
+ """Get a choice from the menu
285
291
286
- def get_option_code (self , item ):
287
- """Get an item's SL code from the menu"""
288
- obj = self .get_item (item )
289
- return obj [2 ]
292
+ Args:
293
+ label (str): The label to search for
294
+ """
295
+ # obj[0] is the choice's label
296
+ for choice in self .get_items ():
297
+ if choice [0 ] == label :
298
+ return choice
299
+
300
+ def get_option_code (self , label ):
301
+ """Get a choice's SL code from the menu
302
+
303
+ Args:
304
+ choice (str): The choice to get
305
+ """
306
+ # choice[2] is the choice's SL code
307
+ choice = self .get_item (label )
308
+ return choice [2 ]
290
309
291
310
def get_items (self ):
292
- """Get all the items in the menu"""
311
+ """Get all the items in the menu
312
+
313
+ Returns:
314
+ A list of three-element tuples where the format is (label, condition, block), where
315
+ label is the visible label given to the user,
316
+ condition is a Python statement that determines whether or not to show the choice, and
317
+ block is SL code
318
+ """
293
319
return self .menu .items
294
320
295
321
def set_conditional (self , item , new_cond ):
@@ -298,14 +324,14 @@ def set_conditional(self, item, new_cond):
298
324
Returns:
299
325
True if successful and False if not
300
326
"""
301
- for i , (lab , _ , block ) in enumerate (self .get_items ()):
302
- if lab == item :
303
- self .menu .items [i ] = (lab , new_cond , block )
327
+ for i , (label , _ , block ) in enumerate (self .get_items ()):
328
+ if label == item :
329
+ self .menu .items [i ] = (label , new_cond , block )
304
330
return True
305
331
return False
306
332
307
333
def add_item (self , label , hook , condition = "True" ):
308
- """Add a new item to the menu
334
+ """Add a new choice to the menu
309
335
310
336
Args:
311
337
label (str): The option's label
@@ -327,8 +353,6 @@ def add_item(self, label, hook, condition="True"):
327
353
node = ASTHook (("AWSWMod" , 1 ))
328
354
node .from_op = self .menu
329
355
node .hook_func = hook
330
- node .name = "AWSWModOp_" + str (self .base .name_serial )
331
- self .base .name_serial += 1
332
356
self .get_items ().append ((label , condition , [node ]))
333
357
return node
334
358
0 commit comments