Page 1 of 1

Changing LuaCustomChartTag::position to writable? Or?

Posted: Mon Jul 29, 2019 5:24 pm
by Schallfalke
It seems LuaCustomChartTag::position is read-only?

https://lua-api.factorio.com/latest/Lua ... rtTag.html shows:

Code: Select all

class LuaCustomChartTag
...
position :: Position [R]	The position of this tag.
...
My use case is that I want to make a mod that auto-align chart tags to chunks, so the icons will tile nicely on the map.
So I can imagine of the following ways to achieve this:
  1. API allows write to "position". So I can simply change that on event "on_chart_tag_added".
  2. API adds a new event "on_pre_chart_tag_added", so I can modify that before it is read-only.
  3. Within event "on_chart_tag_added", I will clone the tag, modify the value, then use "force.add_chart_tag(surface, newtag)". Then destroy the "old" tag.
However, I am not so successful with that. Reasons are:
Method 3 has the problem of recursive calling... Using "force.add_chart_tag(surface, newtag)" will trigger another event "on_chart_tag_added". There seems to be no way to distinguish if the tag is created by script or by actual player clicking on map.
Method 1 and 2 both require API changes.

TL;DR
So if there are other workarounds, may I request either of these API changes:
  1. LuaCustomChartTag::position is also writable. (Thus [RW].)
  2. New event "on_pre_chart_tag_added", which is executed before a tag is really added to the map. Most importantly, "position" can be modified in this event.
Regards,
Schall

Re: Changing LuaCustomChartTag::position to writable? Or?

Posted: Mon Jul 29, 2019 5:28 pm
by pleegwat
How about before replacing the new tag, check if it is already chunk-aligned?

Re: Changing LuaCustomChartTag::position to writable? Or?

Posted: Mon Jul 29, 2019 5:53 pm
by Schallfalke
pleegwat wrote: Mon Jul 29, 2019 5:28 pm How about before replacing the new tag, check if it is already chunk-aligned?
Let me show my code of method 3:

Code: Select all

  local area = { {npos.x - 0.1, npos.y - 0.1}, {npos.x + 0.1, npos.y + 0.1} }
  if next(force.find_chart_tags(surface, area)) == nil then
    force.add_chart_tag(surface, newtag)
    tag.destroy()
  end
If position is the only thing to modify, this above check works.
BUT if I want to other changes (like appending to text with coordinates "(xxx,yyy)", the above checks will also skip those "accurately clicked" tags. So users will find some tags are lacking that text info.

Allow writing to LuaCustomChartTag::position

Posted: Sat Aug 30, 2025 8:17 pm
by Pi-C
Moving a LuaCustomChartTag around is easy for players (click on the tag, then on the button next to the trash-can icon), but impossible for mods:

Currently, if we want to move a LuaCustomChartTag, we cannot simply change its position because LuaCustomChartTag::position is a read-only value. Instead, we have to destroy the existing tag and recreate it on the new position. This is not really efficient, because the game has to deal with more objects and will also raise more events (on_chart_tag_removed and on_chart_tag_added vs. just on_chart_tag_modified).

As players are allowed to move a chart tag, I suppose the game must already have a way to modify the chart tag position. Could you make LuaCustomChartTag::position writable, please, so that mods can move chart tags as well?

Re: Changing LuaCustomChartTag::position to writable? Or?

Posted: Thu Sep 04, 2025 2:48 am
by Bilka
Okay, added LuaCustomChartTag::position and surface write for 2.0.67.

Re: Changing LuaCustomChartTag::position to writable? Or?

Posted: Thu Sep 04, 2025 8:51 am
by Pi-C
Great news, thank you very much! :-)