get_rendering (surface, position)

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

get_rendering (surface, position)

Post by darkfrei »

Hi devs!

Is it possible to get the rendering rectangle by some position? Something like

Code: Select all

local id = surface.get_rendering (position, mod_name)
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: get_rendering (surface, position)

Post by eradicator »

I have to admit that it would be quite nice to not have to remember all ids ever used in global but instead be able to retrieve them from the world somehow. Though i think for me it would me most useful to get a) renderings attached to a specific LuaEntity and b) only retrieve ids of renderings that the calling mod created itself.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Rseding91
Factorio Staff
Factorio Staff
Posts: 15901
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: get_rendering (surface, position)

Post by Rseding91 »

I don't understand what you're asking for. You want "get_rendering" but then reference a draw rectangle function? Additionally the other post is talking about getting render object IDs?
If you want to get ahold of me I'm almost always on Discord.
Rseding91
Factorio Staff
Factorio Staff
Posts: 15901
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: get_rendering (surface, position)

Post by Rseding91 »

eradicator wrote: Sat Aug 17, 2019 1:59 am I have to admit that it would be quite nice to not have to remember all ids ever used in global but instead be able to retrieve them from the world somehow. Though i think for me it would me most useful to get a) renderings attached to a specific LuaEntity and b) only retrieve ids of renderings that the calling mod created itself.
Those would all be O(N) operations. Just store the IDs you make when you tell the game to draw them.
If you want to get ahold of me I'm almost always on Discord.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: get_rendering (surface, position)

Post by eradicator »

Rseding91 wrote: Sun Aug 18, 2019 4:39 am
eradicator wrote: Sat Aug 17, 2019 1:59 am I have to admit that it would be quite nice to not have to remember all ids ever used in global but instead be able to retrieve them from the world somehow. Though i think for me it would me most useful to get a) renderings attached to a specific LuaEntity and b) only retrieve ids of renderings that the calling mod created itself.
Those would all be O(N) operations. Just store the IDs you make when you tell the game to draw them.
So are LuaSurface.find_entities_filtered operations i presume. And i *do* store my entity references and render IDs i create (obviously, as that's currently the only possible way to do it). But sometimes in non-performance-critical applications it would reduce mod-side code complexity if i didn't *have* to. It's imho a typical "if it was in base game mods wouldn't have to reinvent the wheel" situation.

As stated this is rather low priority to me. But if "it's O(N)" was a valid counter argument then half of the modding api would never have been implemented.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Rseding91
Factorio Staff
Factorio Staff
Posts: 15901
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: get_rendering (surface, position)

Post by Rseding91 »

find_entities_filtered is O(N) on the entities that overlap the area you ask for rounded up to 2 tiles. LuaRendering is O(N) on the number of things stored in LuaRendering for that surface.
If you want to get ahold of me I'm almost always on Discord.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: get_rendering (surface, position)

Post by darkfrei »

So if i have the rendering.rectangle somewhere on the map, but I know the position (or connected entity), then the best way is to scan all rendering objects, check them if they are rectangles, get the corners for this rectangles and check if the input position is inside of this rectangle?

Yes, I need to check that they are on the same surface.
Bilka
Factorio Staff
Factorio Staff
Posts: 3426
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: get_rendering (surface, position)

Post by Bilka »

darkfrei wrote: Sun Aug 18, 2019 8:42 am So if i have the rendering.rectangle somewhere on the map, but I know the position (or connected entity), then the best way is to scan all rendering objects, check them if they are rectangles, get the corners for this rectangles and check if the input position is inside of this rectangle?

Yes, I need to check that they are on the same surface.
Yes, that is what the C++ would be doing. Which is why it's not a thing: It's not only O(n), the operation that that to be done for every n is also pretty expensive.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
slippycheeze
Filter Inserter
Filter Inserter
Posts: 587
Joined: Sun Jun 09, 2019 10:40 pm
Contact:

Re: get_rendering (surface, position)

Post by slippycheeze »

darkfrei wrote: Sun Aug 18, 2019 8:42 am So if i have the rendering.rectangle somewhere on the map, but I know the position (or connected entity), then the best way is to scan all rendering objects, check them if they are rectangles, get the corners for this rectangles and check if the input position is inside of this rectangle?
Yes, I need to check that they are on the same surface.
Nope, the "best" way to do this, for the most common values of best, is using a tree structure to index the space for efficiently locating the device -- typically a variant of an R-tree, a BSP-derived tree like a k-d tree or quadtree, or maybe if you are feeling really exotic, some other structure using exotic space filling hibbert curves, or something. Since you have very limited search requirements a k-d tree should sort you right out, though, and is the easiest of those to implement. (Index surface as a Z dimension if you really can't limit your range to even same-surface indexes.)

The performance of this won't really be substantially different to having it done on the C++ side, though, since you already want to do this in a terrible way with an index operation, rather than just remembering where you put things -- or indexing them together yourself, using a classic map, which Lua does a really nice job on.

...but, yeah, if you want to fetch them from the world, do it by indexing them into an appropriate data structure. I mean, it'd be a terrible way to do it compared to, y'know, just writing those 64 bytes of memory or so into the save file, but YOLO, right?
Post Reply

Return to “Modding interface requests”