Modding FAQ: Lua⚓︎
What does the colon operator in Lua do?⚓︎
In Lua, you can invoke module functions (i.e. functions that are attached to a table) in two different ways:
1 2 |
|
A period invokes the function in the "normal" way. A colon invokes the function in a special way that is syntactic sugar for passing the module as the first argument. For example, the following two function calls are equivalent:
1 2 |
|
The point of using the colon is that it is a convenience to save you from typing out the longer function call, at the cost of some obfuscation for those not familiar with Lua. This feature is included in the language since doing this is such a common task. (Lua modules are often used to emulate Java-style classes.)
It is idiomatic in Lua to invoke any function that is part of a module with a colon, and you should follow this convention when writing your own code. Additionally, most API class methods should be invoked with a colon. However, there are exceptions; methods marked as "static", or from object-independant classes (e.g. Isaac
, Input
, Options
), are not invoked with a colon.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
What is the difference between pairs
and ipairs
?⚓︎
pairs
is for iterating over Lua tables that represent a map. In other words, something with key/value associations.ipairs
is for iterating over Lua tables that represent an array. In other words, something that contains a list of elements.
Code speaks louder than words:
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 10 |
|
Since Lua is untyped and uses tables to represent multiple different data structures, pairs
and ipairs
serve as a flag to tell the reader what the underlying data structure really is.
How do I prevent the player from shooting arbitrarily?⚓︎
You can set canShoot
to false
when defining a player in xml. If you want to be able to arbitrarily toggle their ability to shoot, you can either use REPENTOGON's EntityPlayer.SetCanShoot
function, or you can do the following:
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 |
|
If you are using IsaacScript, then all you have to do is call the setBlindfold
function, like so:
1 2 |
|
How do I apply a costume to my character?⚓︎
This is called a "null costume" and it is accomplished via the EntityPlayer.AddNullCostume()
method. For more information, see Lytebringr's 8th video.
The follow is an example of a mod adding a null costume:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
How do I get a familiar to follow the player like Brother Bobby does?⚓︎
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 |
|
How do I iterate over a list object from the API?⚓︎
Sometimes you will be provided with objects in the API which store a list of objects, but are not tables, like the GetRooms method from Level.
Here's an example of how to iterate over the list object in Lua:
1 2 3 4 5 6 7 |
|
In IsaacScript, you could implement the code on the Lua tab in the exact same way. However, for this specific case, you can simply use a helper function to iterate over the rooms directly:
1 2 3 |
|
How do you use StageAPI to add new bosses?⚓︎
This is an example code snippet from Xalum:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|