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 20:51] – [Finding the correct environment] 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 62: Line 64:
 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. 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() ====+==== 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> <code lua>
Line 81: Line 85:
  
 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. 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.1445115100.txt.gz · Last modified: 2015/10/17 20:51 by aethyn