What are the Event callback Parameter(s)? [SOLVED]

Place to post guides, observations, things related to modding that are not mods themselves.
Gwinda
Burner Inserter
Burner Inserter
Posts: 7
Joined: Sun Sep 24, 2017 8:02 pm
Contact:

What are the Event callback Parameter(s)? [SOLVED]

Post by Gwinda »

I just started to look at modding and in the Wiki I can only find information on how to register an event. But there is no information on the actual callback of the event.
I had a look at another mod, in control.lua, to see how it work and this is how the callback is called:

Code: Select all

on_tick = function(object)
    local control = object.meta.entity.get_control_behavior()
I can't find any information on the type of the parameter 'object', nor that this callback is sending a parameter at all.
I can find documentation for the entity but not the 'object' or 'meta'.

So what class is the 'object' ???
Last edited by Gwinda on Mon Sep 25, 2017 5:28 am, edited 1 time in total.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14280
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: What are the Event callback Parameter(s)?

Post by Rseding91 »

If you want to get ahold of me I'm almost always on Discord.
Gwinda
Burner Inserter
Burner Inserter
Posts: 7
Joined: Sun Sep 24, 2017 8:02 pm
Contact:

Re: What are the Event callback Parameter(s)?

Post by Gwinda »

I must be either sever stupid or I haven't been clear enough.
I'm not talking about HOW to register an event, I'm talking about the callback to the registered event.

Code: Select all

classes["item-name"] = {
    on_tick = function(object)
        local control = object.meta.entity.get_control_behavior()
I have been at that link already and it says nothing about the parameter 'object' seen in my above code snippet, found in a mod.
There is no mentioning about any parameter at all.
on_tick
It is fired once every tick. Since this event is fired every tick, its handler shouldn't include performance heavy code.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3716
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: What are the Event callback Parameter(s)?

Post by DaveMcW »

All events
Every event contains at least the name and tick attributes. Other events may include some additional attributes, specific to the event.

Contains
name :: defines.events: Identifier of the event
tick :: uint: Tick the event was generated.
Since on_tick does not include any additional attributes, you can only do:

Code: Select all

local event_name = object.name
local event_tick = object.tick
Let's look at a more interesting event.
on_built_entity
Called when player builds something.

Contains
created_entity :: LuaEntity
player_index :: uint
item :: string (optional)
tags :: dictionary string → Any (optional)

Code: Select all

local control = object.created_entity.get_or_create_control_behavior()
Gwinda
Burner Inserter
Burner Inserter
Posts: 7
Joined: Sun Sep 24, 2017 8:02 pm
Contact:

Re: What are the Event callback Parameter(s)?

Post by Gwinda »

Duh, ok thank you I see now...
But what class type is the parameter object?

According to the Wiki there should only be object.name and object.tick so what is object.meta? Is it just a short for Lua's getmetatable(object) ?
Rseding91
Factorio Staff
Factorio Staff
Posts: 14280
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: What are the Event callback Parameter(s)?

Post by Rseding91 »

Gwinda wrote:Duh, ok thank you I see now...
But what class type is the parameter object?

According to the Wiki there should only be object.name and object.tick so what is object.meta? Is it just a short for Lua's getmetatable(object) ?
There is no "object.meta" - that's just Lua syntax for indexing into a table. Lua only has 4 types: tables, strings, numbers, and booleans.
If you want to get ahold of me I'm almost always on Discord.
Gwinda
Burner Inserter
Burner Inserter
Posts: 7
Joined: Sun Sep 24, 2017 8:02 pm
Contact:

Re: What are the Event callback Parameter(s)?

Post by Gwinda »

Yes I know Lua syntax quite well (I have written a Lua Editor/Debugger in C++) and 'object' is a table with a member 'meta' according to the code I found in a mod.
The member 'meta' seems to be a table with another member named 'entity'. I did find the 'entity' class documentation and it has a member function named 'get_control_behavior()'

But never mind. After digging deeper into the mod I also found this:

Code: Select all

on_place = function(entity)
    return {
      meta = {
        entity = entity,
        ticks = 60
      }
    } 
  end
So the 'meta' table is a user defined table added at some point. Not to be confused with Lua's meta tables that control a table's behaviour.
To make things worse, on_place is a user defined event. I guess I picked a bad example to look at... :D

Thank you for your help. I appreciate it a lot...

PS. I found this Wiki with a little bit better documentation of the events. http://kovarex.com/wiki/index.php?title=Lua/Events
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: What are the Event callback Parameter(s)?

Post by Adil »

Seeing how that wiki article was last updated two years ago, I doubt that it is better documentation.

Your questions are quite puzzling to me. There are no standardized classes in lua and standard event handlers are passing a simple table with fields described in the api docs. Without whatever `meta` fields.

You keep mentioning the "other mod" which you pull the lines from, but it is just a single mod, the fact that its author decided to organize his code that way does not mean that other people are aware of conventions used in the particular code. If you're familiar with lua, the stylistic variance of the codes people generate should not be a revelation to you.

At the very least, mentioning the mod name would increase your chances of getting answers about particular code. As well as directly asking the mod author would.
If the particular mod is not the key interest to you, you should probably read modding tutorials on the wiki, which is not obsolete: https://wiki.factorio.com/Tutorial:Modding_tutorial
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14280
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: What are the Event callback Parameter(s)?

Post by Rseding91 »

The Lua-api page is going to be the only one that's actually 100% accurate since it's generated from the source code on each release. It lists all parameters for every event.
If you want to get ahold of me I'm almost always on Discord.
Gwinda
Burner Inserter
Burner Inserter
Posts: 7
Joined: Sun Sep 24, 2017 8:02 pm
Contact:

Re: What are the Event callback Parameter(s)?

Post by Gwinda »

Adil wrote:Seeing how that wiki article was last updated two years ago, I doubt that it is better documentation.

Your questions are quite puzzling to me. There are no standardized classes in lua and standard event handlers are passing a simple table with fields described in the api docs. Without whatever `meta` fields.
No, Lua don't have classes but that is what the Wiki is calling them. I only used the same term as the Wiki in hope that people would understand what I was referring to:
http://lua-api.factorio.com/latest/Classes.html
However, it is possible to mimic Classes in Lua.
Adil wrote:You keep mentioning the "other mod" which you pull the lines from, but it is just a single mod, the fact that its author decided to organize his code that way does not mean that other people are aware of conventions used in the particular code. If you're familiar with lua, the stylistic variance of the codes people generate should not be a revelation to you.

At the very least, mentioning the mod name would increase your chances of getting answers about particular code. As well as directly asking the mod author would.
If the particular mod is not the key interest to you, you should probably read modding tutorials on the wiki, which is not obsolete: https://wiki.factorio.com/Tutorial:Modding_tutorial
As I wrote in my previous post, I figured it out. the table 'meta' was a user defined table added in a user defined event in the mod. Since I just started to look at Factorio modding, I don't know immediately what is user defined and what isn't.
The mod I was poking around in was just a randomly picked mod to see how it's work.

Again, thank you for your help and time.
Post Reply

Return to “Modding discussion”