How to use "get_player_by_name()"?

Post Reply
Chris
Member
Posts: 27
Joined: Thu Mar 01, 2018 11:33

How to use "get_player_by_name()"?

by Chris » Post

I try to get the current player:

Code: Select all

local player = minetest.get_player_by_name(name)
but I don't know the name of the current_player. I've tried already
"currentplayer", "username", current_player, name and get_player_name()
The message shown is "attempt to call global 'get_player_by_name' (a nil value)

hajo
Member
Posts: 606
Joined: Thu Oct 13, 2016 10:45
Location: DE
Contact:

Re: How to use "get_player_by_name()"?

by hajo » Post

Chris wrote:I try to get the current player:

Code: Select all

local player = minetest.get_player_by_name(name)
See the developer-wiki (caution, infos may be outdated).

User avatar
Krock
Developer
Posts: 4649
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: How to use "get_player_by_name()"?

by Krock » Post

In singleplayer, your name is "singleplayer". See "/status" to get a list of the online players in-game. If it should be used on a multiplayer server, then you must get the player name on a different way:
1) minetest.get_connected_players() to get a list (like in "/status") - to apply your effect on all players
2) chatcommand func callback provides you the player name of who ran the command
3) minetest.get_objects_inside_radius(pos, radius) to get all objects (like Lua Entities and players) inside a certain area
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

Chris
Member
Posts: 27
Joined: Thu Mar 01, 2018 11:33

Re: How to use "get_player_by_name()"?

by Chris » Post

I've tried the example from wiki:

Code: Select all

local player = minetest.get_player_by_name("singleplayer")
if not player then
	return -- Player is not online
end
player:set_pos({x=0, y=0, z=0})
debug.txt says:
2018-07-06 11:25:48: ERROR[Main]: ModError: Failed to load and run script from /home/chris/.minetest/games/test/mods/test/init.lua:
2018-07-06 11:25:48: ERROR[Main]: .../chris/.minetest/games/test/mods/test/init.lua:4: attempt to index local 'player' (a nil value)
2018-07-06 11:25:48: ERROR[Main]: stack traceback:
2018-07-06 11:25:48: ERROR[Main]: .../chris/.minetest/games/test/mods/test/init.lua:4: in main chunk

hajo
Member
Posts: 606
Joined: Thu Oct 13, 2016 10:45
Location: DE
Contact:

Re: How to use "get_player_by_name()"?

by hajo » Post

Chris wrote:I've tried the example from wiki:

Code: Select all

local player = minetest.get_player_by_name("singleplayer")
if not player then
	return -- Player is not online
end
player:set_pos({x=0, y=0, z=0})
debug.txt says: ..: attempt to index local 'player' (a nil value)
If the above snippet is your whole script, it would run the moment when MT starts,
ie. when no player has joined yet.

User avatar
Andrey01
Member
Posts: 2574
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Re: How to use "get_player_by_name()"?

by Andrey01 » Post

Chris wrote:I try to get the current player:

Code: Select all

local player = minetest.get_player_by_name(name)
but I don't know the name of the current_player. I've tried already
"currentplayer", "username", current_player, name and get_player_name()
The message shown is "attempt to call global 'get_player_by_name' (a nil value)
Your "name" varyable is nil because it is not defined. That function is often used in callbacks, for example on_use, on_rightclick and etc.

Chris
Member
Posts: 27
Joined: Thu Mar 01, 2018 11:33

Re: How to use "get_player_by_name()"?

by Chris » Post

hajo wrote: If the above snippet is your whole script, it would run the moment when MT starts,
ie. when no player has joined yet.
Is there a workaround to initialize the player before the snipped is executed?

User avatar
Krock
Developer
Posts: 4649
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: How to use "get_player_by_name()"?

by Krock » Post

Chris wrote:Is there a workaround to initialize the player before the snipped is executed?
You could try to delay it:

Code: Select all

minetest.after(5, function()
	-- This code here is called after 5 seconds
	local player = minetest.get_player_by_name("singleplayer")
	-- etc
end)
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
Tcll
Member
Posts: 87
Joined: Thu Jul 25, 2019 21:43
GitHub: Tcll
IRC: Tcll Tcll5850 DarkPikachu
In-game: Tcll
Location: The Gates of Darkness.

Re: How to use "get_player_by_name()"?

by Tcll » Post

I'd just like to comment that rather, instead of delaying with minetest.after()
what I do instead is run the code WHEN the player is valid:

Code: Select all

minetest.register_on_joinplayer(function(player)
    if player then
        ...
    end
end)
this works in single player too

I personally don't like minetest.after() as it's caused stuff like dropping my hand (item_drop installed) whenever the environment desyncs.
if you really need the functionality, write a globalstep implementation instead with a locally reset time variable.

though I'll be honest, it would be nice if minetest.get_player_by_name(nil) would return the current player (or nil if there is no player)

EDIT: also, sorry if this is a necropost
I feel there's more information needed when this thread is searched.

