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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
|
===== 0. General information =====
Game code for STEAD is written in //lua// (5.1), therefore it is useful to know the language, though not necessary. The engine code in lua is about ~3000 lines long. And it is the best documentation.
The main game window contains information about static and dynamic parts of the scene, active events and the scene picture with possible passages to other scenes (in the graphic interpreter).
//Static part// of the scene is shown only once, when the player enters the scene. Also it is shown when the ''look'' command is repeated (click on the scene name in the graphic interpreter).
The //dynamic part// of a scene is composed of descriptions of the scene objects. It is always shown.
//Inventory// contains objects, that the player can access in every scene. The player can interact with the inventory objects or use inventory objects on other objects in the scene or inventory.
<WRAP center round info>
One should note that the “inventory” is defined rather vaguely. For example it may contain such objects as “open”, “examine”, “use”, etc.
</WRAP>
Possible actions of the player are:
* looking at the scene;
* using a scene object;
* using an inventory object;
* using an inventory object on a scene object;
* using an inventory object on an inventory object;
* using a scene object on a scene object (''scene_use'' mode);
* using a scene object on an inventroy object (''scene_use'' mode);
* passing to another scene;
Each game is a directory with a ''main.lua'' script. Other game resources (lua scripts, images, music) should be in this directory. All references to the resources are given relative to this game directory.
At the beginning of ''main.lua'' file a header may be defined. It consists of tags. Each tag should start with ''--'' symbols — lua comments. Right now only one tag exists: ''$Name:''. It should contain the name of the game in UTF-8 encoding. For example:
<code lua>
-- $Name: The most interesting game!$
</code>
From version 1.2.0 after headers you must define required STEAD API version. It is "1.6.0" currently.
<code lua>
instead_version "1.6.0"
</code>
<WRAP center round important>
Without this line STEAD API will stay in compatible(legacy) mode.
</WRAP>
//Game initialization// should be defined as init function. For example:
<code lua>
function init()
me()._know_truth = false
take(knife);
take(paper);
end
</code>
The graphic interpreter searches for available games in the ''games'' directory. Unix version also checks ''~/.instead/games''. Windows version (>=0.8.7) checks ''Documents and Settings/USER/Local Settings/Application Data/instead/games''.
From version 1.2.0 Windows and Unix standalone builds looks into ''./appdata/games'' dir, if it exists.
===== 1. Scene =====
A scene is a game unit. Within it a player can examine all the scene objects and interact with them. A game should contain at least one scene with the name ''main''.
<code lua>
main = room {
nam = 'main room',
dsc = 'You are in a large room.',
};
</code>
The record means creation of an object ''main'' of a type ''room''. Every object has attributes and handlers. For example the attribute ''nam'' (name) is obligatory for every object.
The ''nam'' attribute for a scene will be the scene name when it is played. The name of a scene is also used to identify it when passing between scenes.
The ''dsc'' attribute is a description of a static part of the scene. It is shown once when entering the scene or after the explicit ''look'' command.
Attention!!! You may use symbol “;” instead of “,”. For example:
<code lua>
main = room {
nam = 'main room';
dsc = 'You are in a large room.';
};
</code>
Attention!!! If your creative design requires the static part description to be shown every time, you may define the “forcedsc” parameter for your game (at the start).
<code lua>
game.forcedsc = true;
</code>
Or similarly set the “forcedsc” for particular scenes.
For long descriptions the following format is convenient:
<code lua>dsc = [[ Very long description... ]],</code>
In this format line breaks are ignored. If you need paragraph breaks in the description, use the “^” symbol.
<code lua>
dsc = [[ First paragraph. ^^
Second paragraph.^^
Third paragraph.^
New line.]],
</code>
===== 2. Objects =====
Objects are units of a scene, with which the player interacts.
<code>
tabl = obj {
nam = 'table',
dsc = 'There is a {table} in the room.',
act = 'Hm... Just a table...',
};
</code>
Object name “nam” is used when the object gets into the inventory or to address the object in a text interpreter.
“dsc” is an object descriptor. It will be shown in the dynamic part of the scene. Curly brackets indicate the text fragment which will be a link anchor in the graphic interpreter.
“act” is a handler, called when the player uses a scene object. It has to return a text line, which will become a part of the scene events, or a boolean value (see chapter 5)
WARNING: in the lua namespace some objects (tables) already exist. For example “table”, “io”, “string”... Be careful when creating objects. In the example above “tabl” is used instead of “table”. (But in fact, in modern versions of INSTEAD this problem is almost solved).
===== 3. Adding objects to the scene =====
A reference to an object is a text string, with the object name at its creation. For example 'tabl' is a reference to the object “tabl”.
To place objects to the scene one has to define the “obj” array of references to objects:
<code>
main = room {
nam = 'main room',
dsc = 'You are in a large room.',
obj = { 'tabl' },
};
</code>
This way when the scene is shown we'll see the table object in the dynamic part.
Attention!!! You can use references to objects without quotes, if the object was defined before reference, but using quotes is always safe.
===== 4. Objects referencing objects =====
Objects may contain “obj” attribute too. This way the list will expand sequentially. For example let's place an apple on the table.
<code>
aple = obj {
nam = 'apple',
dsc = 'There is an {apple} on the table.',
act = 'Should I take it?',
};
tabl = obj {
nam = 'table',
dsc = 'There is a {table} in the room.',
act = 'Hm... Just a table...',
obj = { 'aple' },
};
</code>
This way in the scene description we'll see descriptions of objects “table” and “apple”, because “aple” is an object referenced by tabl.
/* Translator's note: in the English version of the document I renamed the “apple” variable to “aple” to distinguish it from aple.nam */
===== 5. Attributes and handlers as functions =====
Most attributes and handlers may also be functions. For example:
<code>
nam = function()
return 'apple';
end,
</code>
This is synonymous to: nam = 'apple';
Handler must return string. You can also use more user-friendly functions:
* p ("text") -- output text with space;
* pn ("text") -- output text with newline;
* pr ("text") -- output text as-is;
If you call p/pn/pr with only one text parameter, the parentheses could be omitted. Use .. or , for string concatenation. For example:
<code>
pn "No parentheses";
pn ("This is string 1".." This is string 2");
pn ("This is string 1", "This is string 2");
</code>
Functions greatly enhance STEAD capabilities, for example:
<code>
aple = obj {
nam = 'apple',
dsc = function(s)
if not s._seen then
p 'There is {something} on the table.';
else
p 'There is an {apple} on the table.';
end
end,
act = function(s)
if s._seen then
p 'It\'s an apple!';
else
s._seen = true;
p 'Hm... But it\'s an apple!';
end
end,
};
</code>
If the attribute or handler is laid out as a function, then the first argument of the function (s) is the object itself. In the example scene the dynamic part will have the text: 'There is something on the table.' When you try to use this “something”, '_seen' variable of the object “aple” will be set to “true” and we will see it was an apple.
`s._seen` means that the `_seen` variable is placed in the “s” object (in our case “aple”). Underscore means that this variable is saved in a savegame file.
From version 1.2.0 you can define variables as shown in the example:
<code>
global {
global_var = 1;
}
main = room {
var {
i = "a";
z = "b";
};
nam = 'My first room';
var {
new_var = 3;
};
dsc = function(s)
p ("i == ", s.i);
p ("new_var == ", s.new_var);
p ("global_var == ", global_var);
end;
</code>
From version 1.2.0 you may define function like this:
<code>
dsc = code [[
if not self._seen then
p 'There is {something} on the table.';
else
p 'There is an {apple} on the table.';
end
]],
</code>
While running code, the object itself is written into "self" variable. arg1 .. arg9 and args[] array holds all arguments.
Warning! Variable will be saved to savegame file if it was defined in: room, game, obj, player, global space and it's name begins from _ symbol or if it was defined using var/global.
These types of variables can be saved:
* strings;
* boolean values;
* numeric values;
* links to objects;
* code constructions;
Sometimes we need a handler that would do something without showing any description, e.g.:
<code>
button = obj {
nam = "button",
dsc = "There is a big red {button} on the room wall.",
act = function (s)
here()._dynamic_dsc = [[The room transformed after I pressed the button.
The book-case disappeared along with the table and the chest, and a strange
looking device took its place.]];
return true;
end,
}
r12 = room {
nam = 'room',
_dynamic_dsc = 'I am in the room.',
dsc = function (s) return s._dynamic_dsc end,
obj = {'button'}
}
</code>
In this case ''act'' handler is used to change room description but it is not supposed to add any description of its own. To achieve this we need to return true from the handler. It means the action is done successfully but does not require to diplay any additional description.
If you need to show some action is impossible, just don't return anything from handler. In this case default description will be shown for this action. Default actions can be set via ''game.act'' handler and are generally used for description of impossible actions.
Please note the new variable ''_dynamic_dsc'' is used to make a dynamic description in the above example. This is done to ensure new description is saved during the game save. Since the name 'dsc' does not start with underscore or capital letter it will not be saved by default.
So, example above can be looks like this:
<code>
button = obj {
nam = "button";
dsc = "There is a big red {button} on the room wall.";
act = function (s)
here().dsc = "The room looks strange now...";
p [[The room transformed after I pressed the button.
The book-case disappeared along with the table and the chest, and a strange
looking device took its place.]];
end
}
r12 = room {
forcedsc = true;
nam = 'room';
var {
dsc = 'I am in the room.';
};
obj = {'button'}
}
</code>
===== 6. Inventory =====
The easiest way to create a takeable object is to define a “tak” handler.
For example:
<code>
aple = obj {
nam = 'apple',
dsc = 'There is an {apple} on the table.',
inv = function(s)
inv():del(s);
return 'I ate the apple.';
end,
tak = 'You take the apple.',
};
</code>
This way when the player uses the “apple” object the apple is removed from the scene and added to the inventory. When the player uses the inventory “inv” handler is called.
In the present example when the player uses the apple in the inventory, the apple is eaten.
===== 7. Passing between the scenes =====
To pass from one scene to another use the scene attribute — the “way” list.
<code>
room2 = room {
nam = 'hall',
dsc = 'You are in a huge hall.',
way = { 'main' },
};
main = room {
nam = 'main room',
dsc = 'You are in a large room.',
obj = { 'tabl' },
way = { 'room2' },
};
</code>
This way you can pass between ”main” and “room2” scenes. As you remember, “nam” may be a function, and you can generate scene names on the fly. For example if you don't want the player to know the name of the scene until he gets there.
When switching between scenes the engine calls the “exit” handler from the current scene and the “enter” from the destination scene. For example:
<code>
room2 = room {
enter = 'You enter the hall.',
nam = 'hall',
dsc = 'You are in a huge hall.',
way = { 'main' },
exit = 'You leave the hall.',
};
</code>
“exit” and “enter” may be functions. Then the first parameter is the object itself (as usual) and the second parameter is the room where the player is heading (for “exit”) or which he is leaving (for “enter”). For example:
<code>
room2 = room {
enter = function(s, f)
if f == main then
return 'You came from the room.';
end
end,
nam = 'hall',
dsc = 'You are in a huge hall.',
way = { 'main' },
exit = function(s, t)
if t == main then
return 'I don\'t wanna go back!', false
end
end,
};
</code>
As we see, the handlers can return two values: the string and the status. In our example the “exit” function returns “false” if the player tries to go to the “main” room from the hall. “false” means that the player will not pass. Same logic works for “enter” and “tak”.
If you like p/pn/pr instead, just return status itself:
<code>
room2 = room {
enter = function(s, f)
if f == main then
p 'You came from the room.';
end
end,
nam = 'hall',
dsc = 'You are in a huge hall.',
way = { 'main' },
exit = function(s, t)
if t == main then
p 'I don\'t wanna go back!'
return false
end
end,
};
</code>
Keep in mind that the current scene may not be changed by the moment of an enter action handler invocation. Since the version 1.2.0 available two new action handlers: 'left' and 'entered'. They are invoked immediately after the transition and recommended to use in case when transition prohibition is not required.
===== 8. Using an object on an object =====
The player may use an inventory object on other objects. In this case “use” handler is invoked for the object in the inventory and “used” for the other one.
For example:
<code>
knife = obj {
nam = 'knife',
dsc = 'There is a {knife} on the table',
inv = 'Sharp!',
tak = 'I took the knife!',
use = 'You try to use the knife.',
};
tabl = obj {
nam = 'table',
dsc = 'There is a {table} in the room.',
act = 'Hm... Just a table...',
obj = { 'aple', 'knife' },
used = 'You try to do something with the table...',
};
</code>
If the player takes the knife and uses it on the table, he gets the text of “use” and “used” hanlers. “use” and “used” may be functions. Then the first parameter is the object itself. The second parameter for “use” is the object being subjected to the action and fot “used” is the object performing the action.
If “use” returns “false” status, then “used” is not invoked (if there is one). The status of “used” is ignored.
Example:
<code>
knife = obj {
nam = 'knife',
dsc = 'There is a knife on the {table}',
inv = 'Sharp!',
tak = 'I took the knife!',
use = function(s, w)
if w ~= tabl then
p 'I don\'t want to cut this.'
return false
else
p 'You incise your initials on the table.';
end
};
</code>
You can use the knife only on the table.
===== 9. Player object =====
In STEAD the player is represented by the object “pl”. The object type is “player”. In the engine it's created thie way:
<code>
pl = player {
nam = "Incognito",
where = 'main',
obj = { }
};
</code>
The “obj” attribute represents the player's inventory.
===== 10. The object “game” =====
The game is also represented by the object “game” of type “game”. In the engine it is defined this way:
<code>
game = game {
nam = "INSTEAD -- Simple Text Adventure interpreter v"..version.." '2009 by Peter Kosyh",
dsc = [[
Commands:^
look(or just enter), act <on what> (or just what), use <what> [on what], go <where>,^
back, inv, way, obj, quit, save <fname>, load <fname>.]],
pl ='pl',
showlast = true,
};
</code>
As we can see, the object keeps the reference to the current player ('pl') and some parameters. For example at the start of your game you can set the encoding the following way:
<code>game.codepage="UTF-8"; </code>
The support of arbitrary encodings is present in every UNIX version of the interpreter and in windows versions from 0.7.7.
Also the object “game” may contain the default handlers: “act”, “inv”, “use”. They will be invoked if no other handlers are found after the user's actions. For example you can write at the game start:
<code>
game.act = 'You can\'t.';
game.inv = 'Hmm... Odd thing...';
game.use = 'Won\'t work...';
</code>
===== 11. Attribute lists =====
Attribute lists (such as “way” or “obj”) allow to work with themselves thus allowing to implement dynamically defined passages between scenes, live objects, etc.
List methods are: “add”, “del”, “look”, “srch”, “purge”, “replace”. The most used are “add” and “del”.
“add” adds to the list, “del” removes from the list, “purge” removes even disabled object, “srch” performs a search. “replace” replaces object. Note that “del”, “purge”, “replace” and “srch” may use as a parameter not only the object itself or its identifier, but also the object name.
Starting from version 0.8 the object itself may be a parameter of “add”. Also from this version an optional second parameter is added — position in list. From 0.8 you also can modify the list by the index with the “set” method. For example:
<code>
objs():set('knife',1);
</code>
You've seen the above example with the eaten apple. It used inv():del('aple');
“inv()” is a function, which returns the inventory list. “del” after “:” is a method, that deletes an element of the inventory.
Similarly, “tak” may be implemented this way:
<code>
knife = obj {
nam = 'knife',
dsc = 'There is a {knife} on the table,
inv = 'Sharp!',
act = function(s)
objs():del(s);
inv():add(s);
end,
};
</code>
Apart from adding and deleting objects from lists you may switch them on and off with “enable()” and “disable()” methods. E.g. “knife:disable()”. This way the object “knife” will disappear from the scene description, but may be switched on later with “knife:enable()”.
“enable_all()” and “disable_all()” methods works (from 0.9.1) with embedded objects (objects in object).
From version 0.9.1 methods “zap” and “cat” can be used. zap() -- delete all elements. cat(b, [pos]) -- add all elements of list b to current list at position [pos].
Attention!!! Currently, it is recommended to use higher lever functions like: put/get/take/drop/remove/seen/have and so on, to work with objects and inventory.
===== 12. Functions, that return objects =====
In STEAD several functions are defined, that return some frequently used objects. For example:
* inv() returns the inventory list;
* objs() returns the list of objects of the current scene; (from 0.8.5 it has an optional paremeter — the scene for which to return objects;
* ways() returns the list of passages from the current scene; (from 0.8.5 has an optional paremeter — the scene for which to return the list);
* me() returns the player object;
* here() returns the current scene; (from 0.8.5 another function where(obj) returns the scene where is object placed. Works only if it was placed with put/drop/move).
* from() returns an object from a previous scene;
* seen(obj, [scene]) returns an object, if it exists in scene and not disabled;
* have(obj, [scene]) returns an object, if it exists in the inventory;
* exist(obj, [scene]) returns an object, if it is exists in scene;
* live(obj) returns an object, if it is exists in lifes;
* path(obj,[where]) – lookup in way, even for disabled items;
Combining those functions with “add” and “del” methods one can dynamically alter the scene, for example:
<code>
ways():add('nexroom'); — add a passage to a new scene;
</code>
<code>
objs():add('chair'); — add an object to the current scene;
</code>
Another function gets an object by reference:
ref().
For example we can add an object to the 'home' location like this:
<code>
ref('home').obj:add('chair');
</code>
This shorter variant is also correct:
<code>
home.obj:add('chair');
</code>
Or, for version >=0.8.5:
<code>
objs('home'):add('chair');
</code>
and finally:
<code>
put('chair', 'home');
</code>
or even:
<code>
put(chair, home);
</code>
From 0.8.5 deref(o), returns the reference-string for an object;
===== 13. Some auxiliary functions. =====
STEAD has a number of high-level functions, that may come useful when writing games.
have() — checks if the object is in the inventory by object, it's reference or by object “nam” attribute. For example:
<code>
...
act = function(s)
if have('knife') then
return 'But I\'ve got a knife!';
end
end
...
</code>
Next code examples must work to.
<code>
...
if have 'knife' then
...
if have (knife) then
...
</code>
In the next examples you may use all above address modes.
move(o, w) — moves an object from the current scene to another:
<code>move('mycat','inmycar');</code>
If you want to move an object from an arbitrary scene, you'll have to delete it from the original scene with the “del” method. To create objects, that move in complex ways, you'll have to write a method that would save the object's position in the object itself and delete it from the original scene. You can set the initial position (room) as the third parameter of “move”.
<code>move('mycat','inmycar', 'forest'); </code>
From version 0.8 there is a “movef” function similar to “move”, but adding the object to the start of the list.
seen(o) — is object present in the current scene:
<code>
if seen('mycat') then
move('mycat','inmycar');
end
</code>
From 0.8.6 has an optional second parameter — the scene.
drop(o) — drop an object from the inventory to the scene:
drop('knife');
From 0.8 there's a function “dropf” similar to “drop”, but adding the object to the list start. From 0.8.5 there's an optional second parameter — a room where to place the object. Also from >=0.8.5 there are “put/putf” functions which are not remove the object from the inventory.
From version 0.8.9 there's a function remove(o, [from]), which deletes an object from the current scene or from the “from” scene.
take(o) — take an object.
take('knife');
From 0.8.5 has optional second parameter — a room where to take the object from.
taken(o) — returns true if the object has already been taken (with “tak” or “take()”);
rnd(m) — random number from 1 to m.
walk(w) — go to scene w. If you are not using p/pn/pr output method, the handler has to return the “walk” return value. E.g:
<code>
act = code [[
pn "I am going to next room..."
walk (nextroom);
]]
...
act = code [[
return cat('I am going to next room...', walk (nextroom));
]]
</code>
Attention!!! After walk call, handler execution will continue until handler end or return.
change_pl(p) — switch to another player (with one's own inventory and position). The function returns the scene description of the new player and the returned value has to be transferred from the handler (see “walk()”).
<code>
mycar = obj {
nam = 'my car',
dsc = 'In front of the cabin there is my old Toyota {pickup}.',
act = function(s)
return walk('inmycar');
end
};
</code>
walkback() -- go to the previous scene.
back() -- go to the previous scene. In case of returning from dialog to room, dsc/enter/entered methods of room will not be called. Use in dialogs.
walkin(room) -- go to scene, do not call exit/left of current scene;
walkout() -- go to previous scene, do not call enter/entered of previous scene;
time() — returns the current game time in player's moves.
cat(...) — returns the string, concatenating argument strings. If the first argument is nil returns nil.
par(...) — returns the string, concatenating argument strings split by the first argument string.
disable/enable/disable_all/enable_all -- disable/enable/disable_all/enable_all object
visited([where]) -- room visit counter (may be nil);
path(obj,[where]) -- lookup in way, even for disabled items;
nameof(obj) -- get object's name (nam attribute);
purge (obj, [where]) -- see remove, deletes even disabled object;
replace (obj, onobj, [where]) -- replaces object;
disabled(obj) -- returns true for disabled objects;
===== 14. Dialogs =====
Dialogs are scenes with phrase objects. The simplest dialog may look like this:
<code>
povardlg = dlg {
nam = 'in the kitchen',
dsc = 'I see a fat face of a lady cook wearing a white hat with a tired look...',
obj = {
[1] = phr('“Those green, please... Yeah, and beans too!”', '“Enjoy!”'),
[2] = phr('“Fried potato with lard, please!”', '“Bon appetit!”'),
[3] = phr('“Two helpings of garlic sooup!!!”', '“Good choice!”'),
[4] = phr('“Something light, please, I've got an ulcer...”', '“Oatmeal!”'),
},
};
</code>
“phr” creates a phrase. A phrase contains a question, an answer and a reaction (the example has no reaction). When the player picks one of the phrases, it is disabled. When all phrases are disabled, the dialog is over. Reaction is a line of lua code, which is executed when the phrase is disabled. E.g.:
<code>
food = obj {
nam = 'food',
inv = function (s)
inv():del('food');
return 'I eat.';
end
};
gotfood = function(w)
inv():add('food');
food._num = w;
back();
end
povardlg = dlg {
nam = 'in the kitchen',
dsc = 'I see a fat face of a lady cook wearing a white hat with a tired look...',
obj = {
[1] = phr('“Those green, please... Yeah, and beans too!”', '“Enjoy!”', [[pon(); gotfood(1);]]),
[2] = phr('“Fried potato with lard, please!”', '“Bon appetit!”', [[pon(); gotfood(2);]]),
[3] = phr('“Two helpings of garlic sooup!!!”', '“Good choice!”', [[pon(); gotfood(3);]]),
[4] = phr('“Something light, please, I've got an ulcer...”', '“Oatmeal!”', [[pon(); gotfood(4);]]),
},
};
</code>
In the example the player chooses his dinner. After getting the food (recording the choice in the “food._num” variable) he returns back to the scene from where he got in the dialog.
The reaction may have any lua code, but STEAD has some frequently used functions predefined:
* pon(n..) — enable the phrases with numbers n... (in the example it allows to take the same food again).
* poff(n...) — disable the phrases with numbers n...
* prem(n...) — remove (block) phrases with numbers n... (blocked phrases won't be re-enabled with subsequent “pon”).
* pseen(n...) — returns true, if phrases n.. are visible;
* punseen(n..) — returns true, if phrases n... are not visible;
If argument n is not present, current phrase will be affected.
Player enters a dialog the way he enters a scene:
<code>
povar = obj {
nam = 'cook',
dsc = 'I see a {cook}.',
act = function()
return walk('povardlg');
end,
};
</code>
You can pass from one dialog to another, organizing hierarchic dialogs.
You can also hide some phrases when initializing the dialog and show them under certain conditions.
<code>
facectrl = dlg {
nam = 'facecontrol',
dsc = 'I see an unpleasant face of a fat guard.',
obj = {
[1] = phr('“I came to the Belin's lecture...”',
'“I do not know who you are,” he smiles, “but I have orders to let in only decent people.”',
[[pon(2);]]),
[2] = _phr('“I\'ve got an invitation!”',
'“And I don\'t care! Look at yourself in a mirror!!! You\'ve come to listen to Belin himself — the right hand of...” he made a respectful pause. “So get lost...”', [[pon(3,4)]]),
[3] = _phr(' “I\'m gonna kick your ass!”', '“I\'ve had enough...” Strong arms push me out to the corridor...',
[[poff(4)]]),
[4] = _phr('“You, boar! I\'ve told you, I\'ve got an invitation!”',
'“Whaaat?” The guard\'s eyes start getting bloodshot... A powerful kick sends me out to the corridor...',
[[poff(3)]]),
},
exit = function(s,w)
s:pon(1);
end,
};
</code>
`_phr` — creates a disabled phrase, which can be enabled. The example also shows the use of “pon”, “poff”, “prem” methods for a dialog (see “exit”).
You can enable/disable/remove/check phrases not only of the current put of any arbitrary dialog with the “pon”/“poff”/“prem”/“pseen”/“pusneen” methods of a dialog object. For example: shopman:pon(5);
===== 15. Lightweight objects =====
Sometimes a scene has to be filled with decorations with a limited functionality to add variety to the game. For that lightweight objects can be used. For example:
<code>
sside = room {
nam = 'southern side',
dsc = [[I am near the southern wall of an institute building. ]],
act = function(s, w)
if w == "porch" then
ways():add('stolcorridor');
p "I walked to the porch. The sign on the door read 'Canteen'. Hm... should I get in?";
elseif w == "people" then
p 'The ones going out look happier...';
end
end,
obj = { vobj("porch", "There is a small {porch} by the eastern corner."),
vobj("people", "From time to time the porch door slams letting {people} in and out..")},
};
</code>
As you see, “vobj” allows to create a lightweight version of a static object, with which it will still be possible to interact (defining an “act” handler in the scene an analyzing the object name). “vobj” also calls the “used” method with the third parameter being the object which acts on the virtual object.
“vobj” syntax: vobj(name, descriptor); where key is a number to be transferred to the “act”/“used” handlers of the scene as a second parameter.
There is a modification of “vobj” object — “vway”. It creates a reference.
“vway” syntax: vway(name, descriptor, destination scene); for example:
<code>
obj = { vway("next", "Press {here}.", 'nextroom') }
</code>
You can dynamically fill the scene with “vobj” and “vway” objects. Use methods “add” and “del”. For example:
<code>
objs(home):add(vway("next", "{Next}.", 'next_room'));
-- some code here
home.obj:del("next");
</code>
Also a simplified scene “vroom” is defined.
Syntax: vroom(passage name, destination scene). For example:
<code>
home.way:add(vroom("go west", 'mountains');
</code>
===== 16. Dynamic events =====
You can define handlers, that would execute every time when the game time increments by 1. E.g.:
<code>
mycat = obj {
nam = 'Barsik',
lf = {
[1] = 'Barsik is moving in my bosom.',
[2] = 'Barsik peers out of my bosom.',
[3] = 'Barsik purrs in my bosom.',
[4] = 'Barsik shivers in my bosom.',
[5] = 'I feel Barsik's warmth in my bosom.',
[6] = 'Barsik leans out of my bosom and looks around.',
},
life = function(s)
local r = rnd(6);
if r > 2 then
return;
end
r = rnd(6);
return s.lf[r];
end,
....
profdlg2 = dlg {
nam = 'Belin',
dsc = 'Belin is pale. He absently looks at the shotgun.',
obj = {
[1] = phr('“I came for my cat.”',
'I snatch Barsik from Belin's hand and put in my bosom.',
[[inv():add('mycat'); lifeon('mycat')]]),
....
</code>
Any object or scene may have their “life” handler, which is called every time the game time advances, if the object or the scene have been added to the list of living objects with “lifeon”. Don't forget to remofe living objects from the list with “lifeoff”, when you no longer need them. You can do this, for example, in the “exit” handler or some other way.
life may return string, that will be printed after all text of scene.
From 0.9.1 you can return second retval -- importance. (true or false). For example:
<code>
return 'Guard entered the room.', true -- The event will be printed before objects description.
</code>
Or:
<code>
p 'Guard entered the room.'
return true -- The event will be printed before objects description.
</code>
===== 17. Graphics and music =====
Graphic interpreter analyzes the scene “pic” attribute and treats it as a path to the picture. For example:
<code>
home = room {
pic = 'gfx/home.png',
nam = 'at home',
dsc = 'I am at home',
};
</code>
Of couce, “pic” may be a function. This enhaces the developer's capabilities.
If the current scene has no “pic” attribute defined, the “game.pic” attribute is taken. If “game.pic” isn't defined, no picture is displayed.
From version 0.9.2 you can use animated gif files.
From version 0.9.2 graphics can be embedded everywhere in text or inventory with img function. For example:
<code>
knife = obj {
nam = 'Knife'..img('img/knife.png'),
}
</code>
From version 1.3.0 text flow is supported. Using functions imgl/imgr, picture can be inserted at left/right. border. Those pictures can not be links.
For padding, you can use 'pad:'. For example:
<code lua>
imgl 'pad:16,picture.png' -- padding 16px;
imgl 'pad:0 16 16 4,picture.png' -- padding: top 0, right 16, bottom 16, left 4
imgl 'pad:0 16,picture.png' -- padding: top 0, right 16, bottom 0, left 16
</code>
You can use pseudo-images for blank areas and boxes:
<code lua>
dsc = img 'blank:32x32'..[[Line with blank image.]];
dsc = img 'box:32x32,red,128'..[[Line with red semi-transparent square.]];
</code>
In current version you can use disp attribute:
<code>
knife = obj {
nam = 'Knife';
disp = 'Knife'..img('img/knife.png'),
}
</code>
The interpreter cycles the current music defined by the function ”set_music(music file name)”.
For example:
<code>
street = room {
pic = 'gfx/street.png',
enter = function()
set_music('mus/rain.ogg');
end,
nam = 'on the street',
dsc = 'It is raining outside.',
};
</code>
From version 1.0.0 the interpreter can compose picture from base image and overlays:
<code>
pic = 'gfx/mycat.png;gfx/milk.png@120,25;gfx/fish.png@32,32'
</code>
get_music() returns the current track name.
From version 0.7.7 the set_music() function can get an additional parameter — the number of playbacks. You can get the current counter with “get_music_loop”. -1 means that the playback of the current track is over.
From version 0.9.2 the set_sound() function lets to play sound file. get_sound() returns sound filename, that will be played.
To stop music use stop_music() function (from version 1.0.0).
Use is_music() to check if music is playing. (from version 1.0.0)
===== 18. Advices =====
==== Split game into files ====
You can use "dofile" to include source code fragments. You must use "dofile" in global context, to load all game fragments while parsing main.lua.
<code>
-- main.lua
dofile "episode1.lua"
dofile "npc.lau"
dofile "start.lua"
</code>
For dynamic including (with possibility to redefine current objects or/and rooms) you can use "gamefile":
<code>
...
act = code [[ gamefile ("episode2.lua"); ]]
...
</code>
You can also load new file and forget stack of previous loaded fragments, runnig new file like new game.
<code>
...
act = code [[ gamefile ("episode3.lua", true); ]]
...
</code>
==== Modules ====
Starting from version 1.2.0 you can use modules via “require” function call. At the moment the following modules are available:
* dbg — debug module (use ''require "dbg"'' to enable debugger);
* walk — improved implementation of passages;
* xact — multiple references to objects;
* input — keyboard input;
* click — capturing mouse clicks on the scene picture;
* vars — definition of variables;
* prefs — preferences;
* snapshots — snapshots;
* format — formats the output;
* object — improved objects;
* theme — theme manipulations;
Modules can be used like this:
<code>
--$Name: My game!$
instead_version "1.2.0"
require "para"
require "dbg"
...
</code>
If version is >= 1.2.0 then the following modules are used automatically: vars, object, walk.
“prefs” object (included into “prefs” module) can store game preferences, e.g. player progress or attempt count…
<code>
require "prefs"
...
prefs.counter = 0
...
exit = function(s)
prefs.counter = prefs.counter + 1
prefs:store()
end
...
enter = function(s)
return 'You passed the game '..prefs.counter..' times';
end
...
act = function(s)
prefs:purge()
return "Preferences has been cleared"
end
</code>
“xact” module allows to make references to objects from other objects, reactions and life methods. These references have the form {object:string}, e.g.:
<code>
...
act = [[ I noticed a {myknife:knife} under the table.]]
...
</code>
"object" part of the reference can be object variable or object name.
This module also defines “xact” and “xdsc” objects.
“xact” is the simple reaction object. For example:
<code>
main = room {
forcedsc = true;
dsc = [[Author's comment: I was writing this game for a very {note1:long} time.]];
obj = {
xact('note1', [[More than 10 years.]]);
}
}
</code>
A reaction can contain a code:
<code>
xact('note1', code [[p "More than 10 years."]]);
</code>
“xdsc” allows to insert multiple description to the object list:
<code>
main = room {
forcedsc = true;
dsc = [[ I'm in the room. ]];
xdsc = [[ I see an {anapple:apple} and a {aknife:knife}. ]];
other = [[ There are also {achain:chain} and {atool:handsaw} here.]];
obj = {
xdsc(), -- 'xdsc method by default'
xdsc('other'),
'apple', 'knife', 'chain', 'tool',
}
}
</code>
You may use xroom:
<code>
main = xroom {
forcedsc = true;
dsc = [[ I'm in the room. ]];
xdsc = [[ I see an {anapple:apple} and a {aknife:knife}. ]];
obj = {
'apple', 'knife', 'chain', 'tool',
}
}
</code>
“input” module allows to implement simple text entry fields. “click” module helps to handle mouse clicks on scene pictures.
“para” module adds indentation to paragraphs.
"format: module formats the output. By default all settings are disabled:
<code>
format.para = false -- adds indentation to paragraphs;
format.dash = false -- changes double - on dash;
format.quotes = false -- changes quotes on << >>;
format.filter = nil -- user formatting function;
</code>
You may use modules para/dash/quotes to enable specific feature.
==== Formatting ====
You can do simple text formatting with functions:
* txtc() - center align;
* txtr() - right align;
* txtl() - left align;
* txttop() - top of line;
* txtbottom() - bottom of line;
* txtmiddle() - middle (by default);
For example:
<code>
main = room {
nam = 'Intro',
dsc = txtc('Welcome!'),
}
</code>
You can define text style with functions:
* txtb() - bold;
* txtem() - emboss;
* txtu() - underline;
* txtst() - strikesthrougth;
For example:
<code>
main = room {
nam = 'Intro',
dsc = 'You are in the room: '..txtb('main')..'.',
}
</code>
Since the version 1.1.0 you can create unwrapped strings by using txtnb();
For example:
<code>
main = room {
nam = 'Intro',
dsc = 'You are in the room '..txtb('main')..'.',
}
</code>
==== Menus ====
You can do menus in the inventory area, using menu constructor. Menu handler will be called after single mouse click. If handler have no return string the state of game will no change. For example, here is pocket realisation:
<code>
pocket = menu {
State = false,
nam = function(s)
if s.State then
return txtu('pocket');
end
return 'pocket';
end,
gen = function(s)
if s.State then
s:enable_all();
else
s:disable_all();
end
end,
menu = function(s)
if s.State then
s.State = false;
else
s.State = true;
end
s:gen();
end,
};
knife = obj {
nam = 'knife',
inv = 'This is knife',
};
function init()
inv():add(pocket);
put(knife, pocket);
pocket:gen();
end
main = room {
nam = 'test',
};
</code>
==== Player status ====
Below is an implementation of player status as a text in the inventory, which cannot be picked.
<code>
global {
life = 10;
power = 10;
}
status = stat {
nam = function(s)
p ('Life: ', life, 'Power: ', power)
end
};
function init()
inv():add('status');
end
</code>
==== “walk” from the “exit” and “enter” handlers ====
You can do “walk” from the “enter” and “exit” handlers.
==== Dynamically created references. ====
Dynamically created references can be implemented in various ways. The example below uses “vway” objects. To add a reference one can write:
<code>
objs(home):add(vway('Road', 'I noticed a {road} going into the forest...', 'forest'));
</code>
To delete a reference one can use “del” method.
<code>
objs(home):del('Road');
</code>
The “srch” method can check if the reference is present in the scene.
<code>
if not objs(home):srch('Road') then
objs(home):add(vway('Road', 'I noticed a {road} going into the forest...', 'forest'));
end
</code>
It's convenient to create dynamic references either in the “enter” handler, or in the arbitrary place in the game code, where they are required. If the reference is created in the current scene, the example can be simplified:
<code>
if not seen('Road') then
objs():add(vway('Road', 'I noticed a {road} going into the forest...', 'forest'));
end
</code>
Or you can just enable and disable references with “enable()” and “disable()”, for example:
<code>
seen('Road', home):disable();
exist('Road', home):enable();
</code>
Creating disabled “vobj” and “vway”:
<code>
obj = {vway('Road', 'I noticed a {road} going into the forest...', 'forest'):disable()},
</code>
And then enabling them by their index in the “obj” array or by looking them with srch or seen/exist:
<code>
objs()[1]:enable();
</code>
==== Encoding game sources (from version 0.9.3) ====
If you want hide a game source code, you can encode it with command: “sdl-instead -encode <lua file> [encoded file]” and load encode file from lua with “doencfile”. It's neccessary to keep main.lua as plain text file. So, the recommended scheme is (game is a encoded game.lua ):
main.lua
<code>
-- $Name: My closed source game!$
doencfile("game");
</code>
WARNING about using luac compiler:
Do not use lua compiler luac, it produces platform-dependent code!
But game compilation is useful to find errors in the game code.
==== Packing resources in .idf file (from version 1.4.0) ====
You can pack all game's resources (graphics, sounds, theme) in one .idf file. Put all resources in 'data' directory and run:
instead -idf <path to data>
The file data.idf will be created in the current directory. Put it in game's dir and remove resource files.
You may pack whole game in .idf:
instead -idf <path to game>
Game in .idf format can be run like any other game (as it was directory) or directly from command line:
instead game.idf
==== Switching between players ====
You can create a game with several characters and switch between them from time to time (see “change_pl”). But you can also use the same trick to switch between different types of inventory.
==== Using the first parameter of a handler ====
Code example.
<code>
stone = obj {
nam = 'stone',
dsc = 'There is a {stone} at the edge.',
act = function()
objs():del('stone');
return 'I pushed the stone, it fell and flew down...';
end
</code>
The “act” handler could look simpler:
<code>
act = function(s)
objs():del(s);
return 'I pushed the stone, it fell and flew down...';
end
</code>
==== Using “set_music” ====
You can use “set_music” to play sounds setting the second parameter — the cycle counter how many times to play the sound file.
You can write your own music player, creating it from a live object, e.g:
<code>
-- plays tracks in random order, starting from 2-nd
tracks = {"mus/astro2.mod", "mus/aws_chas.xm", "mus/dmageofd.xm", "mus/doomsday.s3m"}
mplayer = obj {
nam = 'media player',
life = function(s)
local n = get_music();
local v = get_music_loop();
if not n or not v then
set_music(tracks[2], 1);
elseif v == -1 then
local n = get_music();
while get_music() == n do
n = tracks[rnd(4)]
end
set_music(n, 1);
end
end,
};
lifeon('mplayer');
</code>
You can use “get_music_loop” and “get_music” functions to remember the last melody and ren restore it, e.g:
<code>
function save_music(s)
s._oldMusic = get_music();
s._oldMusicLoop = get_music_loop();
end
function restore_music(s)
set_music(s._oldMusic, s._oldMusicLoop);
end
-- ....
enter = function(s)
save_music(s);
end,
exit = function(s)
restore_music(s);
end,
-- ....
</code>
From version 0.8.5 functions “save_music” and “restore_music” are already present in the library.
==== Living objects ====
If your hero needs a friend, one of the ways is the “life” method of that character, that would always bring the object to the player's location:
<code>
horse = obj {
nam = 'horse',
dsc = 'A {horse} is standing next to me.',
life = function(s)
if not seen('horse') then
move('horse', here(), s.__where);
s.__where = pl.where;
end
end,
};
function init()
lifeon('horse');
end
</code>
==== Timer ====
Since the version 1.1. 'instead' has a ''timer'' object. (Only for sdl version.)
Timer controls through the ''timer'' object.
* timer:set(ms) -- set timer interval (ms)
* timer:stop() -- stop timer
* timer.callback(s) -- callback for timer, calling in fixed time interval
Timer function can return a ''stead'' interface command that have to be invoked after the callback execution. For example:
<code>
timer.callback = function(s)
main._time = main._time + 1;
return "look";
end
timer:set(100);
main = room {
_time = 1,
forcedsc = true,
nam = 'Timer',
dsc = function(s)
return 'Example: '..tostring(s._time);
end
};
</code>
==== Keyboard ====
Since version 1.1.0 ''instead'' supports keyboard input (works with SDL version only). This can be done using ''input'' object.
input.key(s, pressed, key) -- keyboard handler; pressed -- press or release event; key -- symbolic name of the key;
Handler can return a ''stead'' interface command. In this case the interpreter doesn't handle a key.
For example:
<code>
input.key = function(s, pr, key)
if not pr or key == "escape"then
return
elseif key == 'space' then
key = ' '
elseif key == 'return' then
key = '^';
end
if key:len() > 1 then return end
main._txt = main._txt:gsub('_$','');
main._txt = main._txt..key..'_';
return "look";
end
main = room {
_txt = '_',
forcedsc = true,
nam = 'Keyboard',
dsc = function(s)
return 'Example: '..tostring(s._txt);
end
};
</code>
==== Mouse ====
Since version 1.1.5 ''instead'' supports mouse click handling (works with SDL version only). This can be done using ''input'' object.
input.click(s, pressed, mb, x, y, px, py) -- mouse click handler; pressed -- press or release event. mb -- mouse button index (1 is left button), x and y -- mouse cursor coordinates relative to upper left corner of the window. px and py parameters exist if a picture have been clicked, they contain mouse cursor coordinates relative to upper left corner of this picture.
Handler can return a ''stead'' interface command. In this case the interpreter doesn't handle a key.
For example:
<code>
input.click = function(s, press, mb, x, y, px, py)
if press and px then
click.x = px;
click.y = py;
click:enable();
return "look"
end
end
click = obj {
nam = 'click',
x = 0,
y = 0,
dsc = function(s)
return "You clicked a picture at "..s.x..','..s.y..'.';
end
}:disable();
main = room {
nam = 'test',
pic ='picture.png',
dsc = 'Example.',
obj = { 'click' },
};
</code>
Here is an example of a code layer that implements calling ''click'' method in the current room once the picture is clicked:
<code>
input.click = function(s, press, mb, x, y, px, py)
if press and px then
return "click "..px..','..py;
end
end
game.action = function(s, cmd, x, y)
if cmd == 'click' then
return call(here(), 'click', x, y);
end
end
----------------------------------------------------------------------
main = room {
nam = 'test',
pic ='picture.png',
dsc = 'Example.',
click = function(s, x, y)
return "You clicked a picture at "..x..','..y..'.';
end
};
</code>
Attention!!! From version 1.2.0 it is recommended to use module click.
==== Dynamic object creation ====
You can use ''new'' and ''delete'' functions to create and remove dynamic objects. An example follows.
<code>
new ("obj { nam = 'test', act = 'test' }")
put(new [[obj {nam = 'test' } ]]);
put(new('myconstructor()');
n = new('myconstructor()');
delete(n)
</code>
''new'' treats its string argument as an object constructor. The constructor must return an object. Thus, the string argument usually contains a constructor function call. For example:
<code>
function myconstructor()
local v = {}
v.nam = 'test object',
v.act = 'test feedback',
return obj(v);
end
</code>
The object created will be saved every time the game is saved. ''new()'' returns a real object; to get its name you can use ''deref'' function:
<code>
o_name = deref(new('myconstructor()'));
delete(o_name);
</code>
==== Complex output from event handlers ====
Sometimes the we need to form event handler output from several parts depending on some conditions. In this case ''p()'' and ''pn()'' functions can be useful. These functions add text to the internal buffer of the handler. The content of this buffer is returned from the handler.
<code>
dsc = function(s)
p "There is a {barrel} standing on the floor."
if s._opened then
p "The barrel lid lies nearby."
end
end
</code>
''pn()'' function adds line feed to the text and outputs the result to the buffer. ''p()'' does almost the same thing but adds a space instead of line feed.
There is a function ''pr()'' in versions 1.1.6 and later, that does not add anything at end of output.
To clear the buffer you can use ''pclr()''. To return the status of the action along with the text, use ''pget()'' or just return.
<code>
use = function(s, w)
if w == apple then
p 'I peeled the apple';
apple._peeled = true
return
end
p 'You cannot use it this way!'
return false; -- or return pget(), false
end
</code>
==== Debugging ====
To see lua call stack during an error, launch sdl-instead with “-debug” parameter. In Windows version debugging console will be created.
You can debug your game without INSTEAD at all. For example, you can create the following “game.lus” file:
<code>
dofile("/usr/share/games/stead/stead.lua"); -- path to stead.lua
dofile("main.lua"); -- your game
game:ini();
iface:shell();
</code>
And launch the game in lua: lua game.lua.
This way the game will work in a primitive shell environment. Useful commands: ls, go, act, use....
For ingame simple debugger insert this:
<code>
require "dbg"
</code>
just after version line in main.lua. Then use F7 to call debugger.
===== 19. Themes for sdl-instead =====
Graphic interpreter supports theme mechanism. A theme is a directory with the “theme.ini” file inside.
The theme reqiured at the least is “default”. This theme is always the first to load. All other themes inherit from it and can partially or completely override its parameters. Themes are chosen by the user through the settings menu, but a game may contain its own theme. In the latter case the game directory contains its “theme.ini” file. However, the user may override custom game theme. If he does, the interpreter warns him that it disagrees with the game author's creative design.
“theme.ini” has a very simple syntax:
<parameter> = <value>
or
; comment
Pussible types of values are: string, color, number.
Colors are set in the #rgb form, where r g and b are color components in hexadecimal. Some colours are recognized by their names, e.g.: yellow, green, violet.
Possible parameters are:
scr.w = game area width in pixels (number)
scr.h = game area height in pixels (number)
scr.col.bg = background color
scr.gfx.bg = path to the background image (string)
scr.gfx.cursor.x = x coordinate of the cursor center (number) (version >= 0.8.9)
scr.gfx.cursor.y = y coordinate of the cursor center (number) (version >= 0.8.9)
scr.gfx.cursor.normal = path to the cursor picture file (string) (version >= 0.8.9)
scr.gfx.cursor.use = path to the cursor picture of the “use” indicator (string) (version >= 0.8.9)
scr.gfx.use = path to the cursor picture of the “use” indicator (string) (version < 0.8.9)
scr.gfx.pad = padding for scrollbars and menu edges (number)
scr.gfx.x, scr.gfx.y, scr.gfx.w, scr.gfx.h = coordinates, width and height of the picture window — the area to display the scene picture. Interpreted depending on the layout mode (numbers)
win.gfx.h - synonymous to scr.gfx.h (for compatibility)
scr.gfx.mode = layout mode (string “fixed”, “embedded” or “float”). Sets the mode for the picture. If “embedded”, the picture is part of the main window, scr.gfx.x, scr.gfx.y and scr.gfx.w are ignored. If “float”, the picture is placed in the coordinates (scr.gfx.x, scr.gfx.y) and downscaled to scr.gfx.w by scr.gfx.h if larger. If “fixed”, the picture is part of the main window as in “embedded”, but stays above the text and is not scrolled with it.
win.x, win.y, win.w, win.h = coordinates, width and height of the main wiindow. — the area with the scene description (numbers)
win.fnt.name = path to the font file (string)
win.fnt.size = font size for the main window (number)
win.fnt.height = line height as float number (1.0 by default)
win.gfx.up, win.gfx.down = paths to the pictures of up/down scrollers for the main window (string)
win.up.x, win.up.y, win.down.x, win.down.y = coordinates of scrollers (position or -1)
win.col.fg = font color for the main window (color)
win.col.link = link color for the main window (color)
win.col.alink = active link color for the main window (color)
inv.x, inv.y, inv.w, inv.h = coordinates, width and height of the inventory window (numbers)
inv.mode = inventory mode string (“horizontal” or “vertical”). In the horizontal mode several objects may fit in the same line, in the vertical — only 1 per line. (string)
inv.col.fg = inventory text color (color)
inv.col.link = inventory link color (color)
inv.col.alink = inventory active link color (color)
inv.fnt.name = path to the inventory font file (string)
inv.fnt.size = inventory font size (number)
inv.fnt.height = line height as float number (1.0 by default)
inv.gfx.up, inv.gfx.down = paths to the pictures of inventory up/down scrollers (string)
inv.up.x, inv.up.y, inv.down.x, inv.down.y = coordinates of scrollers (position or -1)
menu.col.bg = menu background (color)
menu.col.fg = menu text color (color)
menu.col.link = menu link color (color)
menu.col.alink = menu active link color (color)
menu.col.alpha = menu transparency 0-255 (number)
menu.col.border = menu border color (color)
menu.bw = menu border width (number)
menu.fnt.name = paths to menu font file (string)
menu.fnt.size = menu font size (number)
menu.fnt.height = line height as float number (1.0 by default)
menu.gfx.button = path to the menu icon (string)
menu.button.x, menu.button.y = menu button coordinates (number)
snd.click = path to the click sound file (string)
include = theme name (the last component in the directory path) (string)
The theme header may include comments with tags. Right now there is only one tag: “$Name:”, it contains an UTF-8 line with the theme name. E.g.:
<code>
; $Name:New theme$
; modified "book" theme
include = book
scr.gfx.h = 500
</code>
The interpreter searches for themes in the “themes” directory. Unix version also checks ~/.instead/themes/ directory. Windows version (>=0.8.7) checks "Documents and Settings/USER/Local Settings/Application Data/instead/themes"
TODO
Full list of objects and methods.
Translation: vopros@pochta.ru
|