HELP PLEASE!!

Post Reply
User avatar
PiJeyEe
Member
Posts: 16
Joined: Wed Nov 04, 2020 03:58
GitHub: PiJeyEe
IRC: PiJeyEe
In-game: Pathreen

HELP PLEASE!!

by PiJeyEe » Post

Can someone please help me with this coding, I need tool cooldown for my mod.
I checked every mod with cooldown like kits, death timer, place limit and more but I still can't complete it.

I'm stuck with this coding;

on_use = function(itemstack, user, pointed_thing)

core.register_globalstep(function(self, dtime)

self.timer= self.timer + dtime

local failure = false

if self.timer >= 10 then

if(pointed_thing.type == "node") then
failure = not flash.teleport(user, pointed_thing)

self.timer = 0

end)


else

failure = true
core.chat_send_player(name, "Still Cooldown!")


end

})

I also did this but it is doing the opposite of what I want, instead of waiting for tools to be use it is doing its function within the time of supposed cooldown;

on_use = function(itemstack, user, pointed_thing)


core.register_globalstep(function(dtime)
local timer = 0
timer = timer + dtime;

local failure = false

if(pointed_thing.type == "node" and timer >= 5) then
failure = not flash.teleport(user, pointed_thing)

else

failure = true
core.chat_send_player(name, "Still Cooldown!")

end
end)
end


})

Please help me, Im using Minetest version 0.4.17, all help would gladly appreciated. Thank you

User avatar
Gael de Sailly
Member
Posts: 845
Joined: Sun Jan 26, 2014 17:01
GitHub: gaelysam
IRC: Gael-de-Sailly
In-game: Gael-de-Sailly gaelysam
Location: Voiron, France

Re: HELP PLEASE!!

by Gael de Sailly » Post

Hi, and happy new year :)

You should use minetest.after instead of core.register_globalstep.
register_globalstep is a very intensive and "dangerous" procedure. It registers a function that runs permanently (at every server iteration) until you shut down the server. Here, each time you use your item, it will make a new copy of the function, and these multiple calls will ruin server performance if there are too many of them.

minetest.after(time, function) calls a function after a delay, and once, so I think it fits your needs.

So what I would propose is this:

Code: Select all

on use = function(itemstack, user, pointed_thing)
	local failure = true
	if pointed_thing.type == "node" then
		minetest.after(5, function()
			failure = not flash.teleport(user, pointed_thing) 
		end)
	end
end
As you can see we get rid of the timer, minetest.after deals with it. I also propose to move the check for pointed_thing.type == "node" outside of the delayed function, because you don't need to wait for 5 seconds to determine whether you are pointing to a node or not.

Also you are using 0.4.17 which is quite outdated. I would suggest updating to recent version (5.x) if you can, and make sure your code is compatible with this new version. If it's not, most players will not use your mod.

A small tip for the forum: when you paste code, use the code block (the button with </>), it displays it in a nice box with a scrollbar if needed, and with indentations :)
Just realize how bored we would be if the world was perfect.

User avatar
PiJeyEe
Member
Posts: 16
Joined: Wed Nov 04, 2020 03:58
GitHub: PiJeyEe
IRC: PiJeyEe
In-game: Pathreen

Re: HELP PLEASE!!

by PiJeyEe » Post

Gael de Sailly wrote:
Fri Jan 01, 2021 11:27
Hi, and happy new year :)

You should use minetest.after instead of core.register_globalstep.
register_globalstep is a very intensive and "dangerous" procedure. It registers a function that runs permanently (at every server iteration) until you shut down the server. Here, each time you use your item, it will make a new copy of the function, and these multiple calls will ruin server performance if there are too many of them.

minetest.after(time, function) calls a function after a delay, and once, so I think it fits your needs.

So what I would propose is this:

Code: Select all

on use = function(itemstack, user, pointed_thing)
	local failure = true
	if pointed_thing.type == "node" then
		minetest.after(5, function()
			failure = not flash.teleport(user, pointed_thing) 
		end)
	end
end
As you can see we get rid of the timer, minetest.after deals with it. I also propose to move the check for pointed_thing.type == "node" outside of the delayed function, because you don't need to wait for 5 seconds to determine whether you are pointing to a node or not.

Also you are using 0.4.17 which is quite outdated. I would suggest updating to recent version (5.x) if you can, and make sure your code is compatible with this new version. If it's not, most players will not use your mod.

