The Object.ForEach... functions call your callback for each object that meets the condition. General rules:
- The callback receives 1 parameter, the object index (
aIndex). - The list is captured before iterating, so adding / removing objects during iteration does not break the loop.
- If the callback returns
false(strictly a boolean), the loop stops; any other return value (or no return value) continues. - If the callback raises an error, the loop stops and the error is reported to the script that called the iteration function (GameServer is not affected).
- If the callback parameter is not a function, or the filter object (map, party, guild) does not exist, the function does nothing.
- These functions have no return value.
Object.ForEachPlayer
Iterates over every character that is in the game (PLAYING state).
Object.ForEachPlayer(callback)
| Parameter | Type | Description |
|---|---|---|
callback | function(aIndex) | Function called for each player |
Object.ForEachPlayer(function(aIndex)
Message.Send(aIndex, 1, "Hello everyone!")
end)
Object.ForEach
Iterates over every existing character, monster and NPC (empty slots are not included).
Object.ForEach(callback)
| Parameter | Type | Description |
|---|---|---|
callback | function(aIndex) | Function called for each object |
local count = 0
Object.ForEach(function(aIndex) count = count + 1 end)
Object.ForEachPlayerOnMap
Iterates over the players in the game on a map.
Object.ForEachPlayerOnMap(map, callback)
| Parameter | Type | Description |
|---|---|---|
map | integer | Map number (must exist on this GameServer) |
callback | function(aIndex) | Function called for each player |
Object.ForEachPlayerOnMap(MAP_LORENCIA, function(aIndex)
Message.Send(aIndex, 0, "The event has started!")
end)
Object.ForEachMonster
Iterates over every living, active monster on the whole server.
Object.ForEachMonster(callback)
| Parameter | Type | Description |
|---|---|---|
callback | function(aIndex) | Function called for each monster |
Object.ForEachMonster(function(aIndex) Log.Add(Object.GetName(aIndex)) end)
Object.ForEachMonsterOnMap
Iterates over the living monsters on a map.
Object.ForEachMonsterOnMap(map, callback)
| Parameter | Type | Description |
|---|---|---|
map | integer | Map number (0 or higher; for a negative number the function does nothing) |
callback | function(aIndex) | Function called for each monster |
Object.ForEachMonsterOnMap(MAP_DEVIAS, function(aIndex) Object.Kill(aIndex) end)
Object.ForEachMonsterByClass
Iterates over the living monsters of a given monster code, on every map.
Object.ForEachMonsterByClass(class, callback)
| Parameter | Type | Description |
|---|---|---|
class | integer | Monster code (0 or higher; for a negative number the function does nothing) |
callback | function(aIndex) | Function called for each monster |
Object.ForEachMonsterByClass(44, function(aIndex) Log.Add("Found monster 44: " .. aIndex) end)
Object.ForEachPartyMember
Iterates over the party members who are online on this GameServer.
Object.ForEachPartyMember(partyNumber, callback)
| Parameter | Type | Description |
|---|---|---|
partyNumber | integer | Party number, obtained from Player.GetPartyNumber |
callback | function(aIndex) | Function called for each member |
local party = Player.GetPartyNumber(aIndex)
if party >= 0 then
Object.ForEachPartyMember(party, function(member) Message.Send(member, 1, "The whole party receives a gift!") end)
end
Object.ForEachGuildMember
Iterates over the guild members who are online on this GameServer.
Object.ForEachGuildMember(guild, callback)
| Parameter | Type | Description |
|---|---|---|
guild | integer or string | Guild number (greater than 0, obtained from Player.GetGuildNumber) or guild name |
callback | function(aIndex) | Function called for each member |
Object.ForEachGuildMember(Player.GetGuildName(aIndex), function(member)
Message.Send(member, 2, "Guild announcement")
end)
Object.ForEachNearby
Iterates over the active characters and monsters, on the same map and in the same instance, within a radius around an object. The center object itself is not included.
Object.ForEachNearby(aIndex, range, callback)
| Parameter | Type | Description |
|---|---|---|
aIndex | integer | Index of the center object |
range | integer | Radius in cells (clamped to 0-255) |
callback | function(aIndex) | Function called for each object within the radius (only living objects are included) |
Object.ForEachNearby(aIndex, 5, function(other)
if Object.GetType(other) == OBJECT_MONSTER then Object.Kill(other) end
end)
Object.CountPlayersOnMap
Counts the players in the game on a map.
Object.CountPlayersOnMap(map) -> integer
| Parameter | Type | Description |
|---|---|---|
map | integer | Map number |
Returns: the number of players; 0 if the map does not exist on this GameServer.
if Object.CountPlayersOnMap(MAP_ARENA) >= 10 then ... end
Object.CountMonstersOnMap
Counts the living monsters on a map.
Object.CountMonstersOnMap(map [, class]) -> integer
| Parameter | Type | Description |
|---|---|---|
map | integer | Map number |
class | integer (optional) | Monster code; omitted = all monster types |
Returns: the number of monsters counted.
local boss = Object.CountMonstersOnMap(MAP_KALIMA1, 100)