Modded car randomly disapears?

Place to get help with not working mods / modding interface.
Kiplacon
Burner Inserter
Burner Inserter
Posts: 12
Joined: Tue Jan 16, 2018 8:14 pm
Contact:

Modded car randomly disapears?

Post by Kiplacon »

A crash report for my mod recently revealed a weird situation where cars disappear seemingly at random.
This is my mod here: https://mods.factorio.com/mod/RenaiTransportation
and this is the report: https://mods.factorio.com/mod/RenaiTran ... 1d8601a94e

The mod has train ramps that you place on rails and then trains can hit the ramp, jump into the air, and land back on the rail. The ramp itself is a simple-entity-with-owner that has the "train-layer" collision mask and it essentially works like this:
1. Train collides with ramp, triggering on_entity_damaged
2. From on_entity_damaged, properties about the train are recorded in global variables before it is destroyed. The game tick it will land, the train's air time, and other things are arbitrarily calculated based on how fast the train was going when it collided with the ramp
3. An invisible, intangible, frictionless, belt-immune car is spawned at the train's position, it is given the speed the train had during collision. If there was a player in the train, they are placed inside the car as the rider
4. I set the car.destructible() = false and create some sprites of the train and train mask where the car is the target, meaning if the car is destroyed so will the sprites.
5. on_tick, while the game.tick is less than the calculated landing tick, the car moves under the speed of the initial "push" in step 3, and the sprites are manipulated to look like a train is jumping
6. on_tick, when the game.tick == the landing tick and if there is rail under the car, the car is destroyed, the train is reformed, and it goes on.

There is a different "magnetic" version of the train ramp that recalculates all the air time and landing tick stuff so that the train always lands at a certain spot, this is what user Legowaffles was using in the crash reportImage

I recreated the setup here:
test2222.zip
dissapearing car?
(1.1 MiB) Downloaded 111 times
.
You should only need my mod and the Creative Mod to recreate https://mods.factorio.com/mod/creative-mod.

Manually drive the train towards the ramp however you like. Sometimes mid-jump the game will crash, sometimes it wont. I would say 4/5 times it crashes, the slower you go the more likely. But bizarrely, if you connect the gap in the rails where the train jumps, it will almost never crash no matter how slow you are going.

By the error message, I traced the issue to be that the sprite of the train that the code is trying to animate during the jump suddenly went missing. I gave the car a visible image just to test and I found that for whatever reason the car sometimes just disappears, which takes the sprites with it, and thus causing the crash. When the game crashes, the player character is visible on the ground too, so it was kicked out of the car when it disappeared. The only time in the code I destroy the car is when the game.tick == the landing tick, and I set the car.destructible() = false so nothing should be damaging it. I also don't believe I am miscalculating anything about the landing tick or air time because this issue only came up recently (crash posted 2 weeks ago, ramp existed for 7 months), and it wouldn't explain why connecting the gap in the rails would prevent the crash. I checked the changelog, the car prototype, and various areas in the mod code but couldn't find anything that might be causing the car to sometimes disappear and sometimes not
Pi-C
Smart Inserter
Smart Inserter
Posts: 1726
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Modded car randomly disapears?

Post by Pi-C »

Kiplacon wrote: Sun May 30, 2021 5:33 pm The only time in the code I destroy the car is when the game.tick == the landing tick, and I set the car.destructible() = false so nothing should be damaging it.
Not true! That's the only time you destroy it in script/trains/on_tick.lua, but there are other places where you do destroy stuff. I've added some logging directives to your code, so here's a log of what actually happens:

Code: Select all

5102.115 Script @__RenaiTransportation__/script/trains/on_tick.lua:68: Tick: 54298
5102.115 Script @__RenaiTransportation__/script/trains/on_tick.lua:455: ANITMATING TRAIN!
5102.115 Script @__RenaiTransportation__/script/trains/animation.lua:6: properties.GuideCar.unit_number: 402
5102.132 Script @__RenaiTransportation__/script/trains/on_tick.lua:68: Tick: 54299
5102.132 Script @__RenaiTransportation__/script/trains/on_tick.lua:455: ANITMATING TRAIN!
5102.132 Script @__RenaiTransportation__/script/trains/animation.lua:6: properties.GuideCar.unit_number: 402
5102.148 Script @__RenaiTransportation__/script/trains/on_tick.lua:68: Tick: 54300
5102.148 Script @__RenaiTransportation__/script/trains/on_tick.lua:455: ANITMATING TRAIN!
5102.148 Script @__RenaiTransportation__/script/trains/animation.lua:6: properties.GuideCar.unit_number: 402
5102.148 Script @__RenaiTransportation__/control.lua:54: Entered CLEAR INVALID THINGS!
5102.148 Script @__RenaiTransportation__/control.lua:71: Checking world
5102.148 Script @__RenaiTransportation__/control.lua:73: each: nauvis	world: nauvis
5102.153 Script @__RenaiTransportation__/control.lua:75: every: 1	ZiplinePart: RTPropCar	unit_number: 402
5102.153 Script @__RenaiTransportation__/control.lua:78: all: 1	player: {
  reset = true
}
5102.153 Script @__RenaiTransportation__/control.lua:79: 	ZiplinePart.unit_number: 402
5102.153 Script @__RenaiTransportation__/control.lua:89: Destroying RTPropCar
5102.155 Script @__RenaiTransportation__/script/trains/on_tick.lua:68: Tick: 54301
5102.155 Script @__RenaiTransportation__/script/trains/on_tick.lua:455: ANITMATING TRAIN!
5102.155 Script @__RenaiTransportation__/script/trains/animation.lua:6: properties.GuideCar.unit_number: not valid
This code, at the end of on_nth_tick(300), is where things go wrong:

Code: Select all

  for each, world in pairs(game.surfaces) do
    for every, ZiplinePart in pairs(world.find_entities_filtered{name = {"RTZipline", "RTZiplinePowerDrain", "RTPropCar"}}) do
      local owned = false
      for all, player in pairs(global.AllPlayers) do
        if ((player.ChuggaChugga and ZiplinePart.unit_number == player.ChuggaChugga.unit_number)
        or  (player.succ and ZiplinePart.unit_number == player.succ.unit_number)
        or  (player.LetMeGuideYou and ZiplinePart.unit_number == player.LetMeGuideYou.unit_number)
        ) then
          owned = true
        end
      end
      if (owned == false) then
log("Destroying " .. ZiplinePart.name)
        ZiplinePart.destroy()
      end
    end
  end
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Kiplacon
Burner Inserter
Burner Inserter
Posts: 12
Joined: Tue Jan 16, 2018 8:14 pm
Contact:

Re: Modded car randomly disapears?

Post by Kiplacon »

Damn nice catch, yeah I use the invisible car to also guide the zipline and I forgot to think about cases where I destroyed the car for zipline purposes. That must mean that the randomness of the car disappearing was because that check which only happens every 5 seconds was sometimes happening during a jump. Most jumps take only 1-2 seconds so I guess people just got lucky dodging the crash for now. That would also mean that a jump that takes longer than 5 seconds is guaranteed to crash. I took out RTPropCar from the find_entities_filtered and so far it's not disappearing anymore. Switching back and forth between with and without the RTPropCar check, it's looking pretty clear that that was the cause. Thank you very much, I never would have thought of that :D
Pi-C
Smart Inserter
Smart Inserter
Posts: 1726
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Modded car randomly disapears?

Post by Pi-C »

Kiplacon wrote: Tue Jun 01, 2021 1:19 am Damn nice catch, yeah I use the invisible car to also guide the zipline and I forgot to think about cases where I destroyed the car for zipline purposes. That must mean that the randomness of the car disappearing was because that check which only happens every 5 seconds was sometimes happening during a jump. Most jumps take only 1-2 seconds so I guess people just got lucky dodging the crash for now. That would also mean that a jump that takes longer than 5 seconds is guaranteed to crash.
It also explains why a crash was more likely to happen with slow than with fast trains …
I took out RTPropCar from the find_entities_filtered and so far it's not disappearing anymore. Switching back and forth between with and without the RTPropCar check, it's looking pretty clear that that was the cause. Thank you very much, I never would have thought of that :D
You're welcome! Glad it works now. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Post Reply

Return to “Modding help”