/usr/share/doc/sludge/SLUDGEDevKitHelp/Passing_Functions_as_Variables.html is in sludge-doc 2.2.1-2build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<TITLE>Passing Functions as Variables</TITLE>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<BODY>
<HR>
<div align="center"><img id="headerGraphic" src="images/sludge300.png" alt="SLUDGE"/></div>
<h2>Passing Functions as Variables</h2>
<HR>
<P>
A variable can be used to hold a number, a line of text, an object type... and a function. This is useful if you have a large number of functions which should all be called in the same way... for example, the functions which are used to set up the scenes of your game.
</P>
<P>
Rather than having a program defined as follows...
</P>
<P>
<pre>var lastRoom; # The room we just came from (string)
var thisRoom; # The room we're in now (string)
sub goToDiningRoom () {
removeAllCharacters ();
removeScreenRegions ();
lastRoom = thisRoom;
thisRoom = "dining room";
completeTimers ();
addOverlay ('diningRoom.tga', 0, 0);
setFloor ('diningRoom.flo');
# More dining room initialisation here
}
sub goToLounge () {
removeAllCharacters ();
removeScreenRegions ();
lastRoom = thisRoom;
thisRoom = "lounge";
completeTimers ();
addOverlay ('lounge.tga', 0, 0);
setFloor ('lounge.flo');
# More lounge initialisation here
}</pre>
</P>
<P>
It would be beneficial to have a number of rooms and one function for changing between them.
</P>
<P>
<pre>var lastRoom; # The room we just came from (function handle)
var thisRoom; # The room we're in now (function handle)
sub goToRoom (newRoom) {
removeAllCharacters ();
removeScreenRegions ();
lastRoom = thisRoom;
thisRoom = newRoom;
completeTimers ();
newRoom ();
}
sub diningRoom () {
addOverlay ('diningRoom.tga', 0, 0);
setFloor ('diningRoom.flo');
# More dining room initialisation here
}
sub lounge () {
addOverlay ('lounge.tga', 0, 0);
setFloor ('lounge.flo');
# More lounge initialisation here
}</pre>
</P>
<P>
You can see that, at the end of the goToRoom sub, newRoom can be called just like a function even though it's actually a variable. This is because if your program was to include a line such as
</P>
<P>
<pre>goToRoom (lounge);</pre>
</P>
<P>
...the value of newRoom would be a pointer to the lounge function. Therefore, the lounge function would be called. You can even pass pointers to functions which take variables, although there are probably very few situations where you would want to...
</P>
<P>
<pre>sub doRepeatingMaths (number, mathFunc, mathSign) {
var total = number;
var newTotal;
for (var a = 0; a < 5; a ++) {
newTotal = mathFunc (total, number);
say (ego, total + mathSign + number + " = " + newTotal);
total = newTotal;
}
}
sub minus (a, b) { return a - b; }
sub plus (a, b) { return a + b; }
sub times (a, b) { return a * b; }</pre>
</P>
<P>
Using the above code, a call to doRepeatingMaths (2, plus, "+") would make the ego character say the following:
</P>
<P>
2+2 = 4
</P>
<P>
4+2 = 6
</P>
<P>
6+2 = 8
</P>
<P>
8+2 = 10
</P>
<P>
10+2 = 12
</P>
<P>
A call to doRepeatingMaths (3, times, "*") would make the ego character say:
</P>
<P>
3*3 = 9
</P>
<P>
9*3 = 27
</P>
<P>
27*3 = 81
</P>
<P>
81*3 = 243
</P>
<P>
243*3 = 729
</P>
<H3>See also:</H3>
<P>
<a href="Variables.html">Variables</a>
</P>
<P>
<a href="onFocusChange.html">onFocusChange</a>
</P>
<P>
<a href="onKeyboard.html">onKeyboard</a>
</P>
<P>
<a href="onLeftMouse.html">onLeftMouse</a>
</P>
<P>
<a href="onMoveMouse.html">onMoveMouse</a>
</P>
<P>
<a href="onRightMouse.html">onRightMouse</a>
</P>
<P class="copyright-notice">SLUDGE and this SLUDGE documentation are <A HREF="Copyright.html">copyright</A> Hungry Software and contributors 2000-2012
</P>
<HR>
</BODY>
</html>
|