Skip to main content

Scripts in Croquet

Tao asked for a "Tweak & Croquet" tutorial. I don't have time right now to actually write one, but here's some sample code anyway.

Using scripts is easy and useful, even without the Tweak GUI. Just use #startScript: to run some method as a script. Inside a script, you can use loops and anything you like, just throw in a wait to account for time. Like, to animate the color of a frame, you could use this method (just add it to your TeapotMorph):
animateColorFor: aFrame
0 to: 360 do: [:hue |
aFrame material color: (Color h: hue s: 1.0 v: 1.0).
self wait: 0.01]
] repeat
This changes the color every 10 ms, and you can start it from the initializeDefaultSpace method:
    self startScript: #animateColorFor: withArguments: {someFrame}.
Here is something that does not loop forever, but finishes after one cycle:
jump: aFrame
| v g |
v := 0@1@0.
g := 0@-0.05@0.
[aFrame translation y >= 0] whileTrue: [
aFrame translation: aFrame translation + v.
v := v + g.
self waitTick.
aFrame translation: 0@0@0
Here we wait for the end of the frame - waiting is essential, because we do want to change the position only once in a frame. This can be run in response to a pointer click like this:
    self startScript: #jump: withArguments: {someFrame} when: {someFrame. #pointerDown}.
Or, if you wish, you could make a Tweak button for it. Here's the whole initializeDefaultSpace method doing it all:
| space cube |
space := TSpace new.
space addChild: TLight new.
self makeFloor: space fileName:'floor.BMP'.

cube := TCube new.
cube material: TMaterial new.
space addChild: cube.

self startScript: #animateColorFor: withArguments: {cube}.

self startScript: #jump: withArguments: {cube} when: {cube. #pointerDown}.

self initializeTweakWorld: [
| button |
button := CButton new.
button label: 'Jump'.
button openAt: 100@100.
self startScript: #jump: withArguments: {cube} when: {button. #fire}.

Hope that helps ...


Philipp said…
Very nice code snippets, a good starting point.

Popular posts from this blog

Frontend-only Multi-Player. Unlimited Bandwidth. Or: What is, really?

A multi-player web app needs a backend, right? What if I told you, it doesn’t? Read on for how Croquet gets rid of servers. No, really . Instantaneous Shared Experiences  is how we describe Croquet on our website. And while that excellently describes What Croquet does, as Croquet's Chief Architect, I wanted to share a bit about How we do that. So I wrote a Twitter thread . Here it is in blog form, slightly extended. Click the animation above if it does not play automatically Croquet lets you build completely client-side multi-user web apps. Read that again. Client-side. Multi-user. No I’m not kidding. I built it, I know it works. 😁  Croquet apps run completely client-side: can be hosted as a static web site no server-side code needed no networking code needed  Croquet is literally virtualizing the server: Instead of running code on a server (or in a serverless function) we run it as a virtual machine (VM) on each client.  Croquet carefully controls the inputs to these identi

Deconstructing Floats: frexp() and ldexp() in JavaScript

While working on my  SqueakJS VM, it became necessary to deconstruct floating point numbers into their mantissa and exponent parts, and assembling them again. Peeking into the C sources of the regular VM, I saw they use the  frexp ()   and ldexp () functions found in the standard C math library. Unfortunately, JavaScript does not provide these two functions. But surely there must have been someone who needed these before me, right? Sure enough, a Google search came up with a few implementations. However, an hour later I was convinced none of them actually are fully equivalent to the C functions. They were imprecise, that is, deconstructing a float using frexp() and reconstructing it with ldexp() did not result in the original value. But that is the basic use case: for all float values, if [ mantissa , exponent ] = frexp (value) then value = ldexp ( mantissa , exponent ) even if the value is subnormal . None of the implementations (even the complex ones) really worked. I

Emulating Smalltalk-76

If you got as excited as me about Dan Ingalls' live Smalltalk-76 demo on an actual 1970's Xerox Alto, you may have wanted to try it yourself.  For one, you could try my Smalltalk-78 VM. Smalltalk-78 is a leaner version of Smalltalk-76 but very much identical in syntax semantics.  It is also possible to run the full Smalltalk-76 environment, and here is how: First, you need an emulator for the Alto computer. Ken Shiriff posted a nice piece on how to run ContrAlto on Windows . It is written in C# and I got it to work on my Mac using Mono. So here's a step-by-step: Install Mono from Download from Download this Smalltalk-76 disk image: Unzip both  and  in the same folder. In a terminal, change to the ContrAlto directory and run mono Contralto.exe .