66#include < scratchcpp/field.h>
77#include < scratchcpp/dev/compilerconstant.h>
88#include < scratchcpp/dev/executablecode.h>
9- #include < scratchcpp/stage .h>
9+ #include < scratchcpp/sprite .h>
1010#include < scratchcpp/iengine.h>
1111#include < scratchcpp/list.h>
1212
@@ -153,31 +153,26 @@ void ScriptBuilder::addDropdownField(const std::string &name, const std::string
153153 impl->blocks .back ()->addField (field);
154154}
155155
156- /* ! Returns the current block (can be used e. g. with a custom Compiler instance). */
157- std::shared_ptr<Block> ScriptBuilder::currentBlock () const
156+ /* !
157+ * Returns the current block (can be used e. g. with a custom Compiler instance).\n
158+ * The script is automatically built to set the compile function of the block.
159+ * \note This method is not intended for building scripts, use build() for that.
160+ */
161+ std::shared_ptr<Block> ScriptBuilder::currentBlock ()
158162{
163+ if (!impl->lastBlock )
164+ return nullptr ;
165+
166+ if (!impl->lastBlock ->compileFunction ())
167+ build (std::make_shared<Sprite>());
168+
159169 return impl->lastBlock ;
160170}
161171
162172/* ! Builds and compiles the script. */
163173void ScriptBuilder::build ()
164174{
165- if (impl->target ->blocks ().empty ()) {
166- for (auto block : impl->blocks )
167- impl->target ->addBlock (block);
168-
169- for (auto block : impl->inputBlocks )
170- impl->target ->addBlock (block);
171- }
172-
173- std::vector<std::shared_ptr<Target>> targets = impl->engine ->targets ();
174-
175- if (std::find (targets.begin (), targets.end (), impl->target ) == targets.end ()) {
176- targets.push_back (impl->target );
177- impl->engine ->setTargets ({ impl->target });
178- }
179-
180- impl->engine ->compile ();
175+ build (impl->target );
181176}
182177
183178/* ! Runs the built script. */
@@ -202,3 +197,25 @@ void ScriptBuilder::addBlock(std::shared_ptr<Block> block)
202197
203198 impl->blocks .push_back (block);
204199}
200+
201+ void ScriptBuilder::build (std::shared_ptr<Target> target)
202+ {
203+ impl->engine ->clear ();
204+
205+ if (target->blocks ().empty ()) {
206+ for (auto block : impl->blocks )
207+ target->addBlock (block);
208+
209+ for (auto block : impl->inputBlocks )
210+ target->addBlock (block);
211+ }
212+
213+ std::vector<std::shared_ptr<Target>> targets = impl->engine ->targets ();
214+
215+ if (std::find (targets.begin (), targets.end (), target) == targets.end ()) {
216+ targets.push_back (target);
217+ impl->engine ->setTargets ({ target });
218+ }
219+
220+ impl->engine ->compile ();
221+ }
0 commit comments