Messages and Signals

Aus EnigmaWiki

Wechseln zu: Navigation, Suche
Available languages: Deutsch, English, Русский

Object interactions are an important component of Enigma and offer endless possibilities for level design. Here we deal in more detail with object interaction on the level of Lua.

There are 2 very similar concepts, in order to let objects interact. These are:

  • signal
  • message

Signals are simpler, but however less powerful than messages. Both have an advantage to the action/target system: you can script events, so there is no need of player interaction.

Inhaltsverzeichnis

Signals

How do you use Signals?

Signals are small bits of information, which are transferred from the sender to the receiver. This happens with the Lua function signal. It is used as follows:

signal(sender, receiver)

Now, how can you indicate the sender and the receiver? That's not difficult: you only need to hand the two strings to the signal function. They can be dynamically generated by means of other Lua code. These strings look for the three object classes in Enigma as follows:

  • For a stone:
    st(X Y)
  • For a floor tile:
    fl(X Y)
  • For an item:
    it(X Y)

X and Y are the coordinates of the object. They are separated only by a blank space, and not a comma!

In order to send a signal from the stone at position (2,3) to the stone at position (5,6), you write the following Lua code:

signal("st(2 3)","st(5 6)")

A further example sends a signal from the item at position (1,1) to the floor tile at position (3,8):

Signal("it(1 1)","fl(3 8)")

So much for the syntax of signal, but when and how do you use it correctly?

When do you use signals?

Always if an object needs to tell about it. But, due to other good alternatives, one can not answer this question finally. There may be cases where signals are more suitable, as this can be written very compact in the code. Their main disadvantage is probably that you do not immediately see who is doing what but only have a number of coordinates.

Alternatives:

A complete level as an example

We take a level as an example of the use of signals. I selected the level "Laser Splitting" (Pack Enigma VI, #6) by Lukas Schüller, as it uses signals as a central part.

 
CreateWorld(20,13)
 
draw_checkerboard_floor("fl-tigris","fl-bluegray",0,0,20,13)
 
function writeLine( line, cells)
  for i=1, strlen(cells) do
    local c = strsub(cells,i,i)
    if(c =="#") then
      set_stone("st-wood_001",i-1,line)
    elseif(c =="z") then
      set_stone("st-laserswitch",i-1,line)
    elseif(c =="e") then
      set_stone("st-laser-e",i-1,line)
    elseif(c =="*") then
      set_stone("st-switch",i-1,line)
    elseif(c =="x") then
      set_stone("st-grate1",i-1,line)
    elseif(c =="s") then
      set_stone("st-laser-s",i-1,line)
    elseif(c =="b") then
      set_stone("st-door_b",i-1,line)
    elseif(c =="/") then
      set_stone("st-mirror-p\\m",i-1,line)
    elseif(c =="<") then
      set_stone("st-mirror-3>m",i-1,line)
    elseif(c =="o")  then
      oxyd(i-1,line)
    elseif(c =="0") then
      set_actor("ac-blackball",i-0.5,line+0.5,{player="0"})
    end
  end
end
 
writeLine(00,"#############*####o#")
writeLine(01,"#        /x/#    bb#")
writeLine(02,"# ####z##  x# /  #b#")
writeLine(03,"# ##zz#zzs x# <  #b#")
writeLine(04,"# #z0      x# /  #b#")
writeLine(05,"# #z < <<  x# <  #b#")
writeLine(06,"# z#  <    x# // #b#")
writeLine(07,"# #z < #z# x# <  #b#")
writeLine(08,"# #z < z#  x# /  #b#")
writeLine(09,"#/ e   #   x# <  #b#")
writeLine(10,"#x            /  #b#")
writeLine(11,"#/xxxxxxxx x#    #b#")
writeLine(12,"##################o#")
 
SetAttrib(enigma.GetStone(3,9),"init",FALSE)
SetAttrib(enigma.GetStone(9,3),"init",FALSE)
 
Signal ("st(13 0)","st(3 9)")
Signal ("st(13 0)","st(9 3)")
Signal ("st(8 3)","st(17 1)")
Signal ("st(7 3)","st(18 1)")
Signal ("st(6 2)","st(18 2)")
Signal ("st(5 3)","st(18 3)")
Signal ("st(4 3)","st(18 4)")
Signal ("st(3 4)","st(18 5)")
Signal ("st(3 5)","st(18 6)")
Signal ("st(2 6)","st(18 7)")
Signal ("st(3 7)","st(18 8)")
Signal ("st(3 8)","st(18 9)")
Signal ("st(8 7)","st(18 10)")
Signal ("st(7 8)","st(18 11)")
 
oxyd_shuffle()
 

The signals run from the laser switches (st-laserswitch), here marked with z, to the doors (st-door_b) at the right, here marked with b. When a laser beam strikes a laser switch, this sends a signal to the appropriate door and opens it.

Messages

Messages have a very similar function to signals, but they can do far more. They are not limited to 1 bit and thus 2 conditions (0 and 1), but can carry additional information.

Syntax

Messages are generally sent away with the Lua function SendMessage. The syntax is somewhat more complicated than that of Signal.

The complete call syntax looks as follows:

SendMessage(object, message [,data])

Here we have the following parameters:

  • object: an object that receives the message
  • message: the message
  • data: additional data, depending upon the message


Object is the name of the target object set with name or a reference to it.

The reference can be gathered in two ways.

If you know the name of the object, then you can get the reference by:

my_stone=enigma.GetNamedObject(objname)

Further information is to be found under enigma.GetNamedObject. (You may use the name as well!)

If however you know the position of the object instead of only the name , you can get the reference in this way:

my_stone = enigma.GetStone(x,y)

Further information is to be found under enigma.GetStone.

The message is a string. That can be completely different things. The messages each object supports is specified in the object reference under "Messages".

The additional Data is not available everywhere. Their possible values differ from object to object.

Examples

St-chess knows the message flip. In order to change the colour of the horses with the name "my_horse":

local pferd = enigma.GetNamedObject("my_horse")
SendMessage(pferd, "flip")

St-disco knows the message "darken". In order to darken st-disko:

SendMessage(enigma.GetNamedObject("my_discostone1"), "darken")

St-fart knows the message "trigger", which causes it to fart. The reference is not found out in the following.

SendMessage(fartstone, "trigger")

Two examples with additional data: Here the direction of travel of the bolders with the name "bolder001" is set to the east:

SendMessage("bolder001","direction",EAST)

A mirror turn:

mir = enigma.GetNamedObject("mirror1")
enigma.SendMessage(mir, "turn", nil)

The data parameter can also be nil!

An exotic example. It sets fires:

SendMessage(set_floor("fl-dunes", x, y, {eternal=TRUE, burnable=TRUE}), "setfire")

Here, the return value of set_floor is used.

Persönliche Werkzeuge