From b075d8e3336dfb2755dd1b9dc29c5320a155013b Mon Sep 17 00:00:00 2001 From: "Y. Wang" Date: Fri, 20 Sep 2024 12:45:20 +0000 Subject: Implement staticdata for trains This patch exposes train.staticdata that can be used by other modders to save data in trains across restarts. It additionally exposes two new APIs for modders where this is relevant: * advtrains.te_register_on_couple(function(init_train, stat_train)): registers a callback for train coupling, where stat_train is couple into init_train and is subsequently removed. This callback is run before the actual coupling takes place; in particular, it is run before stat_train is removed. * advtrains.te_register_on_decouple(function(train, newtrain, index)): registers a callback for train decoupling, where newtrain is created by splitting the train at the given index (the wagon at the index is part of the new train). This callback is run after decoupling takes place. advtrains.te_register_on_decouple(function --- advtrains/trainlogic.lua | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'advtrains/trainlogic.lua') diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 3b006d2..ed49a4c 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -263,6 +263,10 @@ function advtrains.train_ensure_init(id, train) atwarn(debug.traceback()) return nil end + + if not train.staticdata then + train.staticdata = {} + end train.dirty = true if train.no_step then @@ -1190,6 +1194,8 @@ function advtrains.split_train_at_index(train, index) newtrain.points_split = advtrains.merge_tables(train.points_split) newtrain.autocouple = train.autocouple + advtrains.te_run_callbacks_on_decouple(train, newtrain, index) + return newtrain_id -- return new train ID, so new train can be manipulated end -- cgit v1.2.3