These are taken from working scripts that I have used, but are not complete scripts by themselves.
Number of items in a list.
if you have a list in a script entity then you can quickly find how many by using #
Code: Select all
monsters = {"snail","herder","crowern","ogre"}
random_monster = math.random(1,#monsters)
Compare text using a Substring
if you want to compare the name or id of entities and use a partial match all that begin with a particular word or phrase you can you sub to create a substring
Code: Select all
for i in allEntities(party.level) do
if i.id:sub(1,13) == "sequence_pit_" then
i:open()
end
end
EDIT - clarification on how to use sub
s:sub(i,j)
OR
string.sub(s,i,j)
s is the string we want to look at.
i is the start character
j (optional) is the end character. if j is omitted, then the result will run to the end of the original string s.
if i is negative, then the substring will start that many characters in from the right.
Check which plate/button/switch triggered the function
When you create a connection in the editor between an entity and a script, the entity is passed to the script as a variable..
Code: Select all
function checkTrigger(trigger)
if trigger.id == "plate_N" then
Tfacing = 3
elseif trigger.id == "plate_E" then
Tfacing = 0
elseif trigger.id == "plate_S" then
Tfacing = 1
elseif trigger.id == "plate_W" then
Tfacing = 2
else
Tfacing = 0
end
spawn("teleporter",party.level,party.x,party.y,Tfacing)
Spawn items "up in the air"
When spawning items to automatically give to the party, or insert into containers, It was suggested by Petri to spawn them "up in the air", because if spawned with co-ordinates, they will remain in that space when (a copy is) placed in a champion's inventory.
A simple way to spawn something "in the air" is
Code: Select all
spawn("item_name")
Code: Select all
eye_socket_left:addItem(spawn("blue_gem",nil,nil,nil,nil,"fake_blue_gem"))
Check if something has a nil value
As I've been working with more complex scripts I've often found the game crashes because I have attempted to use something that does not exist, or has a nil value... so it can often be beneficial to check if things are nil before acting upon them.
Code: Select all
if party:getChampion(1):getItem(7) == nil then
party:getChampion(1):InsertItem(7,spawn("throwing_axe"))
else
spawn("throwing_axe",party.level,party.x,party.y,party.facing)
end
Check if something IS NOT equal
Possibly a very basic one, but most people will be familar with checking if a == b, but also useful is to use ~= to check if something is not equal.
Code: Select all
if party:getChampion(1):getItem(7) ~= nil then
party:getChampion(1):removeItem(7)
end
Repeat something until something else happens
Repeat until is a very powerful way to create a loop... though care should be taken using it, as it has the potential to cause an endless loop if the until condition can never be met.
Code: Select all
a = math.random(1,4)
repeat b = math.random(1,4)
until a ~= b
Do something for every value in a range (loop)
the for command can be very useful for creating looping scripts in Grimrock editor, allowing us to automate tasks a certain number of times.
Code: Select all
for i = 1,4 do
party:getChampion(i):insertItem(11,spawn("pitroot_bread"))
end
Do something for every entry in a list (loop)
we can also use a for loop to do something for every entry in a list.
Code: Select all
local list_of_gates = {"dungeon_portcullis_4","dungeon_portcullis_2","dungeon_portcullis_3"}
for _,i in ipairs(list_of_gates) do
door = findEntity(i)
door:open()
end
Concatenate (i.e. join) strings
Sometimes it is useful to join separate strings together as a single string, especially when working with a variable string and a fixed string. To do this we can use the concatenate operator ..
Code: Select all
hudPrint(party:getChampion(1):getName() .. " is feeling hungry")
Add multiple tests to the same hook
With so many spells and effects using party, monster or other hooks, it can happen that you need to run two different scripts as part of the same hook. This can be done by evaluating each script as a variable and then returning both.
Code: Select all
onTurn = function(monster,turn_dir)
local hold_test = grimwold_spell_script.checkHeld(monster)
local turn_test = grimwold_spell_script.checkTurn(monster,turn_dir)
return hold_test and turn_test
end,