How To: Simple filters

This is a beginner tutorial for making simple player filters in LunaLua. These can be used to set the player character, or their power-up state.

To start with, let's look at the onLoad function. This is an event you can put in your Lua file that runs when the level is loaded.

Powerup Filters
What we're going to do now is filter the player so that they are forced to be in the "big" state. We are going to be using the player.powerup field for this. One of the nice things about LunaLua is that lots of the values you need are stored as Constants. This means we don't need to worry too much about what the numbers are, but we can just set the powerup based on its name.

This will make force the player to be big when the level starts.

This works, but if the player starts the level with a fire flower, they will lose it when the level loads (because we're forcing them to be "big"). If we don't want this to happen, we need to check to see if the player has anything better, before forcing the player's state.

Now, the player will only be made big if they start the level small, which means they won't lose any hard-won powerups that they gained in other levels. You can also do this for other powerups. This code will give you a fire flower if you start a level small, or with just a mushroom:

Character Filters
Character filters are much the same as powerup filters, only we have to use the mem function for this. This function allows us to change pretty much anything we like about the player, but should be handled with care, as it can cause some nasty things to happen if you don't know what you're doing with it. The way we're using it here, though, is fairly safe.

This code will force the player to be Mario when the level starts. The arguments to the mem function work like this (don't worry too much about the first 2, you only really need to use those if you're planning to use the mem function for other things, which is beyond the scope of this tutorial):

0xF0 is the memory offset. This is the variable we're going to change. In this case, 0xF0 means the player's character ID. FIELD_WORD tells us what type of value we're looking at. Usually, this will be FIELD_WORD, which is a 16-bit integer. 1, the last argument, is the value we want to set this variable to. This is the character code for Mario. The character codes are as follows:

Other values should not be used, as they are unsafe.

As before, we can also do some tests, to set the character depending on other factors. Let's say we want, when the level starts, to set the character to Mario if we choose Mario, Peach or Toad, and to Luigi if we choose Luigi or Link. We can do that like this:

Here, we use the other form of the mem function, which only has two arguments. This just grabs the value of that variable (which character we are), so we can use it in our Lua code.

And that's it for this tutorial! There's a lot more you can do with these values, but they are beyond the scope of this tutorial.