Link this block to various other blocks once the sprite has been assigned
(self, link_subs: bool = True)
| 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: |
nothing calls this directly
no test coverage detected