Skip to content

Commit b603d47

Browse files
committed
Made it create mechanisms variable if needed
1 parent dc7a6fb commit b603d47

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/blocks/mrc_mechanism.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,16 @@ export const setup = function () {
118118
Blockly.Blocks[BLOCK_NAME] = MECHANISM_FUNCTION;
119119
}
120120

121-
//TODO: This needs to cause our own init to create the mechanisms line
122121
export const pythonFromBlock = function (
123122
mechanismBlock: MechanismBlock,
124123
generator: ExtendedPythonGenerator,
125124
) {
126125
if (mechanismBlock.mrcImportModule) {
127126
generator.addImport(mechanismBlock.mrcImportModule);
128127
}
128+
generator.setHasMechanism();
129129
let code = 'self.mechanisms["' + mechanismBlock.getFieldValue('NAME') + '"] = '
130130
+ mechanismBlock.getFieldValue('TYPE') + '('
131-
132131
for (let i = 0; i < mechanismBlock.mrcArgs.length; i++) {
133132
const fieldName = 'ARG' + i;
134133
if(i != 0){

src/editor/extended_python_generator.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,19 @@ export class ExtendedPythonGenerator extends PythonGenerator {
6464
for (let i = 0; i < classVars.length; i++) {
6565
variableDefinitions += this.INDENT + this.getVariableName(classVars[i].getId()) + ' = None\n';
6666
}
67+
if (this.context?.getHasMechanisms()){
68+
variableDefinitions += this.INDENT + "self.mechanisms = []\n";
69+
}
6770
return variableDefinitions;
6871
}
6972
getVariableName(name : string) : string{
7073
const varName = super.getVariableName(name);
7174
return "self." + varName;
7275
}
73-
76+
setHasMechanism() : void{
77+
this.context?.setHasMechanism();
78+
}
79+
7480
workspaceToCode(workspace: Blockly.Workspace, context: GeneratorContext): string {
7581
this.workspace = workspace;
7682
this.context = context;

src/editor/generator_context.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export class GeneratorContext {
3636
// Key is the mrc_class_method_def block's NAME field, value is the python method name.
3737
private classMethodNames: {[key: string]: string} = Object.create(null);
3838

39+
// Has mechanisms (ie, needs in init)
40+
private hasMechanisms = false;
41+
3942
setModule(module: commonStorage.Module | null) {
4043
this.module = module;
4144
this.clear();
@@ -44,6 +47,13 @@ export class GeneratorContext {
4447
clear(): void {
4548
this.clearExportedBlocks();
4649
this.clearClassMethodNames();
50+
this.hasMechanisms = false;
51+
}
52+
setHasMechanism():void{
53+
this.hasMechanisms = true;
54+
}
55+
getHasMechanisms():boolean{
56+
return this.hasMechanisms;
4757
}
4858

4959
getClassName(): string {

0 commit comments

Comments
 (0)