object:get_id() and get_object_by_id()

Post Reply
User avatar
AiTechEye
Member
Posts: 1000
Joined: Fri May 29, 2015 21:14
GitHub: AiTechEye
Location: Sweden

object:get_id() and get_object_by_id()

by AiTechEye » Post

a method like object:get_id() would make a big difference

like when a enity constantly need to check that it is not hitting it self.

Code: Select all

for _, ob in pairs(minetest.get_objects_inside_radius(pos,1)) do
	local en = ob:get_luaentity()
	if not (en and en.id == self.id) then
		...
	end
end
or checking it is not its own driver

Code: Select all

for _, ob in pairs(minetest.get_objects_inside_radius(pos,3)) do
	local en = ob:get_luaentity()
	if not (en and en.id == self.id or (ob:is_player() and self.driver:get_player_name() == ob:get_player_name())) then
		...
	end
end
would just be

Code: Select all

for _, ob in pairs(minetest.get_objects_inside_radius(pos,3)) do
	local id = ob:get_id()
	if self.object:get_id() ~= id and self.driverid:get_id() ~= id then
		...
	end
end

and a method like minetest.get_object_by_id(id) could also make things easyer.

and because the id is stored in the object, you can acces the object wihtout using minetest.get_objects_inside_radius()
would probability fix lots of problems.

what do you think?

User avatar
rubenwardy
Moderator
Posts: 6972
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: object:get_id() and get_object_by_id()

by rubenwardy » Post

The problem is that objects don't have universal IDs yet. They change when the unload and load. The same ID can be different objects at different points in time. Universal IDs will have to be implemented

You can compare objects using ==. So self.object == obj

Also, note pairs is for any tables, and ipairs is just for array tables. So if your table is an array tavle, you should use ipairs - or even better, a range

Code: Select all

local objs = minetest.get_objects_inside_radius(pos, 1)
for i=1, #objs do
        local ob = objs[i]
	if self.object ~= ob then
		...
	end
end
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
AiTechEye
Member
Posts: 1000
Joined: Fri May 29, 2015 21:14
GitHub: AiTechEye
Location: Sweden

Re: object:get_id() and get_object_by_id()

by AiTechEye » Post

rubenwardy wrote:
Fri Jan 01, 2021 20:42
You can compare objects using ==. So self.object == obj
cool, i had no idea you could compare objects like that.
so i have been messing around by checking variables and player names for years for nothing.
rubenwardy wrote:
Fri Jan 01, 2021 20:42
Also, note pairs is for any tables, and ipairs is just for array tables. So if your table is an array tavle, you should use ipairs
by using pairs for both tables & arrays, will it cause problems?

User avatar
rubenwardy
Moderator
Posts: 6972
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: object:get_id() and get_object_by_id()

by rubenwardy » Post

Pairs is slower than ipairs for an array only table, and provides no benefit. A for range loop is then faster than ipairs for array only tables
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
hex
Member
Posts: 82
Joined: Sun Dec 06, 2020 04:22
IRC: hecks
In-game: hex hhhehehe

Re: object:get_id() and get_object_by_id()

by hex » Post

workaround: create a whole shadow entity system with its own persistent UIDs, object management, and serialization =]

User avatar
sorcerykid
Member
Posts: 1841
Joined: Fri Aug 26, 2016 15:36
GitHub: sorcerykid
In-game: Nemo
Location: Illinois, USA

Re: object:get_id() and get_object_by_id()

by sorcerykid » Post

Object IDs are already exposed in the minetest.luaentities table.

https://dev.minetest.net/Global_tables

So in the on_activate() callback, you could store the object ID thusly:

Code: Select all

for id, v in pairs( minetest.luaentities ) do
        if v == self then self.id = id end
end

User avatar
rubenwardy
Moderator
Posts: 6972
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: object:get_id() and get_object_by_id()

by rubenwardy » Post

The IDs are useless and that's not a stable API, so you shouldn't use it
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
sorcerykid
Member
Posts: 1841
Joined: Fri Aug 26, 2016 15:36
GitHub: sorcerykid
In-game: Nemo
Location: Illinois, USA

Re: object:get_id() and get_object_by_id()

by sorcerykid » Post

rubenwardy wrote:
Tue Jan 05, 2021 10:54
The IDs are useless and that's not a stable API, so you shouldn't use it
This is untrue, because object IDs are printed in the debug log to distinguish LuaEntitySAOs. Hence, they serve a purpose.

Also if it's not a stable API, then it shouldn't be in the global, public "minetest" namespace.

User avatar
rubenwardy
Moderator
Posts: 6972
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: object:get_id() and get_object_by_id()

by rubenwardy » Post

They're an active object Id. They change when the object is unloaded and loaded. The same object may use different IDs, and different objects may use the same ID at different times. It's better to just use an objectref for the same purpose as the active object Id, and find other ways of indexing entities for other purposes (ie: making your own universal id system)
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
runs
Member
Posts: 3225
Joined: Sat Oct 27, 2018 08:32

Re: object:get_id() and get_object_by_id()

by runs » Post

The eternal id issue in coding. Minetest still does not support a "primary key" engine.

Sokomine
Member
Posts: 4276
Joined: Sun Sep 09, 2012 17:31
GitHub: Sokomine
IRC: Sokomine
In-game: Sokomine

Re: object:get_id() and get_object_by_id()

by Sokomine » Post

Such functions would be really useful as part of the general MT api. Right now, all mob mods that need IDs have to implement their own system. It'd really be better to have that done only once.
A list of my mods can be found here.

zeuner
Member
Posts: 55
Joined: Fri Dec 01, 2017 20:09
GitHub: zeuner

Re: object:get_id() and get_object_by_id()

by zeuner » Post

I'm implementing functionality like this as a wrapper to be used with arbitrary mods: https://codeberg.org/zeuner/mtobjid

I plan to extend it with additional convenience functionality, like callbacks to postpone operations on an object identified by its ID when it's currently unloaded.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests