Page 1 of 1

[0.17.71] Cross-surface teleporting of cars has a bug

Posted: Wed Oct 16, 2019 3:52 pm
by Pi-C
Consider this code snippet:

Code: Select all

log("entity.position: " .. tostring(entity.position))
log("entity.surface.name: " .. tostring(entity.surface.name))

log("vehicle.valid: " .. tostring(vehicle.valid))
log("vehicle.name: " .. tostring(vehicle.name))
log("vehicle.surface.name: " .. tostring(vehicle.surface.name))

    local teleported = vehicle.teleport(entity.position, entity.surface)

log("teleported: " .. tostring(teleported))
log("vehicle.valid: " .. tostring(vehicle.valid))
Output when player and vehicle are on the same surface:

Code: Select all

 184.995 Script @__GCKI__/control.lua:482: entity.position: table
 184.995 Script @__GCKI__/control.lua:483: entity.surface.name: nauvis
 184.995 Script @__GCKI__/control.lua:485: vehicle.valid: true
 184.995 Script @__GCKI__/control.lua:486: vehicle.name: car
 184.995 Script @__GCKI__/control.lua:487: vehicle.surface.name: nauvis
 184.995 Script @__GCKI__/control.lua:491: teleported: true
 184.995 Script @__GCKI__/control.lua:492: vehicle.valid: true
Output when player and vehicle are on different surfaces:

Code: Select all

 236.546 Script @__GCKI__/control.lua:482: entity.position: table
 236.546 Script @__GCKI__/control.lua:483: entity.surface.name: Factory floor 1
 236.546 Script @__GCKI__/control.lua:485: vehicle.valid: true
 236.546 Script @__GCKI__/control.lua:486: vehicle.name: car
 236.546 Script @__GCKI__/control.lua:487: vehicle.surface.name: nauvis
 236.546 Script @__GCKI__/control.lua:491: teleported: true
 236.546 Script @__GCKI__/control.lua:492: vehicle.valid: false
The vehicle is moved to the other surface, as the screenshot shows, but it's become invalid in the process, so I accessing its properties will crash the game.

Re: [0.17.71] Cross-surface teleporting of cars has a bug

Posted: Wed Oct 16, 2019 5:02 pm
by boskid
This is kind of engine limitation, when removing car from first surface, lua handle gets disconnected from car.

To workaround this, before teleport you may read car's unit_number and then after teleport find this entity on target surface.

For now this is minor issue

Re: [0.17.71] Cross-surface teleporting of cars has a bug

Posted: Wed Oct 16, 2019 6:16 pm
by Pi-C
boskid wrote: Wed Oct 16, 2019 5:02 pm This is kind of engine limitation, when removing car from first surface, lua handle gets disconnected from car.
So, when teleporting to another surface, "vehicle.teleport(entity.position, entity.surface)" is basically just like an alias for

Code: Select all

vehicle.clone{position=entity.position, surface=entity.surface}
destroy(vehicle)
that doesn't raise on_entity_cloned?
To workaround this, before teleport you may read car's unit_number and then after teleport find this entity on target surface.
Do I really need the unit_number, or wouldn't this also be safe?

Code: Select all

if vehicle.teleport(entity.position, entity.surface) then
	vehicle = entity.surface.find_entity("car", entity.position)
end
For now this is minor issue
OK, as long as it works. :-)

Re: [0.17.71] Cross-surface teleporting of cars has a bug

Posted: Wed Oct 16, 2019 6:23 pm
by Pi-C
Pi-C wrote: Wed Oct 16, 2019 6:16 pm
boskid wrote: Wed Oct 16, 2019 5:02 pm This is kind of engine limitation, when removing car from first surface, lua handle gets disconnected from car.
So, when teleporting to another surface, "vehicle.teleport(entity.position, entity.surface)" is basically just like an alias for

Code: Select all

vehicle.clone{position=entity.position, surface=entity.surface}
destroy(vehicle)
that doesn't raise on_entity_cloned?
Answering that myself: No, because teleport leaves driver/passenger inside the car. :-)

Re: [0.17.71] Cross-surface teleporting of cars has a bug

Posted: Wed Oct 16, 2019 9:14 pm
by Rseding91
To expand on this: this is a known side effect of cross-surface teleportation and the same thing happens with character entities when teleporting them across surfaces. I don't have a perfect solution to it yet so that's just how it works for now. When I figure out how to solve the issue(s) then I will implement them.

Re: [0.17.71] Cross-surface teleporting of cars has a bug

Posted: Thu Oct 17, 2019 11:20 am
by eradicator
boskid wrote: Wed Oct 16, 2019 5:02 pm This is kind of engine limitation, when removing car from first surface, lua handle gets disconnected from car.

To workaround this, before teleport you may read car's unit_number and then after teleport find this entity on target surface.
Do we need to place a seperate order of LuaSurface.find_entities_filtered{unit_number=...} in the mean time or does it come with the main dish?

Re: [0.17.71] Cross-surface teleporting of cars has a bug

Posted: Thu Oct 17, 2019 12:09 pm
by eradicator
Rseding91 wrote: Wed Oct 16, 2019 9:14 pm When I figure out how to solve the issue(s) then I will implement them.
On second thought: Would it be possible to return the new entity as the second return value of .teleport()?

Re: [0.17.71] Cross-surface teleporting of cars has a bug

Posted: Thu Oct 17, 2019 12:53 pm
by Pi-C
Rseding91 wrote: Wed Oct 16, 2019 9:14 pm To expand on this: this is a known side effect of cross-surface teleportation and the same thing happens with character entities when teleporting them across surfaces. I don't have a perfect solution to it yet so that's just how it works for now. When I figure out how to solve the issue(s) then I will implement them.
I just tried to teleport a car with a player inside, and after switching over to the other player, I could leave the car and the character was on the same surface (unlike what happens with clone{}, where the connection between player and character was lost). Adding the new entity to the return values, as suggested by eradicator, could work IMHO. (Of course, I still would have to update my tables so that they reference the teleported vehicle. I just wouldn't have to add the code for finding the correct vehicle.)

Re: [0.17.71] Cross-surface teleporting of cars has a bug

Posted: Thu Oct 17, 2019 6:11 pm
by Rseding91
making teleport() return an updated reference doesn't fix that it causes every other lua reference to become invalidated and every other game reference to be come invalidated so i don't want to do that. It's not a new entity - it's just clearing the references.