Closed aSemy closed 2 years ago
I don't think there's currently a built-in way to do this in the factorio api. What you could do, is create your own id-to-string-name table:
export const eventIdToName = new LuaTable<defines.events, keyof typeof defines.events>()
for (const [k, v] of pairs(defines.events)) {
eventIdToName.set(v, k)
}
//usage
script.on_event(defines.events.on_player_mined_entity, (e: OnPlayerMinedEntityEvent) => {
const name = eventIdToName.get(e.name)
})
Wonderful, that does the trick, thanks!
But since the TypeScript code is generated, could you add the string name of the event to the generated TS code, improving upon the actual Factorio class?
Actually, it might be better to have a super-interface or abstract class FactorioEvent
that all events implement/extend. This would have two fields: event_name
and tick
. That would make handling events generically, but still type-safely, much more easy. What do you think? Is that possible, or worth the effort to implement?
But since the TypeScript code is generated, could you add the string name of the event to the generated TS code, improving upon the actual Factorio class?
This would be currently out of the scope of this project as this project only generates type definitions: it defines the types of the existing API, but doesn't extend the api/run any code. I might in the future release a separate project which could have such utilities.
Actually, it might be better to have a super-interface or abstract class
FactorioEvent
that all events implement/extend. This would have two fields:event_name
andtick
. That would make handling events generically, but still type-safely, much more easy. What do you think? Is that possible, or worth the effort to implement?
There is already an interface for this called EventData
. Although currently none of the events types explicitly extend this interface (yet), they should all be assignable to it.
To help people discover this, event types now explicitly extend from EventData
in v0.8.0.
Amazing, thanks!
At the moment I'm processing events from Factorio
I'd like to be able to do something like
e.event_name
which will return a string,"on_player_mined_entity"
.I've tried
e.name
, but that returns another object, not the name.Maybe this is already possible and I don't know how to do it in TypeScript :)