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

[player_api] Small gripe with an assert() #3057

Open
LoneWolfHT opened this issue Aug 28, 2023 · 2 comments
Open

[player_api] Small gripe with an assert() #3057

LoneWolfHT opened this issue Aug 28, 2023 · 2 comments

Comments

@LoneWolfHT
Copy link
Contributor

The function get_player_data() is used for multiple API functions. But it asserts the value of a local table.

-- Player stats and animations
-- model, textures, animation
local players = {}
player_api.player_attached = {}
local function get_player_data(player)
return assert(players[player:get_player_name()])
end

That can be fixed by checking if the player is online via minetest.get_player_by_name() or similar, but that doesn't work for code inside an on_leaveplayer() callback, because player_api's on_leaveplayer() is called first (player_api is a dependency):

minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
players[name] = nil
player_api.player_attached[name] = nil
end)

That also can be fixed, by just not running any player_api functions in an on_leaveplayer(), but in some cases you might have an on_leaveplayer() which calls a function (will name it test_func for later reference) from some other mod's API that's used on players at times other than a player exit, and calls a few player_api functions.

Given the above, the only ways I can think of to avoid a crash is to pcall(), insert your on_leaveplayer() callback to index 1, or when in an on_leaveplayer() callback, pass a bool to test_func which prevents use of player_api functions.

None of those feel like a good solution. I'd prefer to have access to something like player_api._players, or simply have the function return a failure value instead of triggering a crash

@SmallJoker
Copy link
Member

Would it solve your problem if player_api registered its on_leaveplayer callback within minetest.register_on_mods_loaded? That would put move it to the end of the table, effectively calling it after all other mods.

@LoneWolfHT
Copy link
Contributor Author

LoneWolfHT commented Sep 4, 2023

Would it solve your problem if player_api registered its on_leaveplayer callback within minetest.register_on_mods_loaded? That would put move it to the end of the table, effectively calling it after all other mods.

Yeah. It wouldn't solve every possible issue but it fixes mine and is a step in a good direction IMO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants