Giter Site home page Giter Site logo

lifecycle-events's Introduction

lifecycle-events

When a mod creates or destroys an entity via Lua script, no events are fired. This is not a problem if the created/destroyed entities are from that mod, because it can handle everything internally.

However, if the created/destroyed entities are from an external mod, or the vanilla game, other mods which may need to take action have no clue that the entities were created/destroyed - that's a problem, but the devs won't fix it.

When should I use this module?

Use the functions provided by this module when you create or destroy entities that aren't part of your mod.

Example

Imagine you've got a mod that creates roads, but you want add concrete lampposts (entity from a separate mod) to them.

If you use LuaSurface.create_entity() to place a lamppost, the Concrete Lamppost mod won't know about it, and won't spawn the additional entities. Likewise, if you use LuaEntity.destroy(), the Concrete Lamppost mod won't know about it, and any spawned entities will remain on the map. Epic fail.

To solve this, use the create_entity() and destroy_entity() functions - they spawn the events that almost all mods listen for and act upon...

-- control.lua

require 'lifecycle-events'

-- some time later in the script...

local function add_lamppost( surface, pos, dir, player )

  return create_entity(
    surface,
    {
      type = 'electric-pole',
      name = 'concrete-lamppost',
      position = pos,
      direction = dir,
      force = player.force
    },
    player
  )

  -- defines.events.on_built_entity triggered

end

local function remove_lappost( lamppost )

  -- defines.events.on_entity_died triggered

  return destroy_entity( lamppost )

end

As far as the Concrete Lamppost mod is concerned, a player created and destroyed the lampposts, so it will act accordingly.

Installation

Download lifecycle-events.lua and put it in your mod folder, then require it at the top of your own control.lua:

-- in your control.lua:
require 'lifecycle-events`

Usage

Three new global functions are provided:

  • can_destroy( entity )
  • destroy_entity( entity ) -- includes check to see if it can_detroy( entity )
  • create_entity( surface, settings[, player] )

Note: To mtaintain performance, very little error checking is performed.

Destroying entities

The destroy_entity() function is similar to LuaEntity.destroy(), but it triggers an event:

local result = destroy_entity( entity, force )

Where:

  • result = true if entity was destroyed, otherwise nil (which is equivalent to false)
  • entity = a valid LuaEntity table
  • force (optional) = the force that killed the entity, defaults to nil

Creating entities

The create_entity() function is similar to LuaSurface.create_entity(), but it triggers an event:

local new_entity = create_entity( surface, settings[, player] )

Where:

  • surface = a valid LuaSurface table (not a surface name)
  • settings = a table describing the entity to create
  • player (optional) = a valid LuaPlayer table, or player index
    • If player specified, on_built_entity event is used
    • Otherwise, on_robot_built_entity is used with invalid .robot property
  • new_entity = the entity that was created, or nil if there was a problem

License

MIT (open source)

lifecycle-events's People

Contributors

aubergine10 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.