SetLayerRenderer

From Spheriki

Jump to: navigation, search

Set a script to run after a layer has been rendered.


Contents

Usage

SetLayerRenderer(layer, script);
  • layer number, non-negative. Index of the layer to set the render script for.
  • script string. The JavaScript code to run, in string form.


Examples

To substitute a layer with a background, first put this in an included script file (see RequireScript()):

// In a script
const SCREEN_WIDTH = GetScreenWidth();
const SCREEN_HEIGHT = GetScreenHeight();
var background = LoadImage("my_background.png");

function DrawTiledBackground(layer) {
  var x = MapToScreenX(layer, 0) % SCREEN_WIDTH;
  var y = MapToScreenY(layer, 0) % SCREEN_HEIGHT;
  
  while (x > 0) x -= SCREEN_WIDTH;
  while (y > 0) y -= SCREEN_HEIGHT;
  
  for (var iy = y; iy < SCREEN_HEIGHT; iy += SCREEN_HEIGHT) {
    for (var ix = x; ix < SCREEN_WIDTH; ix += SCREEN_WIDTH) {
      background.blit(ix, iy);
    }
  }
}

And before the call to MapEngine():

SetLayerRenderer(0, "DrawTiledBackground(0);");

You may wish to put tiles and persons on a higher layer, since this background draws over anything on layer 0 (the bottom layer), including persons.


Notes

  • Since the JavaScript code is entered as a string, it cannot be checked ahead of time. Be careful, and avoid putting long scripts with logic inside the string.
  • The layer index is valid between 0 and GetNumLayers() - 1 inclusive.
  • Only one layer script can be assigned to a layer at a time.


See also

Personal tools