The functions don`t work.

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

The functions don`t work.

by Andrey01 » Post

Why do minetest.hud_replace_builtin and player:set_physics_override() not work to me? When i tried to call first function it just removed current healthbar and did not add new one. Second function doesn`t work at all.

User avatar
Tmanyo
Member
Posts: 196
Joined: Mon Sep 29, 2014 01:20
GitHub: Tmanyo
IRC: Tmanyo
In-game: tmanyo
Location: United States
Contact:

Re: The functions don`t work.

by Tmanyo » Post

For the set_physics_override function did you look in the API for how it is to be done? player:set_physics_override

I was able to get this to work:

Code: Select all

player:set_physics_override({speed=2,jump=2})
The "player" needs to be userdata, not a name.

Did you provide a valid replacement HUD definition for this function? minetest.hud_replace_builtin
Tmanyo
http://www.rrhmsservers.ml
Servers I Host:
Tmanyo-Realism
Mods of mine that I don't totally hate:
Bank Accounts
T-Rating
Tmusic Player

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

Re: The functions don`t work.

by Andrey01 » Post

Tmanyo wrote:For the set_physics_override function did you look in the API for how it is to be done? player:set_physics_override

I was able to get this to work:

Code: Select all

player:set_physics_override({speed=2,jump=2})
The "player" needs to be userdata, not a name.

Did you provide a valid replacement HUD definition for this function? minetest.hud_replace_builtin
Here is how i wrote:

Code: Select all

local player = minetest.get_player_by_name(playername)
player:set_physics_override({speed = 0.4,
jump = 0.4})
But it did not work.

Replacement HUD definition is valid exactly, because one member suggested correct example (Krock, kinda) to me and I just added new field "text = "poisonous_heart.png".

User avatar
Tmanyo
Member
Posts: 196
Joined: Mon Sep 29, 2014 01:20
GitHub: Tmanyo
IRC: Tmanyo
In-game: tmanyo
Location: United States
Contact:

Re: The functions don`t work.

by Tmanyo » Post

Andrey01 wrote:
Tmanyo wrote:For the set_physics_override function did you look in the API for how it is to be done? player:set_physics_override

I was able to get this to work:

Code: Select all

player:set_physics_override({speed=2,jump=2})
The "player" needs to be userdata, not a name.

Did you provide a valid replacement HUD definition for this function? minetest.hud_replace_builtin
Here is how i wrote:

Code: Select all

local player = minetest.get_player_by_name(playername)
player:set_physics_override({speed = 0.4,
jump = 0.4})
But it did not work.

Replacement HUD definition is valid exactly, because one member suggested correct example (Krock, kinda) to me and I just added new field "text = "poisonous_heart.png".
Is the playername in the function minetest.get_player_by_name() a correct player name? How are you calling this function? What is the trigger?
Tmanyo
http://www.rrhmsservers.ml
Servers I Host:
Tmanyo-Realism
Mods of mine that I don't totally hate:
Bank Accounts
T-Rating
Tmusic Player

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

