MCPcopy Create free account
hub / github.com/TimMcCool/scratchattach / link_using_sprite

Method link_using_sprite

scratchattach/editor/block.py:473–527  ·  view source on GitHub ↗

Link this block to various other blocks once the sprite has been assigned

(self, link_subs: bool = True)

Source from the content-addressed store, hash-verified

471 return _json
472
473 def link_using_sprite(self, link_subs: bool = True): # noqa: C901
474 """
475 Link this block to various other blocks once the sprite has been assigned
476 """
477 if link_subs:
478 self.link_subcomponents()
479
480 if self.mutation:
481 self.mutation.link_arguments()
482
483 if self._parent_id is not None:
484 self.parent = self.sprite.find_block(self._parent_id, "id")
485 if self.parent is not None:
486 self._parent_id = None
487
488 if self._next_id is not None:
489 self.next = self.sprite.find_block(self._next_id, "id")
490 if self.next is not None:
491 self._next_id = None
492
493 for _block in self.relatives:
494 _block.sprite = self.sprite
495
496 for _field in self.fields.values():
497 if _field.id is not None:
498 new_value = self.sprite.find_vlb(_field.id, "id")
499 if new_value is None:
500 # We probably need to add a local global variable
501 _type = _field.type
502
503 if _type == field.Types.VARIABLE:
504 # Create a new variable
505 new_value = vlb.Variable(commons.gen_id(), _field.value)
506 elif _type == field.Types.LIST:
507 # Create a list
508 new_value = vlb.List(commons.gen_id(), _field.value)
509 elif _type == field.Types.BROADCAST:
510 # Create a broadcast
511 new_value = vlb.Broadcast(commons.gen_id(), _field.value)
512 else:
513 # Something probably went wrong
514 warnings.warn(
515 f"Could not find {_field.id!r} in {self.sprite}. Can't create a new {_type} so we gave a warning"
516 )
517
518 if new_value is not None:
519 self.sprite.add_local_global(new_value)
520
521 # Check again since there may have been a newly created VLB
522 if new_value is not None:
523 _field.value = new_value
524 _field.id = None
525
526 for _input in self.inputs.values():
527 _input.link_using_block()
528
529 # Adding/removing block
530 def attach_block(self, new: Block) -> Block:

Callers

nothing calls this directly

Calls 6

link_subcomponentsMethod · 0.95
link_argumentsMethod · 0.80
find_blockMethod · 0.80
add_local_globalMethod · 0.80
link_using_blockMethod · 0.80
find_vlbMethod · 0.45

Tested by

no test coverage detected