SheepTester / htmlifier

The HTMLifier "converts" Scratch 3.0 projects to an HTML file by putting all the project data and the entire Scratch engine into one enormous file
https://sheeptester.github.io/htmlifier/
MIT License
169 stars 77 forks source link

Question about Eval and Blockly variables #143

Closed TheShovel closed 2 years ago

TheShovel commented 2 years ago

-Can you define a blockly variable using it? (after project is loaded)

-Can you modify already existent variables? (player position for example)

-Can you brodcast a message using it?

-If it is not possible with blockly, than is it possible with something else?

SheepTester commented 2 years ago

You can create/change variables and broadcast messages but not using Blockly. Rather, you'd use scratch-vm, an instance of which is made global via Scratch.vm

const stage = Scratch.vm.runtime.getTargetForStage()
const wowVar = stage.lookupOrCreateVariable(null, 'wow')
console.log(wowVar.value) // 0 by default
wowVar.value = 3
SheepTester commented 2 years ago
Scratch.vm.runtime.startHats('event_whenbroadcastreceived', {
  BROADCAST_OPTION: 'message1'
})
TheShovel commented 2 years ago

Thanks a lot for the help!

TheShovel commented 2 years ago

I have another question on how you found the text representation to "run" scratch blocks. Because I searched a lot and didn't find anything.

SheepTester commented 2 years ago

Scratch blocks are represented as text in the form of JSON. If you download a project, rename it to a .zip, then extract it, the project.json file will contain the blocks' text representation

TheShovel commented 2 years ago

I tried running

Scratch.vm.runtime.startHats('event_whenbroadcastreceived', {
  BROADCAST_OPTION: 'message1'
})

and it didn't work... It says Scratch is not defined. Shouldn't it be defined and all that?

SheepTester commented 2 years ago

Scratch should be defined in HTMLified projects

TheShovel commented 2 years ago

Scratch should be defined in HTMLified projects

I just did and vm.runtime.startHats('event_whenbroadcastreceived', { BROADCAST_OPTION: 'e' })worked

TheShovel commented 2 years ago

Is there any documentation for more stuff? Like moving a sprite or modifying any of it's properties?

SheepTester commented 2 years ago

https://llk.github.io/scratch-vm/docs/

TheShovel commented 2 years ago

https://llk.github.io/scratch-vm/docs/

Thanks, but I still don't understand how to modify sprite properties. For example rotation or position. Wold you mind giving me some examples. Sorry for asking for so much help btw.

SheepTester commented 2 years ago

You can look at how the motion blocks are defined

https://github.com/LLK/scratch-vm/blob/d86546d2a2a34d3e367ff3a3b1ecccd99b5861a5/src/blocks/scratch3_motion.js#L116-L119

https://github.com/LLK/scratch-vm/blob/d86546d2a2a34d3e367ff3a3b1ecccd99b5861a5/src/blocks/scratch3_motion.js#L72-L76

TheShovel commented 2 years ago

You can look at how the motion blocks are defined

https://github.com/LLK/scratch-vm/blob/d86546d2a2a34d3e367ff3a3b1ecccd99b5861a5/src/blocks/scratch3_motion.js#L116-L119

https://github.com/LLK/scratch-vm/blob/d86546d2a2a34d3e367ff3a3b1ecccd99b5861a5/src/blocks/scratch3_motion.js#L72-L76

Example that I could use for any of those? So I understand the syntax, since that is my main issues rn, I do not understand how to specify what sprite I wanna modify and all that.

SheepTester commented 2 years ago

You can use vm.runtime.getSpriteTargetByName(spriteName) to get a sprite by its name or vm.runtime.getTargetForStage() for the stage

TheShovel commented 2 years ago

thank you! That is exactly what I wanted.

TheShovel commented 2 years ago

You can use vm.runtime.getSpriteTargetByName(spriteName) to get a sprite by its name or vm.runtime.getTargetForStage() for the stage

weird... that gives me an error, saying it isn't defined vm.runtime.getSpriteTargetByName(Sprite1)

TheShovel commented 2 years ago

nvm, forgot to put it in ('Sprite1')