GetClone

From MaratisWiki
(Difference between revisions)
Jump to: navigation, search
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{lua-function|getClone|object}}
 +
 
Clone an object.
 
Clone an object.
  
#''Some notes about clones :''
+
{{lua-function-param|object|object to operate on}}
#* They keeps the base object active behavior
+
#* They keeps physics proprieties
+
#* They don't keep the translate/force/etc.. values of the base object from lua script (because they are new objects)
+
  
Basic usage :
+
<nowiki>object = getObject("object")
 
+
objectClone = getClone(object)</nowiki>
    object = getObject("object")
+
    objectClone = getClone(object)
+
  
 
For referencing you can store them into a table :
 
For referencing you can store them into a table :
  
    object = getObject("object")
+
<nowiki>object = getObject("object")
    cloneList = {}
+
cloneList = {}
    function onSceneUpdate()
+
function onSceneUpdate()
            if onKeyDown("SPACE") then
+
    if onKeyDown("SPACE") then  
                table.insert(cloneList, #cloneList + 1, getClone(object)) ''-- create a clone''
+
        table.insert(cloneList, #cloneList + 1, getClone(object)) -- create a clone  
            end
+
 
     end
 
     end
 +
end</nowiki>
  
 
With that method, the first clone will be cloneList[1], second clone will be cloneList[2], and so on..
 
With that method, the first clone will be cloneList[1], second clone will be cloneList[2], and so on..
Line 25: Line 22:
 
To manage all these clones at once you could add to the script, for example :
 
To manage all these clones at once you could add to the script, for example :
  
    for i=1, #cloneList do  
+
<nowiki>for i=1, #cloneList do  
        rotate(cloneList[i], {0, 0, 1}, 1, "local")
+
    rotate(cloneList[i], {0, 0, 1}, 1, "local")
    end  
+
end</nowiki>
  
 
In this case all the clones currently on the scene and the newly created ones will rotate.
 
In this case all the clones currently on the scene and the newly created ones will rotate.
 +
 +
'''Notes:'''
 +
* They keeps the base object active behavior
 +
* They keeps physics proprieties
 +
* They don't keep the translate/force/etc.. values of the base object from lua script (because they are new objects)
 +
 +
'''Warning:''' Spawning a lot of clones in a very short time may cause slow-down, because.
 +
* memory allocation : each cloning allocate new memory, fast games (like a shoot'em'up) usually pre-allocate memory.
 +
* when using physics, the new object need to be also allocated to the physics world
 +
* when using sound, the source need to be also allocated to the sound context
 +
 +
[[Category:Lua scripting]]
 +
[[Category:Lua function]]
 +
[[Category:Object]]

Latest revision as of 16:03, 15 February 2014

getClone(object)

Clone an object.

  • object : object to operate on
object = getObject("object")
objectClone = getClone(object)

For referencing you can store them into a table :

object = getObject("object")
cloneList = {}
function onSceneUpdate()
    if onKeyDown("SPACE") then 
        table.insert(cloneList, #cloneList + 1, getClone(object)) -- create a clone 
    end
end

With that method, the first clone will be cloneList[1], second clone will be cloneList[2], and so on..

To manage all these clones at once you could add to the script, for example :

for i=1, #cloneList do 
    rotate(cloneList[i], {0, 0, 1}, 1, "local")
end 

In this case all the clones currently on the scene and the newly created ones will rotate.

Notes:

  • They keeps the base object active behavior
  • They keeps physics proprieties
  • They don't keep the translate/force/etc.. values of the base object from lua script (because they are new objects)

Warning: Spawning a lot of clones in a very short time may cause slow-down, because.

  • memory allocation : each cloning allocate new memory, fast games (like a shoot'em'up) usually pre-allocate memory.
  • when using physics, the new object need to be also allocated to the physics world
  • when using sound, the source need to be also allocated to the sound context
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox