TrainsCaller deprecated

by Hermios

Allows trainstations to call trains when conditions are fulfilled

Content
5 years ago
0.14 - 0.18
15
Trains

b 1.1 pre-existing trainstops, no assigned IDs or variables

8 years ago

whenever I click on a station that existed before this mod was installed, I get a crash:
_Trains_Caller _/methods/guiFrameEvents.lua:32 attempt to index feild '?' (a nil value)

line 31-33:
trainStopEntity.UpdateGui=function(entity)
getFrame(entity,{"station.checkbox"}).state=(global.trainStations[entity.unit_number].callActivated)
end


hmm, maybe i could ...
attempt 1:[

<pre> if getFrame(entity,{"station.checkbox"}).state~=nil then -- check if station has been assigned varibles by Trains_Caller --if station build before mod initial load (or just dont have values, </pre>

-- then give it some
global.trainStations[entity.unit_number]={callActivated=false,station=entity,trainCalled=false}
--
getFrame(entity,{"station.checkbox"}).state=(global.trainStations[entity.unit_number].callActivated)
else -- if has values already, then
getFrame(entity,{"station.checkbox"}).state=(global.trainStations[entity.unit_number].callActivated)
end
]
yay, it works...kinda
but then the activation tic box doesn't stay clicked, and t trying to set up any trains without at least one station active gives yet another error :/
attempt 2,
[
if getFrame(entity,{"station.checkbox"}).state~=nil then -- check if station has been assigned variables by Trains_Caller
--if station build before mod initial load (or just don't have values), then give it some
global.trainStations[entity.unit_number]={callActivated=true,station=entity}
--
getFrame(entity,{"station.checkbox"}).state=(global.trainStations[entity.unit_number].callActivated)
else -- if has values already, then
getFrame(entity,{"station.checkbox"}).state=(global.trainStations[entity.unit_number].callActivated)
end
]
yay, it works...kinda, the activation checkbox stays ticked, the item/signal selection gui can be accessed,
but when in train setup, when an item or number is chosen, errors out
when select station is clicked, errors out

...even for newly built standard stations :(

one error is:
error while running event on_tick (ID 0)
Key station has unknown type

so basically, its having trouble assigning station ids for existing stations for its table, and my poor hax makes it ignore it enough to open the gui, but still ignores the original problem of pre-existing trainstops having no assigned IDs or variables by Trains_Caller. I can't narrow that down in the mod, or how to add it.
[attempts 3-8 have been redacted as nope .flip it. idk.]
hopefully this is some use to you to narrow down the issue

8 years ago

Hi Zuzak
Many thank for real complete answer, very appreciated :)
The solution is easier (Hopefully!) : At the line of the crash, at the train station to the list of train stations, if it doesn't exist.
Theoritically, it should have been done at the init, but... it wasn't!
I updated the fixed version, thanks for your message
Hermios

8 years ago
(updated 8 years ago)

I had to uninstall your mod for a while, and came back to it today, exited to see your post and all the updates.

Now testing with tiny test map, minimal mods to minimize variables
Current error with version 2.1: when a new locomotive is placed on any track, before any station is activated:
Error while running event on_built_entity (ID 6)
Trains_Caller /methods/trainfunctions.lua:47 : attempt to index field 'trains' (a nil value)

relevant comment above code line in question
--if train is nil, means the entity is the train itself
seems to imply this is desired, but is still error-ing out

another error encountered while changing default station:
event_on_tick (ID 0) Trains_Caller /methods/trainfunctions.lua:36: attempt to index a nil value

a simple setup with an oval, a pre-placed train,and 2 stations was tested,
one station as default,
one with a constant combinator wired to it with 2blue as signal, activated
train grid setup with caller, 4 bobs solar panels for power (300kw total), blue>1 as condition.
huh. Train works as expected. while signal is true, it goes to or stays at station.

Lets be silly and try to place another locomotive now.
It works without crash now. o0

maybe try implement some kind of on mod first load, or on game load to start initialize/index trains instead of waiting for placement/activation

ok, what was different now and first test?
station activated before placement? not enough, still crash
ok reload.
crash on selecting the preplaced locomotive
error: Trains_Caller /methods/trainfunctions.lua:81: attempt to index a nil value
wait what?
....
but this was a save right before combinators set up... how...
Still stubbornly optimistic about this. Somehow. It did work for a bit.

8 years ago

Hi Zuzak
I found the issue yesterday. The reason is, that when you didn't research the train caller, the table "trains" is not created. And I forgot to test if it existed before going further in the code.
This is corrected in the new version
Niko

8 years ago

2.2 did work better
still encountered some errors and edge cases

stations and trains placement, manipulation before research completed= no errors. :)

Error while running event on_tick (ID 0)
TrainsCaller/methods/trainFunctions.lua:84: attempt to index field '?' (a nil value)
somewhere during setting up equipment in train, getting distracted and closing it before giving a power source. haven't been able to duplicate, happened on primary giant factory save that was running for ~>hour with this mod and I kinda forgot about activating/researching calling stuff.

Back to quicker loading test map and minimal mods
If no, or only 1 station is activated and wired to circuit during first train setup, at least two stations has to have both colored wires going to them during first train setup, otherwise errors
or when a station has a circuit condition that will evaluate for a train to be called, but is activated later
or when train is at a called station, and I change the train's calling condition

Error while running event on_tick (ID 0)
TrainsCaller/methods/trainFunctions.lua:36: attempt to index a nil value
or
TrainsCaller/methods/trainFunctions.lua:38: attempt to index a nil value
so there needs to be another error catch before assigning the local variables, or doing computations with them
...but but nil=nil can be evaluated correctly... ugh

If I assign a train a calling condition of stone brick = concrete, when at least one active station has none of those values set, it evaluates as true, and it is called there (if multiple, semi-randomly), so it can evaluate nil=nil, which could get messy on large setups, as comparators would not work right, unless done near the station with a combinator outputting a signal with a specific numerical value if true, and then setting calling condition to that.
So, maybe restrict the second calling variable selection box to numbers only to avoid this/force external combinator comparisons if needed.
hmmz
http://lua-users.org/wiki/TablesTutorial
http://lua-users.org/wiki/StoringNilsInTables
I only kinda sorta grok this.

When these cases are avoided, it works wonderfully on my test map :)
much simpler and faster, more extensible, flexible setup than smart trains.
Once these are hammered out, I will finally have a better way to summon fully loaded supply train(s) on demand. with much less combinator trickery.

8 years ago

Hi Zuzak
Well, many thanks for all your feedback
I am actually starting a new game, so I can test it in "real".
I fixed several bugs and improved the solution as well. For example, now, it gonna check at not more than 2 bots are busy, when checking if a train is required for a train station.
I created the condition on bots to avoid that some items seem to be missing, although they are transported by bots.
THe inconvenient of this condition is, that if I unload one item from my train, then robots come to take it, and the condition is not true anymore, so the train considers that it is not needed.
So, if you only have 2 robots on your outpost, just because they are busy doesn't mean that the train is not required anymore.
You may get some extra items, but not that much.
I still have a weakness when a train has more than one type of item, as the inserter that unload may unload the wrong type of item, so empty the train from this one (although not everything is needed), and because the other required item is still not unloaded, the train stays at the station.
I will work on it.
For the issue with no signal, I improved it : If an item is selected in the second variable, but its value is 0, the condition won't be fullfilled anymore. I didn't test it, please do if you can.

But many thanks for your feedback, they are very helpful indeed.
New version 2.3, have fun!

New response