We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 7ed99b5 commit 5d2b425Copy full SHA for 5d2b425
3 files changed
src/dev/blocks/variableblocks.cpp
@@ -24,6 +24,7 @@ std::string VariableBlocks::description() const
24
void VariableBlocks::registerBlocks(IEngine *engine)
25
{
26
engine->addCompileFunction(this, "data_variable", &compileVariable);
27
+ engine->addCompileFunction(this, "data_setvariableto", &compileSetVariableTo);
28
}
29
30
CompilerValue *VariableBlocks::compileVariable(Compiler *compiler)
@@ -37,3 +38,15 @@ CompilerValue *VariableBlocks::compileVariable(Compiler *compiler)
37
38
else
39
return compiler->addConstValue(Value());
40
41
+
42
+CompilerValue *VariableBlocks::compileSetVariableTo(Compiler *compiler)
43
+{
44
+ Field *varField = compiler->field("VARIABLE");
45
+ Variable *var = static_cast<Variable *>(varField->valuePtr().get());
46
+ assert(var);
47
48
+ if (var)
49
+ compiler->createVariableWrite(var, compiler->addInput("VALUE"));
50
51
+ return nullptr;
52
+}
src/dev/blocks/variableblocks.h
@@ -17,6 +17,7 @@ class VariableBlocks : public IExtension
17
18
private:
19
static CompilerValue *compileVariable(Compiler *compiler);
20
+ static CompilerValue *compileSetVariableTo(Compiler *compiler);
21
};
22
23
} // namespace libscratchcpp
test/dev/blocks/variable_blocks_test.cpp
@@ -54,3 +54,26 @@ TEST_F(VariableBlocksTest, Variable)
54
ASSERT_EQ(Value(values[0]), 835.21);
55
ASSERT_EQ(Value(values[1]), "Hello world");
56
57
58
+TEST_F(VariableBlocksTest, SetVariableTo)
59
60
+ auto target = std::make_shared<Sprite>();
61
+ auto var1 = std::make_shared<Variable>("", "", 835.21);
62
+ target->addVariable(var1);
63
+ auto var2 = std::make_shared<Variable>("", "", "Hello world");
64
+ target->addVariable(var2);
65
+ ScriptBuilder builder(m_extension.get(), m_engine, target);
66
67
+ builder.addBlock("data_setvariableto");
68
+ builder.addEntityField("VARIABLE", var1);
69
+ builder.addValueInput("VALUE", "test");
70
71
72
+ builder.addEntityField("VARIABLE", var2);
73
+ builder.addValueInput("VALUE", 123);
74
75
+ builder.build();
76
+ builder.run();
77
+ ASSERT_EQ(var1->value(), "test");
78
+ ASSERT_EQ(var2->value(), 123);
79
0 commit comments