Skip to content

Basic API Usage part 1

IonutParau edited this page Oct 31, 2021 · 5 revisions

Making the mod loadable

The built-in mod loader looks for mods wit htheir names in mods.txt (the names of their files without the file extension) unless auto_detect_mods is true. So make sure your mod's main file name is in mods.txt (in a seperate line from everything else) otherwise the mod loader will think there is no mod to laod.

Making the mod do stuff.

For the mod's private functions and properties to be used globally, we have 2 things. The return table, where the file at the end returns a table containing those properties, tho you can also use ModBinder.

Example for return table.

-- Code of mod above

return {
  init=myInitFunction,
  update=myUpdateFunction,
  etc.
  etc.
}

Exmaple for ModBinder (requires at least v0.1.0.0 of CelLuAPI)

-- Code of mod above

local binding = ModBinder.bindMod("myCoolModName")

binding.bindFunction("init", myInitFunction).bindFunction("update", myUpdateFunction) etc. etc.

So now that we know how to tell the mod loader what our functions are, we can just make those functions use code.

That code can update the cell. A example of it is this, which checks if the cell has some ID stored in a variable and if it is mover it like a mover.

-- The function gets as parameters the id of the cell that is updated, the x, the y and the direction.
function myUpdateFunction(id, x, y, direction)
  if id == myCoolCellID then
    DoMover(x, y, direction)
  end
end

Ok, this is cool! But.... how do we actually create the cell? Well, in the init function we can just use addCell!

--Remember, our init function is equal to this function. That is why its not called init.
function myInitFunction()
  addCell("my cool cell label", "my cool textures path")
end

We just used the function addCell(), which takes the label, the path of the texture (ending in the file extension as well), and a third optional parameter called the options table.

CelLuAPI has a lot of built in options to help you, like Options.unpushable, Options.invisible etc. etc.

The option table is most of the time used to describe the cell with a pushability callback or custom cell type.

So, let's add a cell of type mover that we pushed spreads like a fungal because... why not?

local fungalMoverID = 0

function fungalMoverPushability(x, y, direction, pusherX, pusherY, pusherRotation, pushType)
  -- On the cell grid, it starts with the y and then the x.
  cells[pusherY][pusherX].ctype = fungalMoverID
  return true -- We return true to show we do want to be pushed
end

function myInitFunction()
  fungalMoverID = addCell("cool fungal mover thing", "cool_fungal_mover_thing.png", {push = fungalMoverPushability, type = "mover"})
end

addCell() returns the id of the cell.

We go in more detail on them in the 2nd part of the Basic API Usage.

Clone this wiki locally