User Tools

Site Tools


lua:scriptenvironments

This is an old revision of the document!


Script Environments

With the exception of the shared table all Lua scripts run in the environment they're first called upon and by default have no access to variables or methods in other environments. These environments are the OBJ, CH/MOB, ROOM, AREA spaces, along with a few others. There are several ways to "break out" of an environment and affect external environments.

Simple Interactions

The most common way to manipulate an outside environment is with the Prog Trigger mechanism. Mob Progs, Obj Progs, Room Progs and Area Progs all return a CH object, and some return an OBJ object.

For example, in this simple script, a Mob Prog will return the name of a CH, which is a property of CH.

-- GRALL. Let's say HI! to PCs
if ch.ispc then – ispc is also a property of CH
  say("HI "..ch.name.."!")
end

These simple interactions often depend on the method that wants to be called being available to the environment that is calling it. For example, damage() is a method of CH, so if a mob wanted to damage a PC upon entering a room, it would be quite simple:

-- GRALL. Our PC entered this room
mob:damage(ch, 5000, true, "holy")

And now our char has been damaged for 5000 hitpoints.

Because damage() is a method of CH, this could easily be rewritten so that the PC damages themselves:

-- GRALL. Our PC entered this room
ch:damage(ch, 5000, true, "holy")

There is no qualitative difference between the two versions.

Executing a single method in another environment

In our above example damage() could be used in two different ways because the method belongs to CH. But what if you wanted an object to damage a player? OBJ does not have the method damage(), so you must "break out" into the CH environment.

In this example an object will hurt a player who tries to drop it:

-- DROP. Damage a player who tries to drop this object.
echo("You can't drop me! I'll hurt you!")
ch1:damage(ch1, 5000, true)
return false -- don't let them drop the object

Since "ch1" is one of the script arguments this will work. However, not all Obj Progs return a ch1. For instance, if we wanted an object that hurts a player every 5 seconds they're carrying it, you'd need to break out in other ways:

-- TIMER 5 seconds. Damage the player every 5 seconds.
if obj.carriedby then -- Make sure it's being carried
  obj.carriedby:damage(obj.carriedby, 5000, true)
end

In this case OBJs have a property "carriedby" which returns a CH if the object is being carried. By using this property we can "break out" of the OBJ environment into a CH environment that contains the damage() method.

lua/scriptenvironments.1445059493.txt.gz · Last modified: 2015/10/17 05:24 by aethyn