joe7575 / minecart

Minecart, the lean railway transportation automation system
Other
12 stars 5 forks source link

Added timer to the teleport #7

Closed Konfuz closed 3 years ago

Konfuz commented 3 years ago

This prevents the user from teleporting a second time, since before adding the timeout the target buffer often would receive the same punch event as the origin. (Sometimes leading to the impression that no teleport happend at all when the user ports back right away)

joe7575 commented 3 years ago

Please check this https://rubenwardy.com/minetest_modding_book/en/quality/common_mistakes.html#never-store-objectrefs-ie-players-or-entities and adapt the code accordingly.

Konfuz commented 3 years ago

Thanks for catching that one, it would have been a nightmare to debug.

Any problems with the way I am doing it now?

joe7575 commented 3 years ago

Does this really work? I have not downloaded nor tested this patch. But using function local variables at a time when the function is already finished and the variables not on the stack anymore, seems weird to me. I would feel better which the following approach:

            local playername = puncher:get_player_name()
            local pos = S2P(route.dest_pos)

            local teleport = function(pos, playername)
                -- Make sure the player object still exists
                local player = minetest.get_player_by_name(playername)
                if player and pos then player:set_pos(pos) end
            end
            minetest.after(0.25, teleport, pos, playername)
Konfuz commented 3 years ago

Yes it works. I never did much C-Style development or assembler and mostly do python. Maybe I have to much trust in the GC but my understanding is: The function is an object which still holds a reference to pos and playername and the gc should never eat these unless their ref-counter is zero.

I tested it with

            minetest.after(0.25, collectgarbage)
            minetest.after(1, teleport)

which should trigger a full gc-cycle before using the vars - and it still works fine.

joe7575 commented 3 years ago

Ok, you are right, Lua does not have stack variables