Tips and Tricks
From Emma
How do I iterate over all the nodes of a scene?
How do I iterate over the fields of Node?
for fieldName,fieldValue in pairs(node) do
print("node[", fieldName, "] = '", fieldValue, "'")
end
How do I access the scene, its camera, etc?
root.scene
How do I access the 'meta' tag values ?
Every xma proto now has an '__meta' property. This is a table with an array of tables, each of which is the meta data. This allows for multiple meta tags. Each meta data table contains all the attributes of the <meta> tag. This means that you can put arbitrary attributes in meta, since expat does not validate. But to maintain valid XML you should only use the attributes: http-equiv, name, content and scheme.
As an example of how to access the meta data, here is how you print it all out:
print("$$$$$$ meta:")
for i,v in ipairs(proto.__meta) do
print(" meta[", i, "]:")
for p,vv in pairs(v) do
print(" ", p, "='", vv, "'")
end
end
For mainProtoTest.xma this will print:
$$$$$$ meta:
meta[1]:
name='description'
content='Basic PROTO test'
meta[2]:
name='author'
content='Rob Myers'
How do I call a top-level function from a DO script?
- Top-level functions are in the 'self' scope, so call them as:
function foo()
end
DEF T Timer {
interval 10
loop true
fraction DO {
self:foo()
}
}
When do I use 'self' vs. 'id'
- 'self' means the top-level object that you are dealing with, like a Proto. To refer to a node, you simply use its 'id' XML attribute, which lives in the top-level object with everything else.
- Example of referring to a node's fields in one of its DO script:
DEF T Timer {
loop true
interval 5
fraction DO {
local f = Math:abs(T.fraction - 0.5)
root.scene.environment.camera:setPosition { f * 8 - 2, 0, 10 }
}
}
- Example of referring to a top-level field in a DO script of a node:
field SFInt32 cycle 0
DEF T Timer {
loop true
interval 3
fraction DO {
if (self.cycle == 0) then
-- walk with no attack
end
}
}
What is the difference between -- and // comments?
In the scope of .ema markup, C/JavaScript commenting conventions are used:
- // for commenting the remainder of the line, and
- /* ... */ for commenting the enclosed block of text
Within the scope of lua scripts, you should use the lua comment characters:
- -- for commenting the remainder of the line, and
- --[[ ]]-- for commenting the enclosed block of text
The scope of lua scripts encompassed within .ema markup lies within:
function blah ( ...) {
-- lua goes here
}
or
DO { ...
-- lua goes here
}
Sometimes a content file pops open the console, or not. Why?
Emma defaults to leaving the console closed, unless unusual output is sent to it; when errors or warning messages are sent to the console, it opens up automatically.
If you hit the 'c' key when Emma has keyboard focus, this will open up the console on demand.
Press 'h' to see the list of all available keyboard shortcuts.
Note that each keyboard command to the underlying engine will be overridden if the Emma content expresses interest in that specific key.
If you want to pop-up the console automatically, your log message should be an 'error' message, such as:
log("ERROR", "myfunc", "pop up the console = ", reason);
where "myfunc" by convention is a 6-character module identifier.
What are the stages of a proto's initialization ?
function preInitialize(self)
print("preInitialize : This is called first")
end
function __initialize(self,o)
print("__initialize : This is called second")
initPropsFromTable(self,o)
end
function initialize(self)
print("initialize : This is called last")
end
How can I do something when a proto is going away ?
function finalize(self)
print("finalize : this object is being garbage-collected now")
end
