mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 23:12:24 -05:00
Broadcast message functionality (works with creating new messages, and switching back and forth between them as well). This commit includes a temporary workaround for the issue where the default broadcast message, 'message1' wasn't triggering a var_create event (filed in LLK/scratch-blocks#1258).
This commit is contained in:
parent
d5acdbe983
commit
0a15190b85
5 changed files with 52 additions and 4 deletions
|
@ -56,14 +56,20 @@ class Scratch3EventBlocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
broadcast (args, util) {
|
broadcast (args, util) {
|
||||||
const broadcastOption = Cast.toString(args.BROADCAST_OPTION);
|
// TODO KARISHMA adding lookupOrCreate here temporarily (LLK/scratch-blocks#1258)
|
||||||
|
util.target.lookupOrCreateBroadcastMsg(args.BROADCAST_OPTION.id,
|
||||||
|
args.BROADCAST_OPTION.name);
|
||||||
|
const broadcastOption = Cast.toString(args.BROADCAST_OPTION.name);
|
||||||
util.startHats('event_whenbroadcastreceived', {
|
util.startHats('event_whenbroadcastreceived', {
|
||||||
BROADCAST_OPTION: broadcastOption
|
BROADCAST_OPTION: broadcastOption
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
broadcastAndWait (args, util) {
|
broadcastAndWait (args, util) {
|
||||||
const broadcastOption = Cast.toString(args.BROADCAST_OPTION);
|
// TODO KARISHMA adding lookupOrCreate here temporarily (LLK/scratch-blocks#1258)
|
||||||
|
util.target.lookupOrCreateBroadcastMsg(args.BROADCAST_OPTION.id,
|
||||||
|
args.BROADCAST_OPTION.name);
|
||||||
|
const broadcastOption = Cast.toString(args.BROADCAST_OPTION.name);
|
||||||
// Have we run before, starting threads?
|
// Have we run before, starting threads?
|
||||||
if (!util.stackFrame.startedThreads) {
|
if (!util.stackFrame.startedThreads) {
|
||||||
// No - start hats for this broadcast.
|
// No - start hats for this broadcast.
|
||||||
|
|
|
@ -365,7 +365,8 @@ class Blocks {
|
||||||
case 'field':
|
case 'field':
|
||||||
// Update block value
|
// Update block value
|
||||||
if (!block.fields[args.name]) return;
|
if (!block.fields[args.name]) return;
|
||||||
if (args.name === 'VARIABLE' || args.name === 'LIST') {
|
if (args.name === 'VARIABLE' || args.name === 'LIST' ||
|
||||||
|
args.name === 'BROADCAST_OPTION') {
|
||||||
// Get variable name using the id in args.value.
|
// Get variable name using the id in args.value.
|
||||||
const variable = optRuntime.getEditingTarget().lookupVariableById(args.value);
|
const variable = optRuntime.getEditingTarget().lookupVariableById(args.value);
|
||||||
if (variable) {
|
if (variable) {
|
||||||
|
|
|
@ -171,6 +171,19 @@ const execute = function (sequencer, thread) {
|
||||||
if (!fields.hasOwnProperty(fieldName)) continue;
|
if (!fields.hasOwnProperty(fieldName)) continue;
|
||||||
if (fieldName === 'VARIABLE' || fieldName === 'LIST') {
|
if (fieldName === 'VARIABLE' || fieldName === 'LIST') {
|
||||||
argValues[fieldName] = fields[fieldName].id;
|
argValues[fieldName] = fields[fieldName].id;
|
||||||
|
} else if (fieldName === 'BROADCAST_OPTION') {
|
||||||
|
// TODO KARISHMA (scratch-blocks #1258) this will change when we
|
||||||
|
// upstream blockly issue #1395 is fixed.
|
||||||
|
|
||||||
|
// we need both the id and the name here, because
|
||||||
|
// this object eventually gets passed to scratch3_event.broadcast
|
||||||
|
// which will create the message for the first time if one with the
|
||||||
|
// given id doesn't exist
|
||||||
|
// (e.g. default broadcast message, 'message1')
|
||||||
|
argValues[fieldName] = {
|
||||||
|
id: fields[fieldName].id,
|
||||||
|
name: fields[fieldName].value
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
argValues[fieldName] = fields[fieldName].value;
|
argValues[fieldName] = fields[fieldName].value;
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,6 +92,23 @@ class Target extends EventEmitter {
|
||||||
return newVariable;
|
return newVariable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Look up a broadcast message object, and create it if one doesn't exist.
|
||||||
|
* @param {string} id Id of the variable.
|
||||||
|
* @param {string} name Name of the variable.
|
||||||
|
* @return {!Variable} Variable object.
|
||||||
|
*/
|
||||||
|
lookupOrCreateBroadcastMsg (id, name) {
|
||||||
|
debugger;
|
||||||
|
const broadcastMsg = this.lookupVariableById(id);
|
||||||
|
if (broadcastMsg) return broadcastMsg;
|
||||||
|
// No variable with this name exists - create it locally.
|
||||||
|
const newBroadcastMsg = new Variable(id, name,
|
||||||
|
Variable.BROADCAST_MESSAGE_TYPE, false);
|
||||||
|
this.variables[id] = newBroadcastMsg;
|
||||||
|
return newBroadcastMsg;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Look up a variable object.
|
* Look up a variable object.
|
||||||
* Search begins for local variables; then look for globals.
|
* Search begins for local variables; then look for globals.
|
||||||
|
@ -134,7 +151,7 @@ class Target extends EventEmitter {
|
||||||
* dictionary of variables.
|
* dictionary of variables.
|
||||||
* @param {string} id Id of variable
|
* @param {string} id Id of variable
|
||||||
* @param {string} name Name of variable.
|
* @param {string} name Name of variable.
|
||||||
* @param {string} type Type of variable, '' or 'list'
|
* @param {string} type Type of variable, '', 'broadcast_msg', or 'list'
|
||||||
*/
|
*/
|
||||||
createVariable (id, name, type) {
|
createVariable (id, name, type) {
|
||||||
if (!this.variables.hasOwnProperty(id)) {
|
if (!this.variables.hasOwnProperty(id)) {
|
||||||
|
|
|
@ -25,6 +25,9 @@ class Variable {
|
||||||
case Variable.LIST_TYPE:
|
case Variable.LIST_TYPE:
|
||||||
this.value = [];
|
this.value = [];
|
||||||
break;
|
break;
|
||||||
|
case Variable.BROADCAST_MESSAGE_TYPE:
|
||||||
|
this.value = this.name;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error(`Invalid variable type: ${this.type}`);
|
throw new Error(`Invalid variable type: ${this.type}`);
|
||||||
}
|
}
|
||||||
|
@ -51,6 +54,14 @@ class Variable {
|
||||||
static get LIST_TYPE () {
|
static get LIST_TYPE () {
|
||||||
return 'list';
|
return 'list';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type representation for list variables.
|
||||||
|
* @const {string}
|
||||||
|
*/
|
||||||
|
static get BROADCAST_MESSAGE_TYPE () {
|
||||||
|
return 'broadcast_msg';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Variable;
|
module.exports = Variable;
|
||||||
|
|
Loading…
Reference in a new issue