LuaLibraryTracker: clarify runOn
authorWuzzy
Thu, 03 May 2018 01:37:28 +0100
changeset 1397 63655141bb8f
parent 1396 d0f79e828581
child 1398 704301f9bd1d
LuaLibraryTracker: clarify runOn
LuaLibraryTracker.wiki
--- a/LuaLibraryTracker.wiki	Thu May 03 01:23:14 2018 +0100
+++ b/LuaLibraryTracker.wiki	Thu May 03 01:37:28 2018 +0100
@@ -8,23 +8,31 @@
 
 To set it up you need to add some hooks in different events. The hooks `trackGear` and `trackDeletion` to `onGearAdd` and `onGearDelete` respectively. It is strongly recommended to only track the gears you are interested in as, especially with snow on, the amount of gears can go up high and that will slow down the script. To keep track of teams you need to keep track of `gtHedgehog` and `gtResurrector` (if resurrection might be used) and add `trackTeams` to `onGameStart`.
 
-If you want to call a function on a couple of gears you will have to call the “`runOn`” functions. To these you will pass the function you want to be run as a variable to the function. The function must take a gear as a parameter, nothing else, for example:
-<code language="lua">function killall(gear)
+If you want to call a function on a couple of gears you will have to call the “`runOn`” functions. To these you will pass the function you want to be run as a variable to the function.
+
+=== Examples ===
+Here's a minimal example which tracks all hedgehogs and kills them all when a target is destroyed:
+<code language="lua">HedgewarsScriptLoad("Scripts/Tracker.lua")
+
+function onGearAdd(gear)
+    if GetGearType(gear) == gtHedgehog then
+        trackGear(gear)
+    end
+end
+
+local function killAll(gear)
     SetHealth(gear, 0)
 end
 
 function onGearDelete(gear)
     if GetGearType(gear) == gtTarget then
-        runOnHogs(killall)
+        runOnGears(killAll)
     end
 end</code>
-This will kill all hogs if a target is destroyed.
 
 To see a commented example of `Tracker` in use by a script you can look at
 [https://hg.hedgewars.org/hedgewars/file/default/share/hedgewars/Data/Scripts/Multiplayer/Random_Weapon.lua Random Weapons].
 
-
-
 == Tracking functions ==
 These functions must be called to start tracking stuff. *IMPORTANT:* The other functions will only work on tracked objects. So make sure you track the things you care about.
 
@@ -41,12 +49,12 @@
 Will keep track of the given hedgehog gear when it becomes restored, i.e. no longer hidden (e.g. with `RestoreHog`).
 
 === `trackTeams()` ===
-Will start the tracking of teams, clans and hedgehogs (hogs can be tracked without this). If you want to use any of the team or clan-related functions below, you *must* call this function in `onGameStart`.
+Will start the tracking of teams, clans and hedgehogs (individual hogs can be tracked without this). If you want to use any of the team or clan-related functions below, you *must* call this function in `onGameStart`.
 
 
 
 == “`runOn`” functions ==
-These functions are used to run a function on _tracked_ gears, teams and clans.
+These functions are used to run a function on _tracked_ gears, teams and clans. The passed function is assumed to take a single argument, which is a gear ID.
 
 === `runOnGears(func)` ===
 Runs the function `func` on all tracked gears.
@@ -54,18 +62,28 @@
 === `runOnHogs(func)` ===
 Runs the function `func` on all tracked hedgehogs.
 
+Requires `trackTeams` to be called beforehand.
+
 === `runOnHogsInTeam(func, team)` ===
 Runs the function `func` on all tracked hedgehogs in the specified team (`team` = team name).
 
+Requires `trackTeams` to be called beforehand.
+
 === `runOnHogsInOtherTeams(func, team)` ===
 Runs the function `func` on all tracked hedgehogs but those in the specified team (`team` = team name).
 
+Requires `trackTeams` to be called beforehand.
+
 === `runOnHogsInClan(func, clan)` ===
 Runs the function `func` on all tracked hedgehogs in the specified clan (`clan` = clan ID).
 
+Requires `trackTeams` to be called beforehand.
+
 === `runOnHogsInOtherClans(func, clan)` ===
 Runs the function `func` on all tracked hedgehogs but those in the specified clan (`clan` = clan ID).
 
+Requires `trackTeams` to be called beforehand.
+
 
 
 == Helper functions ==