Skip to content

Commit ed50a48

Browse files
authored
Update switchblock.ts - adding extra execution pathway ...
New Execution Pathway: - **Name:** `switchcase.executeFromValue(name: string, value: any)` - Adds new block for executing a registered switch (with already registered cases) but that allows you to postpone the switch.value until the time of execution. - This provides a few _NEW_ features: 1. You can now add new cases later in the game, such as a new power up you claimed. 2. You can now remove cases later in the game, such as when a powerup is replaced by a new one or when a powerup fails/exhausts due to usage or time limit. 3. Because execution occurs at any time, your switch-case can be registered like a lookup table whenever you desire, but only execute during the absolute moment when it needs to be executed, allowing you to not only execute from an existing value, the value can now be the most recent value upon execution (like a standard switch statement) which provides freedom to update the value as necessary without fear of using an out-of-date variable... - The switch-case now no longer 'penalizes' you for not knowing the variable (or its state) at the beginning of the creation of said switch-case.
1 parent 34cd195 commit ed50a48

1 file changed

Lines changed: 34 additions & 7 deletions

File tree

switchblock.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace switchcase {
2121
/**
2222
* Switch Block Container
2323
*/
24-
//% block="switch $value"
24+
//% block="switch $name looking for $value"
2525
//% blockId=switchcase_switch_block
2626
//% group="Control"
2727
//% weight=199
@@ -60,9 +60,9 @@ namespace switchcase {
6060
* Use this after all case blocks have been added to your switch.
6161
* It will run the SwitchContext.execute() after pulling the switch by the correct name.
6262
*/
63-
//% block="switch $name execute on above cases"
64-
//% blockId=switchcase_switch_executor
65-
//% group="Control"
63+
//% block="switch $name :: execute on registered value against registered cases"
64+
//% blockId=switchcase_switch_execute
65+
//% group="Advanced Timing"
6666
//% weight=190
6767
//% draggableParameters
6868
export function executeSwitch(name: string) {
@@ -80,11 +80,32 @@ namespace switchcase {
8080
cxt.execute();
8181
}
8282

83+
/**
84+
* execute the switch [name] on [value].
85+
* Use this when you want to run a registered switch and it's
86+
* registered cases against a specific value
87+
*/
88+
//% block="switch $name :: execute on $value against registered cases"
89+
//% blockId=switchcase_execute_switch_on_value
90+
//% group="Advanced Timing"
91+
//% weight=60
92+
//% draggableParameters
93+
export function executeSwitchOnValue(name: string, value: any) {
94+
let cxt = switchManager.create(name);
95+
if(!cxt) {
96+
//throw error?
97+
return;
98+
}
99+
100+
cxt.executeFromValue(value);
101+
}
102+
83103
/**
84104
* Instantiate the SwitchContext, or retrieve it, but do NOT execute on it. Merely assign the value to it.
85105
*/
86-
//% block="create switch:$name with value:$value"
106+
//% block="create switch $name looking for $value"
87107
//% blockId="switchcase_create_switch_full"
108+
//% group="Advanced Timing"
88109
//% blockSetVariable=switchContext
89110
export function createSwitchFull(name: string, value: any): SwitchContext {
90111
let cxt = createSwitch(name);
@@ -99,20 +120,26 @@ namespace switchcase {
99120
*/
100121
//% block="create (or get) switch $name"
101122
//% blockId="switchcase_create_or_get_switch"
123+
//% group="Core"
102124
//% blockSetVariable=switchContext
103125
export function createSwitch(name: string) : SwitchContext {
104126
let cxt = switchManager.get(name);
105-
if(cxt)
127+
if(cxt) {
128+
switchContext = cxt;
106129
return cxt;
130+
}
107131

108132
cxt = switchManager.create(name);
133+
switchContext = cxt;
109134
return cxt;
110135
}
111136

112137
/**
113138
* Set the Current Switch by name (from the local switchManager)
114139
*/
115-
//% block="set active name: $name"
140+
//% block="set active switch name: $name"
141+
//% blockId=switchcase_set_current_switch_name
142+
//% group="Debugging & Testing"
116143
//% blockSetVariable=currentSwitchName
117144
export function setCurrentSwitch(name: string) {
118145
currentSwitchName = name;

0 commit comments

Comments
 (0)