Re: The functions don`t work.

by Andrey01 » Post

Tmanyo wrote:
Andrey01 wrote:
Tmanyo wrote:For the set_physics_override function did you look in the API for how it is to be done? player:set_physics_override

I was able to get this to work:

Code: Select all

player:set_physics_override({speed=2,jump=2})
The "player" needs to be userdata, not a name.

Did you provide a valid replacement HUD definition for this function? minetest.hud_replace_builtin
Here is how i wrote:

Code: Select all

local player = minetest.get_player_by_name(playername)
player:set_physics_override({speed = 0.4,
jump = 0.4})
But it did not work.

Replacement HUD definition is valid exactly, because one member suggested correct example (Krock, kinda) to me and I just added new field "text = "poisonous_heart.png".
Is the playername in the function minetest.get_player_by_name() a correct player name? How are you calling this function? What is the trigger?
Here is a part of the code where it is shown how i call the function:

Code: Select all

function effects.create_poisoning_effect(playername, effect_time) -- Not whole function definition
    active_effects["poisoning_effect"] = true
    
    local player = minetest.get_player_by_name(playername)
    
    
    minetest.hud_replace_builtin("health", {
        hud_elem_type = "statbar",
        position = {x = 0.5, y = 1},
        text = "poisonous_heart.png",
        number = core.PLAYER_MAX_HP_DEFAULT,
        direction = 0,
        size = {x = 24, y = 24},
        offset = {x = (-10*24) - 25, y = -(48 + 24 + 16)}
    })
    
    player:set_physics_override({
        speed = 0.4,
        jump = 0.4,
        gravity = 1.0,
        sneak = true,
        sneak_glitch = false
    })
end

minetest.register_craftitem("medicine:toxic_apple", {
    description = "Toxic Apple",
    inventory_image = "toxic_apple.png",
    on_use = function (itemstack, user, pointed_thing)
        itemstack:take_item()
            
        return itemstack, effects.create_poisoning_effect(user:get_player_name(), 10)
    end
})

Astrobe
Member
Posts: 577
Joined: Sun Apr 01, 2018 10:46

Re: The functions don`t work.

by Astrobe » Post

I do something similar (sorry, bad indentation due to copy/paste):

Code: Select all

