[TUTORIAL] How to add custom (linked) entities.
Posted: 06 Mar 2012, 03:57
The following is a brief guide on how I managed to add a spikey spawner to Mari0 1.3 that highlights the purpose of some files and what was changed in them.
List of files and modified variables / functions:
First I added a icon to entities.png so that the entity editor knows what icon to use. I had to add a new row because if I added a new column then item IDs would have broken and that would have been terrible (unless I wanted to relocate all the icons properly). I padded the empty spaces with icons. It's important to remember that the grid size is 17x17.
graphics/SMB/spikeydispenser.png
I also added an upside down cube dispenser because it felt more natural to have spineys pop out the top.
entity.lua entitylist
entity.lua entitydescriptions
Blank entries are included because I had to make a new row of entities when I made this. When you add your own entities you can put them next to each other in what the blank spaces would be.
game.lua game_load
Here I added 101 to the end of inputsi and "spikeydispenser" to the end of inputs. Inputs doesn't appear to be used elsewhere but I put it here for good measure. 101 is the tenth row, first icon, thus the spikey icon that got added to entities.png.
For the next batch of code I mostly searched for the terms:
game.lua loadmap
game.lua startlevel
I don't use line numbers because they are subject to change between versions of Mari0 and previous source versions are not listed publicly on the site.
variables.lua spikeydispensertime
There is no global variable spikeydispensertime like there is cubedispensertime, so we have to make it. This makes it so that spikeydispenser.lua has a global timer to use.
main.lua love.load
Here I include in two different sections of the love.load function code to require and load in the spikeydispenser lua code and I define its image.
spikeydispenser.lua
spikeydispenser.lua - this is mostly re-purposed code from cubedispenser and a mishmash of stuff from lakitu.lua and goomba.lua (because that's where spikey's code is, hiding in there). I will outline the code in this later when I have more time.
The message Maurice originally replied to was:
List of files and modified variables / functions:
- graphics/SMB/entities.png - New row of icons added.
- graphics/SMB/spikeydispenser.png - Sprite for the new entity to use.
- entity.lua - Information about the entity while in the editor.
- array entitylist - Internal names of entities.
- array entitydescriptions - Descriptions shown on the editor bottom line.
- variables.lua - A global variable is necessary to get our entity to work.
- float spikeydispensertime - The timer for making spikeys appear.
- game.lua - Important to include draw and initialization code so we can see and use the entity.
- function game_load - Loads the game with the data it needs.
- array inputs - The names of all the entities that accept inputs.
- array inputsi - The ID of entities that accept inputs based on their position on entities.png.
- function game_draw - Draws all the objects on the screen every frame.
- function loadmap - Loads information from map files and feeds it to the engine.
- function startlevel - Actually begins the level, blank initializes all arrays as necessary.
- function game_load - Loads the game with the data it needs.
- spikeydispenser.lua - The code our entity is going to use.
- main.lua - Include the entity code and image location.
- function love.load - The function that (re)initializes love2d.
First I added a icon to entities.png so that the entity editor knows what icon to use. I had to add a new row because if I added a new column then item IDs would have broken and that would have been terrible (unless I wanted to relocate all the icons properly). I padded the empty spaces with icons. It's important to remember that the grid size is 17x17.
graphics/SMB/spikeydispenser.png
I also added an upside down cube dispenser because it felt more natural to have spineys pop out the top.
entity.lua entitylist
Code: Select all
"spikeytube",
"",
"",
"",
"",
"",
"",
"",
"",
""
Code: Select all
"place on empty tile - will drop a spikey and remove previous one - use link tool", --"spikeytube"
"place nowhere - unused",
"place nowhere - unused",
"place nowhere - unused",
"place nowhere - unused",
"place nowhere - unused",
"place nowhere - unused",
"place nowhere - unused",
"place nowhere - unused",
"place nowhere - unused"
game.lua game_load
Code: Select all
inputs = { "door", "groundlight", "wallindicator", "cubedispenser", "walltimer", "notgate", "laser", "lightbridge", "spikeydispenser"}
inputsi = {28, 29, 30, 43, 44, 45, 46, 47, 48, 67, 74, 84, 52, 53, 54, 55, 36, 37, 38, 39, 101}
For the next batch of code I mostly searched for the terms:
- cubedispenser
- boxtube
- spikeydispenser
- spikeytube
Code: Select all
--spikeydispensers
for j, w in pairs(objects["spikeydispenser"]) do
w:draw()
end
Code: Select all
elseif t == "spikeytube" then
table.insert(objects["spikeydispenser"], spikeydispenser:new(x, y, r))
Code: Select all
objects["spikeydispenser"] = {}
variables.lua spikeydispensertime
Code: Select all
spikeydispensertime = 1
main.lua love.load
Code: Select all
require "spikeydispenser"
Code: Select all
spikeydispenserimg = love.graphics.newImage("graphics/" .. graphicspack .. "/spikeydispenser.png")
spikeydispenser.lua
spikeydispenser.lua - this is mostly re-purposed code from cubedispenser and a mishmash of stuff from lakitu.lua and goomba.lua (because that's where spikey's code is, hiding in there). I will outline the code in this later when I have more time.
The message Maurice originally replied to was:
EntranceJew wrote:I'm working on a custom entity but for some reason the game both runs painfully slow after I rebuild it, also I can't find where to give it an icon in the entity menu and it crashes the game when I try to place one.