Skip to content

Code Blocks

Niklas Korz edited this page Apr 21, 2019 · 5 revisions

When writing code blocks in the audio3d editor, a few things have to be considered.

First of all, the JavaScript is running right in the browser and theoretically has access to all available browser APIs. While this might look like a security risk at first, the risk is arguably low. A native game running on your computer can do far more damage than a web application, so similar to how you should be cautious when running software you don't trust, you should be cautious about what projects you import and run in the audio3d editor. The risk is significantly lower though, as no sensisitive information is stored on the page of the web application. A code block has access to the editor's storage though and could theoretically damage, steal or even delete any saved editor projects. This could be mitigated by running the code in an isolated interpreter (e.g. by using WebAssembly) but is outside of this project's scope.

A code block is passed a few useful parameters that should fulfill most requirements when writing a code block. There are four useful objects in a code block's scope:

  • this: A code block's this references the GameObject on which the code is executed.
  • game: The game object references the the Project the code block is running in.
  • playerState: The playerState is a hash map of type Map<string, any> and can be used for storing runtime variables that should be available from all rooms.
  • roomState: The roomState is a hash map of type Map<string, any> and can be used for storing runtime variables that are bound to the room the code block is running in.

The types of these parameters offer a set of useful methods and properties.

GameObject

  • interactionType: string: Can be set to "No interaction" to disable interaction with this object
  • audio: Audio3D: The game object's audio object (see Audio3D below)
  • playAudio(id: number, loop: boolean = false)
    • id: The audio id to be played instead of the game object's current audio. You can find the id associated with an audio file in the audio library (View > Audio Library).
    • loop: Whether the audio should be played endlessly (optional, defaults to false)
  • destroy(): Removes the object from the game and stops its audio playback

Audio3D

  • isPlaying: boolean: Whether the audio object is currently playing
  • hasStarted: boolean: Whether the audio object has started playback before
  • play(): Start (or restart) playback of the audio object
  • stop(): Stop playback of the audio object
  • setLoop(loop: boolean): Set whether the audio should be played endlessly

Project

  • teleportPlayer(roomId: string, spawnId?: string)
    • roomId: The unique id of the room the player should be teleported to. Can be found in the sidebar on the left after selecting a room.
    • spawnId: The unique id of the spawn marker the player should be teleported to (optional, defaults to the first one found in the room). Can be found in the sidebar on the left after selecting a spawn marker.

Map<string, any>

See Map on the Mozilla Developer Network.

The most useful methods are:

  • has(key: string): boolean: Returns whether there is a key-value-pair with the specified key in the map.
  • get(key: string): any | undefined): Returns the key-value-pair with the specified key or undefined if it does not exist.
  • set(key: string, value: any): this): Sets the key-value-pair with the specified key and returns the map object itself.
  • delete(key: string): boolean: Deletes the key-value-pair with the specified key and returns whether a pair with the specified key was found or not.
  • clear(): Deletes all key-value-pairs in the map
Clone this wiki locally