User Tools

Site Tools


dev:tutorials:basic

Basic EntityScript

  • A script load some resources and define some entity scripts.
  • The context is not shared with other scripts, so global variables are only available for this script.
  • Each lua script is executed when the lua threads are created (so the script can be executed 8 times if there are 8 threads).

Global initialization

Since the entity scripts are executed in threads, it's not possible to use graphical functions in them, so you need to load any GPU resource at the script execution.

Simple example (in this case the script is executed in both sides, client and server) :

-- global initialization
 
-- load a hemisphere mesh
mesh_hemisphere = DataManager.loadMesh("shinsekai.archzenvald.com_repos_test_room1/objects/hemisphere.sho")
-- or DataManager.loadMesh(REPODIR.."/objects/hemisphere.sho")
 
-- load a texture (client-side)
if client then
  tex_sky = DataManager.loadTexture("shinsekai.archzenvald.com_repos_test_room1/textures/skydome_night.jpg",true)
end

Define the entity script

MyEntity = {}
 
-- overload some methods
 
MyEntity:spawn()
  print("Nice to be alive.")
 
  -- create the sky
  if client then
    self.sky = self:createFalseInstance()
    self.sky:setModel(mesh_hemisphere)
    self.sky:setDiffuseTexture(tex_sky)
    self.sky:sky(true) -- set sky flag
 
    self:sceneAdd(self.sky) -- add the object to the scene
  end
end
 
MyEntity:frame(time) -- called at every client/server frames
end
 
MyEntity:despawn()
  print("Please, not yet.")
end
 
-- register the entity script
registerEntityScript("MyEntity",MyEntity) -- choose a unique class name for your entity, without space

To spawn it on your own server, add the entity to the entities.list :

entities.list
MyEntity my_entity1

API Doc

Tutorials can't cover everything and every changes made to the scripting interface, here is the up to date online documentation.

dev/tutorials/basic.txt · Last modified: 2017/04/03 15:37 by archzenvald