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 21:01] – [getpc() and getroom()] 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 95: Line 97:
 end end
 </code> </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.1445115666.txt.gz · Last modified: 2015/10/17 21:01 by aethyn