User Tools

Site Tools


lua:scriptenvironments

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
lua:scriptenvironments [2015/10/17 05:46] – [Simple Interactions] aethynlua:scriptenvironments [2016/02/08 04:45] (current) – [Script Environments] vodur
Line 1: Line 1:
 ====== Script Environments ====== ====== Script Environments ======
  
-With the exception of the [[:lua:sharedtable|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 environmentsThese environments are the OBJCH/MOB, ROOM, AREA spaces, along with few others. There are several ways to "break out" of an environment and affect external environments.+All prog scripts run in the scripting environment that is attached to the game object to which the trigger is attached. For instance, mprogs run in the environment of the mob that is running the scriptEach individual environment represents the "global" scope for the scriptand thusly "global" variable or function in one environment cannot be accessed from a script running in a different environment. 
 + 
 +There are a few ways to share values between different script environments, the most simple to use being [[:lua:sharedtable|shared table]].
  
 ===== Simple interactions ===== ===== Simple interactions =====
Line 11: Line 13:
 <code lua> <code lua>
 -- GRALL. Let's say HI! to PCs -- GRALL. Let's say HI! to PCs
-if ch.ispc then – ispc is also a property of CH+if ch.ispc then -- ispc is also a property of CH
   say("HI "..ch.name.."!")   say("HI "..ch.name.."!")
 end end
Line 57: Line 59:
  
 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. 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.
 +
 +===== Finding the correct environment =====
 +
 +There are many [[:lua:globals|global functions]] that will return a single environment or a table of environments. The most commonly used are getmobworld(), getpc(), getroom() and getobjworld(). Below are some examples.
 +
 +==== getmobworld() and getobjworld() ====
 +
 +These two functions work the same way. Only getmobworld() is shown, but the examples can be made to fit getobjworld(), as long as you use methods and properties common to OBJs, not CHs.
 +
 +<code lua>
 +-- Let's find a mob with vnum 22000 and make him "sleep". We know there's only one mob with that vnum.
 +getmobworld(22000)[1]:mdo("sleep")
 +</code>
 +
 +Since we know there's only one mob with the vnum 22000 we can use the bracket notation to target that mob. getmobworld() returns a table of mobs, so if there's only one mob we know that mob will be the first in the table. If there is more than one we have to loop through the list:
 +
 +<code lua>
 +-- Make mob 22000 in room 22050 "sleep"
 +for _,mobile in ipairs(getmobworld(22000)) do
 +  if mobile.room.vnum == 22050 then
 +    mobile:mdo("sleep")
 +  end
 +end
 +</code>
 +
 +In this example we've looped over the getmobworld() list for that vnum, but also "broke out" in a more conventional way. A mob has a ROOM property, which has a vnum property. In that way we can find out which room a mobile is in and make them sleep if they're in it.
 +
 +==== getpc() and getroom() ====
 +
 +getpc() takes a string argument. It, like getroom(), can return only one object because PC names are unique.
 +
 +<code lua>
 +-- TIMER. Every 5 minutes let's give Vodur a hug. He deserves it.
 +if getpc("Vodur") then -- First let's make sure he's online
 +  mob:goto(getpc("Vodur").room.vnum) -- make sure we're going to the room he's in
 +  mob:mdo("hug vodur")
 +end
 +</code>
 +
 +This contrived example is obviously not very useful. In the Cards Against Humanity script we can see how to use getpc() in a more constructive way:
 +
 +<code lua>
 +function check_player_num()
 +  for _,v in ipairs(players) do
 +    if not(getpc(v.name)) then
 +      missing_players = {}
 +      table.insert(missing_players, v.name)
 +      active_players = #players - #missing_players
 +    end
 +  end
 +  if active_players> 2 then
 +    start_hand()
 +  else
 +    for _,j in ipairs(players) do
 +      tell(j.name, "We're missing "..util.format_list("and", missing_players).."! We require three players to play, and we currently have "..active_players.."!")
 +      tell(j.name, "There will be a 30 second break for another player to join, or for "..util.format_list("and", missing_players).." to return.")
 +      delay(30, final_check)
 +    end
 +  end
 +end
 +</code>
 +
 +In the function the "players" table, which only contains the strings of a PC's name (and not the CH object) is looped through and checked against getpc() to make sure players haven't logged off.
 +
 +getroom() is even more straightforward. Since ROOMs of a certain vnum always exist, using getroom() will always allow you to access that ROOMs properties and methods. For instance, we can turn a "dormant" EXIT visible upon a mob's death:
 +
 +<code lua>
 +-- DEATH. Make the exit up from 22000 visible.
 +getroom(22000).up:setflag("dormant", false)
 +</code>
 +
 +Again, there's no need for any checks, because if the room exists it always exists.
  
lua/scriptenvironments.1445060812.txt.gz · Last modified: 2015/10/17 05:46 by aethyn