Lua object programming example

From MaratisWiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "This example shows how to create a basic object using lua metatables : <nowiki>MyObject = {} MyObject.__index = MyObject function MyObject.create(ref, pos, speed) -- object...")
 
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This example shows how to create a basic object using lua metatables :
+
This example shows how to create a basic object using lua metatables :<br><br>
  
 
  <nowiki>MyObject = {}
 
  <nowiki>MyObject = {}
Line 21: Line 21:
  
 
end</nowiki>
 
end</nowiki>
 
  
 
It can then be used like this :
 
It can then be used like this :
Line 35: Line 34:
  
 
end</nowiki>
 
end</nowiki>
 +
 +
[[Category:User manual]]
 +
[[Category:Lua scripting]]

Latest revision as of 15:01, 5 February 2014

This example shows how to create a basic object using lua metatables :

MyObject = {}
MyObject.__index = MyObject

function MyObject.create(ref, pos, speed) -- object creation

	local newObj = {} -- our new object
	setmetatable(newObj, MyObject) -- set metatable
  
	newObj.object = getClone(ref) -- get a clone from the ref Maratis object
	newObj.speed = speed -- a local value
	setPosition(newObj.object, pos)

	return newObj
end

function MyObject:update() -- update function

	rotate(self.object, {0, 0, 1}, self.speed) -- rotate the object around the Z axis using the speed value

end

It can then be used like this :

Ref = getObject("Ref") -- object to clone

MyObj = MyObject.create(Ref, {0, 0, 0}, 1)

-- scene update
function onSceneUpdate()

	MyObj:update()

end
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox