# HG changeset patch # User Wuzzy # Date 1525307848 -3600 # Node ID 63655141bb8f12cacffdce20cb12005e5ad0e6e7 # Parent d0f79e82858129c94f7fb69778e834740b12370b LuaLibraryTracker: clarify runOn diff -r d0f79e828581 -r 63655141bb8f 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: -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: +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 -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 ==