Skip to content

BuiltinFunctions

uzudil edited this page Jun 25, 2020 · 15 revisions

The following built-in functions are available for bscript to call:

General

  • print(string message) print a message in text mode
  • input(string prompt) show a prompt a wait for input. Returns the text entered
  • len(array or string) return the length of characters in a string or the length of an array
  • keys(map) return the array of keys in a map
  • substr(string, from, length) return a part of a string starting at "from" for "length" characters
  • split(s, r) split string s by r regex delimiter
  • replace(string, old, new) return a new string where substring "old" has been replaced with "new"
  • debug() print the current status of variables, call stack, etc. in text mode
  • assert(a, b) used to fail tests if a != b
  • random() return a random number in the range of [0,1)
  • trace(message) Print a message to the command line (not to video)
  • getTicks() Return the number of elapsed seconds since the epoch as a floating point number
  • isKeyDown(int key) Return true if the given key is currently pressed down
  • anyKeyDown() Return true if any key is pressed
  • anyNonHelperKeyDown() Same as above but excludes shift, ctrl, etc. keys
  • int(float n) Return the integer part of a number
  • round(float n) Return the rounded integer of a number
  • abs(float n) Return the absolute value of a number

Graphics

  • setVideoMode(int mode) Set the video mode to 0,1,2 (0=text, 1=hires, 2=color modes)
  • updateVideo() update the video hardware. Usually called in the main loop of a game or demo. This function also limits the rendering and execution threads to a constant frame rate.
  • clearVideo() fill the entire screen with the current background color
  • setBackground(color) Set the default background color
  • limitFps(fps) by default video updates at 60 fps but you can use this function to change that.

Graphics Primitives

  • setPixel(int x, int y, int color) Set a pixel at x,y to color in graphics modes
  • flood(int x, int y, int color) Flood paint with color starting at x, y
  • drawLine(int x, int y, int x2, int y2, int color) draw a line from (x, y) to (x2, y2) with the given color
  • drawCircle(int x, int y, int radius, int color) draw a circle at (x, y) with the given radius and color
  • fillCircle(int x, int y, int radius, int color) fill a circle with the given color
  • drawRect(int x, int y, int x2, int y2, int color) draw a rectange
  • fillRect(int x, int y, int x2, int y2, int color) fill a rectange
  • drawText(int x, int y, int fg, int bg, string text) Print text at location (x,y) (which is pixels in graphics modes and character coordinates in text modes) with the given foreground and background colors
  • drawFont(int x, int y, int fg, int bg, int ch) Print the character "ch" at the given location and colors

Other graphics functions

  • scroll(int dx, int dy) Scroll the screen by dx and dy pixels. Works in text mode also.
  • getFont(int font) Returns an array of what this character looks like in memory. It's an array of 8 numbers where each one is an 8-bit value representing each pixel in that row.
  • setFont(int font, []byte rows) Update the character 'font' with a new value. Allows you to change what the characters look like.
  • getColor(int color) Returns an array of 3 rgb values for the color.
  • setColor(int color, []byte rgb) Allows you to change one of the 16 colors to a new rgb value
  • getImage(int x, int y, int x2, int y3) Get a rectangular section of the screen. Returns an Image object (which is a map.)
  • drawImage(int x, int y, Image image) Draw a previously captured image somewhere else.
  • setSprite(int index, Image[] images) Create a sprite object from a list of images. Index is 0-7. The images all must be the same size.
  • drawSprite(int x, int y, int index, int imageIndex, int flipX, int flipY) Move the previously created sprite "index" to location x,y and show the "imageIndex" image from its list of images. Drawing a sprite does not require a clearVideo call. You should still call updateVideo in your main loop, however. Sprites are draw on top of each other by their index order. FlipX and Y control mirroring the sprite.

Collision Detection

  • addBoundingBox(int key, int x, int y, int x2, int y2) Create a rectangular bounding area of type "key". Returns the index of the new bounding box.
  • getBoundingBox(int key, int index) Get the rectangle of this bounding box: [x, y, x2, y2].
  • delBoundingBox(int key, int index) Remove a bounding box. This only moves its rectangle off-screen, so it doesn't affect other bounding box indexes.
  • clearBoundingBoxes(int key) Delete all bounding boxes of this type
  • checkBoundingBoxes(int key, int x, int y, int x2, int y2) Check if the rectangle given in the params overlaps any of the bounding boxes of type "key". Returns the index of the overlapped box, or -1 if none are matched.
  • checkSpriteCollision(int spriteA, int spriteB) Returns true if the two sprites overlap.

Audio

  • playSound(int channel, float frequency, float duration) Play a frequency on one of the 4 channels (0-3) for a duration length of seconds. If the channel is paused it will buffer the sounds for later playback.
  • pauseSound(int channel, boolean enabled) - pause or resume playback
  • loopSound(int channel, boolean enabled) - loop any buffered and unplayed notes
  • clearSound(int channel) - remove any unplayed notes
Clone this wiki locally