Action and Target
Aus EnigmaWiki
Available languages: Deutsch, English, Русский
The attributes action and target appear at many different places. They make it possible to activate lasers, open doors, transform hills into hollows and do much more.
Inhaltsverzeichnis |
A simple example
In this example a laser is switched on and off with a switch.
create_world(20, 13) fill_floor("fl-bluegray",0,0,20,13) draw_border("st-glass") set_stone("st-laser-e",6,4, {name="laser cannon"}) set_stone("st-switch",4,4, {action="onoff", target="laser cannon"}) set_actor("ac-blackball", 5.5, 5.5)
(Insert the code into the Level Template to get a functional level.)
Analysis
If you read the page Getting started, only two lines of this level contain something new:
set_stone("st-laser-e",6,4, {name="laser cannon"})) set_stone("st-switch",4,4, {action="onoff", target="laser cannon"})
In the first line a stone st-laser-e is set. We assign the attribute name to it. Now this stone can be addressed by the name "laser cannon".
In the second line a switch st-switch is set. Two attributes are assigned to this switch: action and target.
-
targetdefines the target, and/or which object is addressed, if the switch is operated. Since we gave the st-laser the name "laser cannon" before, we can address it in such a way now. -
actiondefines to the addressed object which instruction to implement. For which instructions a certain object hears, see the reference in the section "Messages" for the respective object.
Some frequently used code snippets
set_stone("st-fart",5,5,{name="farter"}) set_stone("st-timer",6,5,{action="trigger", target="farter", interval="3"})
Lua function as target
It is also possible to call with action and for target a Lua function:
create_world(20, 13) fill_floor("fl-bluegray",0,0,20,13) draw_border("st-glass") set_actor("ac-blackball", 5.5, 5.5) set_stone("st-switch",4,4, {action="callback", target="my function"}) function my function() set_stone("st-magic",6,4) set_item("it-document",6,5,{text="I was set by my function"}) end
Analysis
action="callback" means: Implement the function, whose name is indicated in target. It is not possible to hand over the function arguments in the usual style of Lua. But there is nevertheless a way to give the function information.
Argument delivery on callback function
A frequent example: you have a surface of triggers and the called function should know by which trigger it was called.
for i = 1, 5 do for j = 1, 5 do set_item("it-trigger", 10+i, 5+j, {action = "callback", target="my function", _my_attribute_1=i, _my_attribute_2=j }) end end function my function(onoff, sender) -- "onoff" contains the state of the trigger -- "sender" is the trigger, as object local mein_i = enigma.GetAttrib(sender, "_my_attribute_1") local mein_j = enigma.GetAttrib(sender, "_my_attribute_2") mein_i=mein_i+10 mein_j=mein_j+5 set_item("it-document", 2, 2, {text="Called of the trigger on position ("..tostring(mein_i)..";"..tostring(mein_j)..")."}) end
Analysis
You give further attributes to the calling object (this can be a trigger, switch, coinslot etc.).
The names of these attributes are freely selectable, however they must be clear. Then you assign the values that you would like to access from the Lua function to these attributes. You access then with GetAttrib(object, attribname) on the appropriate attribute. The object is always called "sender".
Naturally you can also write these attributes (to change) by means of enigma.SetAttrib(object, attribname, value) For example you can make each trigger of this field callable only once.
If you are been versed with Lua a little, you can program very complex levels with your own mechanisms, for example the level "Enigmastermind" in Level pack "Enigma II" or "Island of Safety" in "Enigma V".

