IncredibleHannes / TPF2-Timetables

The Timetable mod for Transport Fever 2
GNU General Public License v3.0
32 stars 19 forks source link

Avoid rare random crashes with game API #50

Closed Vacuum-Tube closed 3 years ago

Vacuum-Tube commented 3 years ago

https://github.com/IncredibleHannes/TPF2-Timetables/blob/a803a2a99dddfb49dc9ec0fcad910df9530f259c/res/scripts/celmi/timetables/timetable_helper.lua#L158

You should be really careful with those direct indexing of getComponent. I did this in my mod, too (very often) and got randomly "Error occured".

I got this info from UG:

There seems to be problems with temporaries, i.e. somehow "fatInstances" contains garbage, even if it is in the local scope. I still have to pin-point the problem exactly. It seems that the library we are using to interface with C++ (sol2) is not liking the following:

​local fatInstances = api.engine.getComponent(id, api.type.ComponentType.MODEL_INSTANCE_LIST).fatInstances

If you change that to:

local comp = api.engine.getComponent(id, api.type.ComponentType.MODEL_INSTANCE_LIST) local fatInstances = comp.fatInstances

the problem should be fixed.

I think this is a general issue with the library, where the lifetime of certain objects is incorrect, so (as a workaround) I suggest you try (as much as possible) to assign the result of "api" calls to temporary variables.

(This is mostly because the result of "api" calls are not lua tables, but is just lua userdata to real game objects or wrappers around them (in order to avoid expensive copies).)

IncredibleHannes commented 3 years ago

see #72 for fix