[Solved] How do I check if a table is a position?

Post Reply
User avatar
Blockhead
Member
Posts: 1622
Joined: Wed Jul 17, 2019 10:14
GitHub: Montandalar
IRC: Blockhead256
In-game: Blockhead Blockhead256
Location: Land Down Under
Contact:

[Solved] How do I check if a table is a position?

by Blockhead » Post

From a Matrix conversation today, for posterity:

Zughy asks:
I can't recall, is there a function to check whether a table is a position?

and adds:

I've got a function passing a table and I want to make sure that the table is a position

Blockhead (I) first attempted to answer:
not an obvious one in the API the best I can think to do is reverse-engineered from reading the Lua builtin code:
getmetatable(t) == vector.metatable
vectors and positions being basically the same thing (I eagerly await everyone's comments on how they are not)

LMD (appgurueu) answers:
vector.check(v) [api ref]

I then reply:
which as is turns out.. has an implementation that checks the same metatable I mentioned
it pays to read more :) [referring to reading documentation to find what you are after, rather than blindly using Ctrl+F]

Further comments:
Vectors and positions both use the same table format in Minetest, which is constructed with vector.new and sets its metatable to the table available at vector.metatable. Thankfully you do not need to know this detail and can use the library function vector.check which takes care of the details for you. You can use this kind of check to change behaviour based on the provided data type (like how vector.add can work with vectors or scalars), or reject invalid input to your APIs.
/˳˳_˳˳]_[˳˳_˳˳]_[˳˳_˳˳\ Advtrains enthusiast | My map: Noah's Railyard | My Content on ContentDB ✝️♂

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: [Solved] How do I check if a table is a position?

by SuperStarSonic » Post

You could also use minetest.is_pos(position) if you didn't want to figure the vector API out.

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: [Solved] How do I check if a table is a position?

by LMD » Post

SuperStarSonic wrote:
Mon Mar 13, 2023 03:23
You could also use minetest.is_pos(position) if you didn't want to figure the vector API out.
There is no minetest.is_pos; grepping the API docs, the C sources and the Lua sources (builtin) yields no result.
My stuff: Projects - Mods - Website

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: [Solved] How do I check if a table is a position?

by SuperStarSonic » Post

That is odd, maybe I used something like it and forgot the name, I'm sorry.

User avatar
Blockhead
Member
Posts: 1622
Joined: Wed Jul 17, 2019 10:14
GitHub: Montandalar
IRC: Blockhead256
In-game: Blockhead Blockhead256
Location: Land Down Under
Contact:

Re: [Solved] How do I check if a table is a position?

by Blockhead » Post

SuperStarSonic wrote:
Mon Mar 13, 2023 03:23
You could also use minetest.is_pos(position) if you didn't want to figure the vector API out.
SuperStarSonic wrote:
Mon Mar 13, 2023 16:44
That is odd, maybe I used something like it and forgot the name, I'm sorry.
By the time you've reached this forum thread, it would be kind of silly to ignore the advice contained in it, don't you think? Don't you also think that if that function were real and not made up, then that would be the answer given? I'm really not sure what you were trying to achieve here.
/˳˳_˳˳]_[˳˳_˳˳]_[˳˳_˳˳\ Advtrains enthusiast | My map: Noah's Railyard | My Content on ContentDB ✝️♂

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: [Solved] How do I check if a table is a position?

by rubenwardy » Post

Did you use ChatGPT to find out that function? It likes to make them up

Image
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

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: [Solved] How do I check if a table is a position?

by SuperStarSonic » Post

Whoa, that's creepy, that looks like I may have come across a bot-scam in a non-trusted site!
What's odd is I think it was on a forum, so I don't think it would of been that convincing.

I know about the fake articles, but aren't they based off of real data?

Post Reply

Who is online

Users browsing this forum: Andarius68 and 5 guests