minetest.register_globalstep(function(dtime)
	--Loop through all connected players
	for playerName,playerInfo in pairs(players) do
		local player = minetest.get_player_by_name(playerName)
		if player ~= nil then
			local physics=player:get_physics_override()
			if (player:get_player_control_bits()%32>=16) and playerInfo.energy>dtime then
               physics.gravity=0
               playerInfo.energy=math.max(playerInfo.energy-dtime*2, 0)
            else
				physics.gravity=1 -- math.min(0.8, physics.gravity+0.2)
				if playerInfo.energy < MAX_ENERGY then
					playerInfo.energy=math.min(playerInfo.energy+dtime/2, MAX_ENERGY)
				end
			end
			player:set_physics_override(physics)

			--Update the players's hud energy
			hb.change_hudbar(player, "energy", math.max(0,playerInfo.energy))
		end
	end
end
But one day I installed a mod and it stopped working. This mod was using set_physics_override() like me in global_step(), but it wasn't immediately obvious it would do that sort of thing.

User avatar
Tmanyo
Member
Posts: 196
Joined: Mon Sep 29, 2014 01:20
GitHub: Tmanyo
IRC: Tmanyo
In-game: tmanyo
Location: United States
Contact:

Re: The functions don`t work.

by Tmanyo » Post

Here is a part of the code where it is shown how i call the function:

Code: Select all

function effects.create_poisoning_effect(playername, effect_time) -- Not whole function definition
    active_effects["poisoning_effect"] = true
    
    local player = minetest.get_player_by_name(playername)
    
    
    minetest.hud_replace_builtin("health", {
        hud_elem_type = "statbar",
        position = {x = 0.5, y = 1},
        text = "poisonous_heart.png",
        number = core.PLAYER_MAX_HP_DEFAULT,
        direction = 0,
        size = {x = 24, y = 24},
        offset = {x = (-10*24) - 25, y = -(48 + 24 + 16)}
    })
    
    player:set_physics_override({
        speed = 0.4,
        jump = 0.4,
        gravity = 1.0,
        sneak = true,
        sneak_glitch = false
    })
end

minetest.register_craftitem("medicine:toxic_apple", {
    description = "Toxic Apple",
    inventory_image = "toxic_apple.png",
    on_use = function (itemstack, user, pointed_thing)
        itemstack:take_item()
            
        return itemstack, effects.create_poisoning_effect(user:get_player_name(), 10)
    end
})
I suggest removing the ":get_player_name()" from when you call the function in "minetest.register_craftitem". Also remove the line where you get the player from a name. It is an unneeded step. Maybe you should try just returning the itemstack. Then call the function before you return the itemstack.
Tmanyo
http://www.rrhmsservers.ml
Servers I Host:
Tmanyo-Realism
Mods of mine that I don't totally hate:
Bank Accounts
T-Rating
Tmusic Player

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

Re: The functions don`t work.

by Andrey01 » Post

Tmanyo wrote:
Here is a part of the code where it is shown how i call the function:

Code: Select all

function effects.create_poisoning_effect(playername, effect_time) -- Not whole function definition
    active_effects["poisoning_effect"] = true
    
    local player = minetest.get_player_by_name(playername)
    
    
    minetest.hud_replace_builtin("health", {
        hud_elem_type = "statbar",
        position = {x = 0.5, y = 1},
        text = "poisonous_heart.png",
        number = core.PLAYER_MAX_HP_DEFAULT,
        direction = 0,
        size = {x = 24, y = 24},
        offset = {x = (-10*24) - 25, y = -(48 + 24 + 16)}
    })
    
    player:set_physics_override({
        speed = 0.4,
        jump = 0.4,
        gravity = 1.0,
        sneak = true,
        sneak_glitch = false
    })
end

minetest.register_craftitem("medicine:toxic_apple", {
    description = "Toxic Apple",
    inventory_image = "toxic_apple.png",
    on_use = function (itemstack, user, pointed_thing)
        itemstack:take_item()
            
        return itemstack, effects.create_poisoning_effect(user:get_player_name(), 10)
    end
})
I suggest removing the ":get_player_name()" from when you call the function in "minetest.register_craftitem". Also remove the line where you get the player from a name. It is an unneeded step. Maybe you should try just returning the itemstack. Then call the function before you return the itemstack.
But why? Then player:set_physics_override() won`t work. And why do i need to return just the itemstack?

User avatar
Tmanyo
Member
Posts: 196
Joined: Mon Sep 29, 2014 01:20
GitHub: Tmanyo
IRC: Tmanyo
In-game: tmanyo
Location: United States
Contact:

Re: The functions don`t work.

by Tmanyo » Post

Andrey01 wrote:But why? Then player:set_physics_override() won`t work. And why do i need to return just the itemstack?
I know for a fact that player:set_physics_override() does work. Maybe instead of returning the function alongside the itemstack, you should try calling the function before you return the itemstack. Do what you want. I am just experimenting to try to help.
Tmanyo
http://www.rrhmsservers.ml
Servers I Host:
Tmanyo-Realism
Mods of mine that I don't totally hate:
Bank Accounts
T-Rating
Tmusic Player

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

Re: The functions don`t work.

by Andrey01 » Post

Tmanyo wrote:
Andrey01 wrote:But why? Then player:set_physics_override() won`t work. And why do i need to return just the itemstack?
I know for a fact that player:set_physics_override() does work. Maybe instead of returning the function alongside the itemstack, you should try calling the function before you return the itemstack. Do what you want. I am just experimenting to try to help.
How will this code look like then? Write so

Code: Select all

return effects.create_poisoning_effect(user:get_player_name(), 10), itemstack
?

User avatar
Tmanyo
Member
Posts: 196
Joined: Mon Sep 29, 2014 01:20
GitHub: Tmanyo
IRC: Tmanyo
In-game: tmanyo
Location: United States
Contact:

Re: The functions don`t work.

by Tmanyo » Post

Andrey01 wrote:/quote]
How will this code look like then? Write so

Code: Select all

return effects.create_poisoning_effect(user:get_player_name(), 10), itemstack
?
I have already recommended just returning the itemstack. If you call the function

Code: Select all

effects.create_poisoning_effect(user:get_player_name(), 10)
within the other function before returning the itemstack, I believe it should work. Replacing the order in which the function and itemstack are returned would not solve the problem I don't believe.
Tmanyo
http://www.rrhmsservers.ml
Servers I Host:
Tmanyo-Realism
Mods of mine that I don't totally hate:
Bank Accounts
T-Rating
Tmusic Player

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 10 guests