LuaAPI.wiki
changeset 404 37d44392ad2b
parent 403 7642806ce7b9
child 405 4e1c166740b0
equal deleted inserted replaced
403:7642806ce7b9 404:37d44392ad2b
     1 #summary API for writing Lua scripts in Hedgewars.
     1 #summary API for writing Lua scripts in Hedgewars.
     2 #labels Featured
     2 #labels Featured
     3 <wiki:toc max_depth="3" />
     3 <wiki:toc max_depth="4" />
     4 
     4 
     5 == Introduction ==
     5 == Introduction ==
     6 
     6 
     7 Version 0.9.13 of Hedgewars introduced the ability to use Lua scripts to modify Hedgewars behaviour for different maps without having to recompile the whole game. The till then used triggers (only appeared in training maps) were removed.
     7 Version 0.9.13 of Hedgewars introduced the ability to use Lua scripts to modify Hedgewars behaviour for different maps without having to recompile the whole game. The till then used triggers (only appeared in training maps) were removed.
     8 
     8 
   672 
   672 
   673 === <tt>!SetGearPos(gearUid, value) (0.9.18-dev)</tt> ===
   673 === <tt>!SetGearPos(gearUid, value) (0.9.18-dev)</tt> ===
   674 
   674 
   675 <blockquote>Set pos of specified gear to specified value.</blockquote>
   675 <blockquote>Set pos of specified gear to specified value.</blockquote>
   676 
   676 
   677 == Other Functions ==
   677 
   678 
   678 == Gameplay functions ==
   679 === <tt>!ClearGameFlags()</tt> ===
   679 
       
   680 === `GameFlags` ===
       
   681 
       
   682 ==== <tt>!ClearGameFlags()</tt> ====
   680 
   683 
   681 <blockquote>Disables *all* !GameFlags
   684 <blockquote>Disables *all* !GameFlags
   682 </blockquote>
   685 </blockquote>
   683 
   686 
   684 === <tt>!DisableGameFlags(gameflag, ...)</tt> ===
   687 ==== <tt>!DisableGameFlags(gameflag, ...)</tt> ====
   685 
   688 
   686 <blockquote>Disables the listed !GameFlags, without changing the status of other !GameFlags
   689 <blockquote>Disables the listed !GameFlags, without changing the status of other !GameFlags
   687 </blockquote>
   690 </blockquote>
   688 
   691 
   689 === <tt>!EnableGameFlags(gameflag, ...)</tt> ===
   692 ==== <tt>!EnableGameFlags(gameflag, ...)</tt> ====
   690 
   693 
   691 <blockquote>Enables the listed !GameFlags, without changing the status of other !GameFlags
   694 <blockquote>Enables the listed !GameFlags, without changing the status of other !GameFlags
   692 </blockquote>
   695 </blockquote>
   693 
   696 
   694 === <tt>!GetGameFlag(gameflag)</tt> ===
   697 ==== <tt>!GetGameFlag(gameflag)</tt> ====
   695 
   698 
   696 <blockquote>Returns true if the specified gameflag is enabled, otherwise false
   699 <blockquote>Returns true if the specified gameflag is enabled, otherwise false
   697 </blockquote>
   700 </blockquote>
   698 
   701 
   699 === <tt>!EndGame()</tt> ===
   702 === Environment ===
   700 
   703 
   701 <blockquote>Makes the game end.
   704 ==== <tt>!SetGravity(percent)</tt> ====
   702 </blockquote>
       
   703 
       
   704 === <tt>!ShowMission(caption, subcaption, text, icon, time)</tt> ===
       
   705 
       
   706 <blockquote>Use to tell the player what he is supposed to do.
       
   707 
       
   708 As of (0.9.15), icon currently accepts:
       
   709  * -amBazooka, -amShotgun etc. (and other ammo types)
       
   710  * 0 (Gold Crown icon)
       
   711  * 1 (Target icon)
       
   712  * 2 (Exclamation mark)
       
   713  * 3 (Question mark)
       
   714  * 4 (Gold star)
       
   715 </blockquote>
       
   716 
       
   717 === <tt>!HideMission()</tt> ===
       
   718 
       
   719 <blockquote>Hides the mission. This function is currently bugged somehow and will completely ruin your life, and your script should you happen to use it.
       
   720 </blockquote>
       
   721 
       
   722 === <tt>!AddCaption(text)</tt> ===
       
   723 
       
   724 <blockquote>Display event text in the upper part of the screen.
       
   725 </blockquote>
       
   726 
       
   727 === <tt>!MapHasBorder()</tt> ===
       
   728 
       
   729 <blockquote>Returns true/false if the map has a border or not.
       
   730 </blockquote>
       
   731 
       
   732 === <tt>!TestRectForObstacle(x1, y1, x2, y2, landOnly) (0.9.16) </tt> ===
       
   733 
       
   734 <blockquote>Checks the rectangle between the given coordinates for possible collisions. set landOnly to true if you don't want to check for collisions with gears (hedgehogs, etc.)
       
   735 </blockquote>
       
   736 
       
   737 === <tt>!PlaySound(soundId)</tt> ===
       
   738 
       
   739 <blockquote>Plays the specified sound.
       
   740 </blockquote>
       
   741 
       
   742 === <tt>!PlaySound(soundId, gearUid)</tt> ===
       
   743 
       
   744 <blockquote>Plays the specified sound for the chosen hedgehog's team.
       
   745 </blockquote>
       
   746 
       
   747 === <tt>!SetInputMask(mask)</tt> ===
       
   748 
       
   749 <blockquote>Masks specified player input.
       
   750 </blockquote>
       
   751 Example: 
       
   752 <code lang="lua">    -- masks the long and high jump commands
       
   753 SetInputMask(band(0xFFFFFFFF, bnot(gmLJump + gmHJump))) 
       
   754     -- clears input mask, allowing player to take actions
       
   755     SetInputMask(0xFFFFFFFF) 
       
   756 		</code>
       
   757 Note: Using the input mask is an effective way to script uninterrupted cinematics, or create modes such as No Jumping. 
       
   758 
       
   759 
       
   760 === <tt>!SetGravity(percent)</tt> ===
       
   761 
   705 
   762 <blockquote>Changes the current gravity of the game in percent (relative to default, integer value).
   706 <blockquote>Changes the current gravity of the game in percent (relative to default, integer value).
   763 Setting it to 100 will set gravity to default gravity of hedgewars, 200 will double it, etc.
   707 Setting it to 100 will set gravity to default gravity of hedgewars, 200 will double it, etc.
   764 </blockquote>
   708 </blockquote>
   765 
   709 
   766 === <tt>!GetGravity()</tt> ===
   710 ==== <tt>!GetGravity()</tt> ====
   767 
   711 
   768 <blockquote>Returns the current gravity in percent
   712 <blockquote>Returns the current gravity in percent
   769 </blockquote>
   713 </blockquote>
   770 
   714 
   771 === <tt>!SetWaterLine(waterline)</tt> ===
   715 ==== <tt>!SetWaterLine(waterline)</tt> ====
   772 
   716 
   773 <blockquote>Sets the water level to the specified y-coordinate
   717 <blockquote>Sets the water level to the specified y-coordinate
   774 </blockquote>
   718 </blockquote>
   775 
   719 
   776 === <tt>!SetZoom(zoomLevel)</tt> ===
   720 ==== <tt>!SetWind(windSpeed)</tt> ====
   777 
       
   778 <blockquote>Sets the zoom level. The value for maximum zoom is currently 1.0 and for minimum 3.0 The default zoom level is 2.0
       
   779 </blockquote>
       
   780 
       
   781 === <tt>!GetZoom()</tt> ===
       
   782 
       
   783 <blockquote>Returns the current zoom level
       
   784 </blockquote>
       
   785 
       
   786 === <tt>!GetRandom(number)</tt> ===
       
   787 
       
   788 <blockquote>Returns a randomly generated number in the range of 0 to number - 1.  This random number uses the game seed, so is synchronised, and thus safe for multiplayer and saved games.  Use GetRandom for anything that could impact the engine state.  For example, a visual gear can use the Lua random, but adding a regular gear should use GetRandom.
       
   789 </blockquote>
       
   790 
       
   791 === <tt>!SetWind(windSpeed)</tt> ===
       
   792 
   721 
   793 <blockquote>Sets the current wind in the range of -100 to 100. Use together with gfDisableWind for full control.
   722 <blockquote>Sets the current wind in the range of -100 to 100. Use together with gfDisableWind for full control.
   794 </blockquote>
   723 </blockquote>
   795 
   724 
   796 === <tt>!GetDataPath()</tt> ===
   725 ==== <tt>!EndGame()</tt> ====
   797 
   726 
   798 <blockquote>Returns the path to the data directory, used when adding libraries.
   727 <blockquote>Makes the game end.
   799 </blockquote>
   728 </blockquote>
   800 
   729 
   801 === <tt>!GetUserDataPath()</tt> ===
   730 === Map ===
   802 
   731 ==== <tt>!MapHasBorder()</tt> ====
   803 <blockquote>Returns the path to the user data directory, used when adding libraries.
   732 
   804 </blockquote>
   733 <blockquote>Returns true/false if the map has a border or not.
   805 
   734 </blockquote>
   806 === <tt>!GetClanColor(clan)</tt> ===
   735 
   807 
   736 ==== <tt>!TestRectForObstacle(x1, y1, x2, y2, landOnly) (0.9.16) </tt> ====
   808 <blockquote>Returns the colour of the chosen clan by its number.
   737 <blockquote>Checks the rectangle between the given coordinates for possible collisions. set landOnly to true if you don't want to check for collisions with gears (hedgehogs, etc.)
   809 </blockquote>
   738 </blockquote>
   810 === <tt>!SetClanColor(clan, color)</tt> ===
   739 
   811 
   740 ==== <tt>!PlaceGirder(x, y, state)</tt> (0.9.16) ====
   812 <blockquote>Sets the colour of the chosen clan by its number.
       
   813 </blockquote>
       
   814 
       
   815 === <tt>!PlaceGirder(x, y, state)</tt> (0.9.16) ===
       
   816 
   741 
   817 <blockquote>Places a girder with centre points x, y and the chosen state.
   742 <blockquote>Places a girder with centre points x, y and the chosen state.
   818 These are the accepted states:
   743 These are the accepted states:
   819   * 0: short, horizontal
   744   * 0: short, horizontal
   820   * 1: short, decreasing right
   745   * 1: short, decreasing right
   824   * 5: long, decreasing right
   749   * 5: long, decreasing right
   825   * 6: long, vertical
   750   * 6: long, vertical
   826   * 7: long, increasing right
   751   * 7: long, increasing right
   827 </blockquote>
   752 </blockquote>
   828 
   753 
   829 === <tt>!GetCurAmmoType()</tt> (0.9.16) ===
   754 === Current hedgehog ===
       
   755 
       
   756 ==== <tt>!GetCurAmmoType()</tt> (0.9.16) ====
   830 
   757 
   831 <blockquote>Returns the currently selected [AmmoTypes Ammo Type].
   758 <blockquote>Returns the currently selected [AmmoTypes Ammo Type].
   832 </blockquote>
   759 </blockquote>
   833 
   760 
   834 === <tt>!SwitchHog(gearUid)</tt> (0.9.16) ===
   761 ==== <tt>!SwitchHog(gearUid)</tt> (0.9.16) ====
   835 
   762 
   836 <blockquote>This function will switch to the hedgehog with the specified Uid.</blockquote>
   763 <blockquote>This function will switch to the hedgehog with the specified Uid.</blockquote>
   837 
   764 
       
   765 ==== <tt>!SetInputMask(mask)</tt> ====
       
   766 
       
   767 <blockquote>Masks specified player input.
       
   768 </blockquote>
       
   769 Example: 
       
   770 <code lang="lua">    -- masks the long and high jump commands
       
   771 SetInputMask(band(0xFFFFFFFF, bnot(gmLJump + gmHJump))) 
       
   772     -- clears input mask, allowing player to take actions
       
   773     SetInputMask(0xFFFFFFFF) 
       
   774 		</code>
       
   775 Note: Using the input mask is an effective way to script uninterrupted cinematics, or create modes such as No Jumping. 
       
   776 
       
   777 
       
   778 === Randomness ===
       
   779 ==== <tt>!GetRandom(number)</tt> ====
       
   780 
       
   781 <blockquote>Returns a randomly generated number in the range of 0 to number - 1.  This random number uses the game seed, so is synchronised, and thus safe for multiplayer and saved games.  Use GetRandom for anything that could impact the engine state.  For example, a visual gear can use the Lua random, but adding a regular gear should use GetRandom.
       
   782 </blockquote>
       
   783 
       
   784 === Clans ===
       
   785 ==== <tt>!GetClanColor(clan)</tt> ====
       
   786 
       
   787 <blockquote>Returns the colour of the chosen clan by its number.
       
   788 </blockquote>
       
   789 ==== <tt>!SetClanColor(clan, color)</tt> ====
       
   790 
       
   791 <blockquote>Sets the colour of the chosen clan by its number.
       
   792 </blockquote>
       
   793 
       
   794 
       
   795 
       
   796 
       
   797 == Functions affecting the GUI ==
       
   798 
       
   799 === <tt>!AddCaption(text)</tt> ===
       
   800 
       
   801 <blockquote>Display event text in the upper part of the screen.
       
   802 </blockquote>
       
   803 
       
   804 === <tt>!ShowMission(caption, subcaption, text, icon, time)</tt> ===
       
   805 
       
   806 <blockquote>Use to tell the player what he is supposed to do.
       
   807 
       
   808 As of (0.9.15), icon currently accepts:
       
   809  * -amBazooka, -amShotgun etc. (and other ammo types)
       
   810  * 0 (Gold Crown icon)
       
   811  * 1 (Target icon)
       
   812  * 2 (Exclamation mark)
       
   813  * 3 (Question mark)
       
   814  * 4 (Gold star)
       
   815 </blockquote>
       
   816 
       
   817 === <tt>!HideMission()</tt> ===
       
   818 
       
   819 <blockquote>Hides the mission. This function is currently bugged somehow and will completely ruin your life, and your script should you happen to use it.
       
   820 </blockquote>
       
   821 
       
   822 === <tt>!SetZoom(zoomLevel)</tt> ===
       
   823 
       
   824 <blockquote>Sets the zoom level. The value for maximum zoom is currently 1.0 and for minimum 3.0 The default zoom level is 2.0
       
   825 </blockquote>
       
   826 
       
   827 === <tt>!GetZoom()</tt> ===
       
   828 
       
   829 <blockquote>Returns the current zoom level
       
   830 </blockquote>
       
   831 
       
   832 == Sound functions ==
       
   833 === <tt>!PlaySound(soundId)</tt> ===
       
   834 
       
   835 <blockquote>Plays the specified sound.
       
   836 </blockquote>
       
   837 
       
   838 === <tt>!PlaySound(soundId, gearUid)</tt> ===
       
   839 
       
   840 <blockquote>Plays the specified sound for the chosen hedgehog's team.
       
   841 </blockquote>
       
   842 
       
   843 
       
   844 == File system functions ==
       
   845 === <tt>!GetDataPath()</tt> ===
       
   846 
       
   847 <blockquote>Returns the path to the data directory, used when adding libraries.
       
   848 </blockquote>
       
   849 
       
   850 === <tt>!GetUserDataPath()</tt> ===
       
   851 
       
   852 <blockquote>Returns the path to the user data directory, used when adding libraries.
       
   853 </blockquote>
       
   854 
       
   855 
       
   856 
       
   857 
       
   858 == Stats functions ==
   838 === <tt>!SendStat(TStatInfoType, statMessage[, teamName])</tt> (0.9.20) ===
   859 === <tt>!SendStat(TStatInfoType, statMessage[, teamName])</tt> (0.9.20) ===
   839 
   860 
   840 <blockquote>Exposes the uIO SendStat to the lua scripts. Use it to produce custom stat pages.
   861 <blockquote>Exposes the uIO SendStat to the lua scripts. Use it to produce custom stat pages.
   841 
   862 
   842 <b>Examples:</b>
   863 <b>Examples:</b>