How To: Simple filters

From Moondust Wiki
Revision as of 06:26, 11 December 2016 by Horikawa otane (talk | contribs)
Jump to navigation Jump to search

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 onStart function. This is an event you can put in your Lua file that runs when the level is loaded.

function onStart()
    --Some code here
end

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.

function onStart()
    player.powerup = PLAYER_BIG
end

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.

function onStart()
    if (player.powerup == PLAYER_SMALL) then
        player.powerup = PLAYER_BIG
    end
end

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:

function onStart()
    if (player.powerup == PLAYER_SMALL or player.powerup == PLAYER_BIG) then
        player.powerup = PLAYER_FIREFLOWER
    end
end

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.

function onStart()
    player.character = CHARACTER_MARIO
end

This code will force the player to be Mario when the level starts.

The character codes are as follows:

CHARACTER_MARIO = Mario
CHARACTER_LUIGI = Luigi
CHARACTER_PEACH = Peach
CHARACTER_TOAD = Toad
CHARACTER_BOWSER = Bowser
CHARACTER_JUNI = Juni
CHARACTER_KLONOA = Klonoa
CHARACTER_MEGAMAN = Mega Man
CHARACTER_NINJABOMBERMAN = Ninja Bomberman
CHARACTER_ROSALINA = Rosalina
CHARACTER_SAMUS = Samus
CHARACTER_SNAKE = Snake
CHARACTER_UNCLEBROADSWORD = Uncle Broadsword
CHARACTER_WARIO = Wario
CHARACTER_ZELDA = Zelda
CHARACTER_ULTIMATERINKA = Ulitmate Rinka
CHARACTER_PRINCESSRINKA = Princess Rinka


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 Megaman if we choose Mario, Peach or Toad, and to Luigi if we choose anyone else. We can do that like this:

function onStart()
    local character = player.character
    if (character == CHARACTER_MARIO or character == CHARACTER_PEACH or character == CHARACTER_TOAD) then
        player.character = CHARACTER_MEGAMAN
    else
        player.character = CHARACTER_LUIGI
    end
end

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.

Further Reading

LunaLua events

LunaLua constants

Player (class)