Learn

J! Code Academy

Making a Spinning Platform with Traits

Hosted by Icoso

Create a reusable Spin trait with schema and metadata, attach it via the wrench tool, and spin entities by reading spinSpeed. Learn Traits, schemas, metadata, and angular velocity.

Script

Hey Mangos, welcome back to Jamango! Academy! Today we’re diving into Traits — Jamango’s plug-and-play behaviours to achieve almost anything in your game. By the end, you’ll have a reusable "Spin" trait to make spinning platforms for your obby. You can attach this trait to almost anything and tweak its speed.

What are Traits?#

Traits are lines of code that we attach to objects in the world to control how those objects function. They’re a core part of Jamango games. Open the code menu (J) and bring up server.ts.

We’ll define a new trait named Spin. This trait will have:

  • A unique ID ("Spin")
  • A schema with a spinSpeed vector (X, Y, Z)
  • Some metadata (name, description, icon, color)

Define the Spin trait#

ts
export const Spin = J.defineTrait(
  "Spin",
  J.schema.object({
    spinSpeed: J.schema.vec3(),
  }),
  {
    name: "Spin",
    description: "Makes the object spin!",
    icon: "🔄",
    color: "#51f",

Recap:

  • We create a variable Spin and call J.defineTrait to make it a trait.
  • The schema defines the shape of data: here a vec3 called spinSpeed.
  • Metadata customizes how the trait looks in-game.

Attach the trait to a prop using the wrench tool:

  • Select the prop, click "Add Trait", choose "Spin".
  • Set spinSpeed (e.g. Y-axis for upright rotation).
  • Ensure Motion Type is "Kinematic" if you don’t want gravity moving the platform.

Spin objects with code#

Now let’s find all entities that have the Spin trait and apply their angular velocity on game start.

ts
J.onGameStart(() => {
  const spinningObjects = J.getAllWithTraits([Spin]);

  for (const [entityId, spinningTrait] of spinningObjects) {
    J.setEntityAngularVelocity(entityId, spinningTrait.spinSpeed);
  }
});

Explanation:

  • J.onGameStart runs once when the world loads on the server.
  • J.getAllWithTraits([Spin]) returns every entity with the Spin trait plus its data.
  • For each entity, we read spinningTrait.spinSpeed and pass it to J.setEntityAngularVelocity.

Wrap-up#

With a few lines of code, you’ve built a customizable spinning mechanic. Change the speed or direction per entity using the trait’s spinSpeed. Try different axes and values for unique obstacles.

Share your wild spinning setups in #Creator-Logs on Discord, ask questions any time, and get ready — next we’ll use Traits to make a Bounce Pad!