A small tip for the forum: when you paste code, use the code block (the button with </>), it displays it in a nice box with a scrollbar if needed, and with indentations :)
Hi, Happy New Year! thank you so much for giving me an idea and info regarding posting, I tried to run your code and the tool did the opposite. When I'm using it, it doesn't give cooldown but its delaying the function.

I am using 0.4.17 for a reason, my iPhone can only play this version. I will update to 5.x when I need it, for now I will use this old version and also I am not planning on uploading mods, its for my personal use only.

Again, thank you so much have a great day!

User avatar
Gael de Sailly
Member
Posts: 845
Joined: Sun Jan 26, 2014 17:01
GitHub: gaelysam
IRC: Gael-de-Sailly
In-game: Gael-de-Sailly gaelysam
Location: Voiron, France

Re: HELP PLEASE!!

by Gael de Sailly » Post

PiJeyEe wrote:
Fri Jan 01, 2021 13:43
I tried to run your code and the tool did the opposite. When I'm using it, it doesn't give cooldown but its delaying the function.
Ok I think I did not understand your cooldown concept at first, I thought you just wanted to call a function with a delay.

If I'm correct you want to disallow the use of an item during 5 seconds after use.
For this you don't exactly need a timer function like before, you just need to know whether the object was used in the last 5 seconds or not.

What you can do is to write the time of last use in the item metadata, and when the item is used, check the time difference.
You can obtain the time in seconds since world creation with minetest.get_gametime().
You can access the item's metadata by local meta = itemstack:get_meta(), and then you can read/write variables with meta:get_int(variable) and meta:set_int(variable, value).

The code below tests whether there is at least 5 seconds delay (line 6) and if it's OK, calls your function (line 7) and updates the item's last use time (line 8).
An important thing is to return the itemstack (line 12), because its data can be modified.

There is just one problem here, I don't know how meta:get_int (line 3) behaves when the variable is not set, and I have not found an easy way to test it. Maybe it can be needed to check that last_use_time is not nil.

Code: Select all

on_use = function(itemstack, user, pointed_thing)
	local meta = itemstack:get_meta()
	local last_use_time = meta:get_int("last_use")
	local current_time = minetest.get_gametime()
	local failure = true
	if current_time - last_use_time >= 5 then
		failure = not flash.teleport(user, pointed_thing)
		meta:set_int("last_use", current_time)
	else
		core.chat_send_player(name, "Still Cooldown!")
	end
	return itemstack
end
Just realize how bored we would be if the world was perfect.

User avatar
PiJeyEe
Member
Posts: 16
Joined: Wed Nov 04, 2020 03:58
GitHub: PiJeyEe
IRC: PiJeyEe
In-game: Pathreen

Re: HELP PLEASE!!

by PiJeyEe » Post

Gael de Sailly wrote:
Fri Jan 01, 2021 16:04
PiJeyEe wrote:
Fri Jan 01, 2021 13:43
I tried to run your code and the tool did the opposite. When I'm using it, it doesn't give cooldown but its delaying the function.
Ok I think I did not understand your cooldown concept at first, I thought you just wanted to call a function with a delay.

If I'm correct you want to disallow the use of an item during 5 seconds after use.
For this you don't exactly need a timer function like before, you just need to know whether the object was used in the last 5 seconds or not.

What you can do is to write the time of last use in the item metadata, and when the item is used, check the time difference.
You can obtain the time in seconds since world creation with minetest.get_gametime().
You can access the item's metadata by local meta = itemstack:get_meta(), and then you can read/write variables with meta:get_int(variable) and meta:set_int(variable, value).

The code below tests whether there is at least 5 seconds delay (line 6) and if it's OK, calls your function (line 7) and updates the item's last use time (line 8).
An important thing is to return the itemstack (line 12), because its data can be modified.

There is just one problem here, I don't know how meta:get_int (line 3) behaves when the variable is not set, and I have not found an easy way to test it. Maybe it can be needed to check that last_use_time is not nil.

Code: Select all

on_use = function(itemstack, user, pointed_thing)
	local meta = itemstack:get_meta()
	local last_use_time = meta:get_int("last_use")
	local current_time = minetest.get_gametime()
	local failure = true
	if current_time - last_use_time >= 5 then
		failure = not flash.teleport(user, pointed_thing)
		meta:set_int("last_use", current_time)
	else
		core.chat_send_player(name, "Still Cooldown!")
	end
	return itemstack
end
Whoa, omg you really helped me out, you did everything and I'm sorry if I took some of your time. Thank you so much. I would gladly share the mod I'm doing when its done. Fyi regarding my mod, I'm doing the league of legends mod and among us mod. This is almost the key for everything.

Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests