Skip to content

Lua scripting

Mikulas Florek edited this page Dec 14, 2015 · 150 revisions

Lua script plugin provides an option to script using Lua. It provides a new type of component, which points to a lua script file. This file is executed when a game starts. Each component has lua sandboxed - it means that all variables and function are not accessible from other components unless they are put in the global table _G or accessed using Engine.getEnvironment.

Update function

Each script can contain function update(dt) and this function is called each frame.

API

Engine

Global variables

  • UniverseContext g_universe_context
  • Universe g_universe
  • Engine g_engine

All scenes are visible to script as global variables, each with prefix "g_scene_" and ends with the name of a plugin, e.g.:

  • g_scene_renderer
  • g_scene_animation
  • g_scene_audio
  • g_scene_physics
  • g_scene_lua_script

Types

Other than standard lua types there is one more - Vec3. It is just table with three numbers.

Functions

  • Table Engine.getEnvironment(Scene scene, Entity entity) #620
  • void Engine.setEntityPosition(Universe univ, Entity entity, Vec3 position)
  • void Engine.setEntityRotation(Universe univ, Entity entity, Vec3 axis, number angle)
  • void Engine.setEntityLocalRotation(UniverseContext univ, Entity entity, Vec3 axis, number angle)
  • void Engine.setRenderablePath(Scene scene, Component cmp, string path)
  • number Engine.getInputActionValue(Engine engine, number action)
  • void Engine.addInputAction(Engine engine, number action, number type, number key)
  • Component Engine.createComponent(Scene scene, string type, Entity entity)
  • void Engine.logError(string text)
  • void Engine.logInfo(string text)
  • Vec3 Engine.multVecQuat(Vec3 vec, Vec3 axis, number angle)
  • Component Engine.getRenderable(Scene scene, Entity entity)
  • void Engine.destroyComponent(Scene scene, string type, Component component)

Physics

  • void Physics.moveController(Scene scene, Component cmp, Vec3 velocity, number time_delta)
  • number Physics.getActorSpeed(Scene scene, Component cmp)
  • Component Physics.getActorComponent(Scene scene, Entity entity)
  • void Physics.putToSleep(Scene scene, Entity entity)
  • void Physics.applyForceToActor(Scene scene, Component component, Vec3 force)

If two entities with a physical component collide, onContact function is called in script on both of them:

function onContact(entity)
  API_logError("not implemented - onContact(" .. entity .. ")")
end

Audio

  • SoundHandle Audio.playSound(Scene scene, Entity entity, string clip_name, bool is_3d)
  • void Audio.setSoundVolume(Scene scene, SoundHandle sound_id, number volume)
  • void Audio.setEcho(IScene* scene, SoundHandle sound, number wet_dry_mix, number feedback, number left_delay, number right_delay)

Clone this wiki locally