User avatar
SuperStarSonic
Member
Posts: 160
Joined: Fri Oct 14, 2022 20:30
GitHub: Python-Sargent
In-game: SuperStarSonic
Location: Earth (may be out of date)

Re: How to use "get_player_by_name()"?

by SuperStarSonic » Post

Code: Select all

Access denied. Reason: Lua: Runtime error from mod '' in callback on_mods_loaded(): opmod:init.lua:4: attempt to call field 'get_player_by_name' (a nil value)
stack traceback:
	opmod:init.lua:4: in function 'make_op'
	opmod:init.lua:13: in function <opmod:init.lua:12>
	*builtin*:client/register.lua:25: in function <*builtin*:client/register.lua:13>
Do I have a scope problem I didn't notice? (this is a client mod)

This is the code:

Code: Select all

local max_hp = 50

local make_op = function(name)
	local player = minetest.get_player_by_name("name")
	if not player then
		return false -- Player is not online
	end
	player:set_properties({hp_max = max_hp})
	player:set_hp(max_hp)
end

minetest.register_on_mods_loaded(function()
	worked = make_op("singleplayer")
	if worked then
		minetest.log("singleplayer was made OP")
	else
		minetest.log("make_op() failed, trying agian in thirty seconds...")
		minetest.after(30, make_op, "singleplayer")
	end
end)
I tested real quick on oy.edgy1.net, and it didn't work (I replaced singleplayer with my name)

User avatar
Desour
Member
Posts: 1469
Joined: Thu Jun 19, 2014 19:49
GitHub: Desour
IRC: Desour
In-game: DS
Location: I'm scared that if this is too exact, I will be unable to use my keyboard.

Re: How to use "get_player_by_name()"?

by Desour » Post

SuperStarSonic wrote:
Sat Jun 17, 2023 13:40
(this is a client mod)
https://github.com/minetest/minetest/bl ... lua_api.md
he/him; Codeberg; GitHub; ContentDB; public personal TODO list; "DS" is preferred (but often too short)

User avatar
SuperStarSonic
Member
Posts: 160
Joined: Fri Oct 14, 2022 20:30
GitHub: Python-Sargent
In-game: SuperStarSonic
Location: Earth (may be out of date)

Re: How to use "get_player_by_name()"?

by SuperStarSonic » Post

I can't find anything that would cause a problem (except obviously what the disclaimer states).

EDIT: should I use the 'core' namespace?

User avatar
SuperStarSonic
Member
Posts: 160
Joined: Fri Oct 14, 2022 20:30
GitHub: Python-Sargent
In-game: SuperStarSonic
Location: Earth (may be out of date)

Re: How to use "get_player_by_name()"?

by SuperStarSonic » Post

I got rid of some of the stuff just to test, it works until 'make_op()' is called by 'minetest'.after

Code: Select all

local max_hp = 50

local make_op = function(name)
	local player = core.get_player_by_name("SuperStarSonic")
	if not player then
		return false -- Player is not online
	end
	player:set_properties({hp_max = max_hp})
	player:set_hp(max_hp)
end

core.register_on_mods_loaded(function()
	core.after(30, make_op, "SuperStarSonic")
end)

Code: Select all

Access denied. Reason: Lua: Runtime error from mod '' in callback environment_step(): opmod:init.lua:4: attempt to call field 'get_player_by_name' (a nil value)
stack traceback:
	opmod:init.lua:4: in function 'func'
	*builtin*:common/after.lua:20: in function <*builtin*:common/after.lua:5>
	*builtin*:client/register.lua:25: in function <*builtin*:client/register.lua:13>

User avatar
SuperStarSonic
Member
Posts: 160
Joined: Fri Oct 14, 2022 20:30
GitHub: Python-Sargent
In-game: SuperStarSonic
Location: Earth (may be out of date)

Re: How to use "get_player_by_name()"?

by SuperStarSonic » Post

Here's my updated code:

Code: Select all

local make_op = function(player, max_hp)
	if not player then
		return false -- Player is not online
	end
	player:set_properties({hp_max = max_hp})
	player:set_hp(max_hp)
end

minetest.register_chatcommand("make_op", {
	func = function(name, param)
		make_op(minetest.get_player_by_name(name), param)
	end,
})
still doesn't work.

EDIT: I am aware that this is extremely cheaty, but I'm still learning how CSMs work and want to have a little fun :)
PS: SkyBlock is annoying when it glitches and your island disappears :(.

User avatar
LMD
Member
Posts: 1386
Joined: Sat Apr 08, 2017 08:16
GitHub: appgurueu
IRC: appguru[eu]
In-game: LMD
Location: Germany
Contact:

Re: How to use "get_player_by_name()"?

by LMD » Post

Desour pointed you towards client_lua_api.md. These are the API functions you can use for client modding. You can not use all lua_api.md API functions; those are serverside. In particular, minetest.get_player_by_name is serverside, not clientside.
My stuff: Projects - Mods - Website

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests