Page 1 of 1

LuaPlayer :: create_local_flying_text at cursor position

Posted: Sat Dec 28, 2019 4:31 pm
by raiguard
In the vanilla GUI, flying text is used to convey errors:

Image

There is no way AFAIK for us modders to do the same. Matching vanilla behavior as closely as possible is something I strive to do with my mods, and being able to create "error" texts like this would be very nice.

The simplest way to do this that I can think of would be to add an argument to player.create_local_flying_text to create it at the cursor position instead of on the map. Something like this:

Code: Select all

player.create_local_flying_text{
  text = {'fe-gui-search.no-recipes-as-ingredient-error'},
  create_at_cursor = true
}
The create_at_cursor argument would replace position in this case.

Is this something that can be done?

Re: LuaPlayer :: create_local_flying_text at cursor position

Posted: Sat Dec 28, 2019 5:17 pm
by Honktown
You can't get cursor position, but you could center flying text at the position of the button that produced the bad action. If it's on map, use the cursor selected property or entity (player.selected.position, entity.position), etc.

Re: LuaPlayer :: create_local_flying_text at cursor position

Posted: Sat Dec 28, 2019 5:31 pm
by PyroFire
Honktown wrote: Sat Dec 28, 2019 5:17 pm You can't get cursor position
That's why this is a modding interface request.

Seems fairly reasonable.

+1

Re: LuaPlayer :: create_local_flying_text at cursor position

Posted: Sat Dec 28, 2019 5:31 pm
by raiguard
You can't read cursor position, but you can tell the game to put something at the cursor position. If you could read cursor position, things would be very interesting, but that's never going to happen.

Re: LuaPlayer :: create_local_flying_text at cursor position

Posted: Sat Dec 28, 2019 5:43 pm
by Honktown
Raiguard wrote: Sat Dec 28, 2019 5:31 pm You can't read cursor position, but you can tell the game to put something at the cursor position. If you could read cursor position, things would be very interesting, but that's never going to happen.
Eh? how pls

Re: LuaPlayer :: create_local_flying_text at cursor position

Posted: Sat Dec 28, 2019 5:47 pm
by raiguard
Cursor position is not part of the gamestate (the part of the game that is deterministic). Mods run completely in gamestate, and so anything that's not deterministic cannot be read through the API. However, in a few circumstances, mods can interact with things outside of gamestate by writing them. One of these write-only properties is the width and height of a LuaGuiElement.

player.create_local_flying_text() creates the flying text outside of gamestate, so it's entirely possible for the game to attach it to the mouse cursor (or maybe a LuaGuiElement) instead of a place on the map.

If we wanted to be able to read cursor position, it would need to be added to the gamestate, which would mean sending a network packet every time the player's cursor moves even a single pixel. That would bloat network traffic in multiplayer to the extreme, which is why it will never happen.

Re: LuaPlayer :: create_local_flying_text at cursor position

Posted: Sun Sep 13, 2020 8:45 pm
by KingIonTrueLove
+1
I could use this as well for one of my mods. Matching vanilla behavior is a great reason to have such a thing.

Re: LuaPlayer :: create_local_flying_text at cursor position

Posted: Sun Sep 13, 2020 10:46 pm
by Rseding91
Ok, I added it for the next release.

Re: LuaPlayer :: create_local_flying_text at cursor position

Posted: Sun Sep 13, 2020 10:57 pm
by eradicator
Thank you very much :).

Re: LuaPlayer :: create_local_flying_text at cursor position

Posted: Sun Sep 13, 2020 11:44 pm
by raiguard
Thank you!