Skip to content

meshV2: Create broadcast message locally if it doesn't exist when received from mesh #84

@takaokouji

Description

@takaokouji

Summary

In the meshV2 extension, when a broadcast message is received from the mesh network, it might not exist in the local Scratch project. Currently, if the message name is not found in the project's variables, the broadcast is ignored, and the message doesn't appear in the block dropdowns.

Current Behavior

  • src/extensions/scratch3_mesh_v2/mesh-service.js#broadcastEvent triggers event_broadcast opcode with a message name.
  • Scratch3EventBlocks.broadcast (in src/blocks/scratch3_event.js) uses lookupBroadcastMsg.
  • If lookupBroadcastMsg returns null (because the message was never created locally), no hats are started.
  • The received message is not selectable in "when I receive [message]" or "broadcast [message]" block dropdowns because it doesn't exist in stage.variables.

Expected Behavior

  • When a broadcast message is received via meshV2, if it doesn't exist locally, it should be created automatically on the stage.
  • The new message should be selectable in the broadcast block dropdowns.
  • The event_broadcast opcode should be able to trigger the hats for the newly created message.

Proposed Fix

In MeshV2Service (likely in handleBatchEvent or broadcastEvent), add logic to ensure the broadcast message exists:

  1. Get the stage target: const stage = this.runtime.getTargetForStage();
  2. Check if the message exists: let broadcastVar = stage.lookupBroadcastMsg(null, event.name);
  3. If it doesn't exist:
    • Create it: stage.createVariable(null, event.name, Variable.BROADCAST_MESSAGE_TYPE);
    • Get the newly created variable: broadcastVar = stage.lookupBroadcastMsg(null, event.name);
    • Notify the GUI to refresh blocks: this.runtime.requestBlocksUpdate();
  4. Use the id from broadcastVar in the args passed to event_broadcast.

Locations to modify

  • src/extensions/scratch3_mesh_v2/mesh-service.js

References

  • Broadcast message type: Variable.BROADCAST_MESSAGE_TYPE ('broadcast_msg')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions