Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Human:getVehicle() causes Lua error when null #111

Open
Deewarz opened this issue Jan 12, 2025 · 1 comment
Open

Human:getVehicle() causes Lua error when null #111

Deewarz opened this issue Jan 12, 2025 · 1 comment
Labels
bug Something isn't working research

Comments

@Deewarz
Copy link
Contributor

Deewarz commented Jan 12, 2025

Description

If we try to get the player's vehicle while he is not in it, we get this error:

Lua error: (straight from the exception): Entity handle '18446744073709551615' is invalid!
[2025-01-12 00:53:58.189] [error] Entity handle '18446744073709551615' is invalid!
stack traceback:
        [C]: in method 'getVehicle'
        gamemode/server/main.lua:118: in local 'foundCommand'
        gamemode/server/main.lua:99: in function <gamemode/server/main.lua:89>

Reproduction

When not in vehicle, connected to a server, type /bug in chat

listenEvent("onChatCommand", function (player, message, command, args)
  if command == "bug" then
    local veh = player:getVehicle() -- causes issue
  end
end)

Possible reason

This could be related to the Human:getVehicle() function which returns Vehicle(-1).
Could we return null / nil instead?

Vehicle Human::GetVehicle() const {
const auto updateData = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
const auto carEnt = flecs::entity(_ent.world(), updateData->carPassenger.carId);
if (carEnt.is_valid() && carEnt.is_alive()) {
return Vehicle(carEnt);
}
return Vehicle(-1);
}

@Deewarz Deewarz added the bug Something isn't working label Jan 12, 2025
@zpl-zak
Copy link
Member

zpl-zak commented Jan 12, 2025

Check if we can return std::optional<Vehicle> here so that null can be returned in Lua VM.

EDIT: sol3 lua wrapper already has an optional<> abstraction, see if it's of any use before using std::optional
https://github.com/ThePhD/sol2/blob/develop/include/sol/optional.hpp

@Segfaultd Segfaultd changed the title [Bug] [Scripting] Human:getVehicle() causes Lua error when null Human:getVehicle() causes Lua error when null Jan 14, 2025
@zpl-zak zpl-zak added this to the racing gamemode milestone Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working research
Projects
Status: No status
Development

No branches or pull requests

3 participants