Page 1 of 1

Reading dimensions of sprites during control stage.

Posted: Tue Sep 28, 2021 2:24 pm
by Creidhne
Why ?

For Dana, I need precise control of the size/position/x-y ratio of sprites in rendering.draw_sprite(). For some kind of sprites (ex: technology, entities) which don't have standard dimensions like item sprites, this doesn't seem possible with the current API.

Proposition

1) New read-only class/concept to get informations about sprites.

Code: Select all

LuaSpriteInfo {
    valid: boolean   -- Is this object valid ?
    x_size: number   -- X dimension of this object (preferably in tiles)
    y_size: number   -- Y dimension of this object (preferably in tiles)
}
I would rather have the sizes in tiles instead of pixels, since coordinates in the rendering API are already mostly in tiles. I also assume pixel size may be dependent on "Graphics -> Sprite resolution" settings, so would be player-specific.

2) New method in LuaGameScript to get this object

Code: Select all

LuaGameScript::get_sprite_info(spritePath: string) -> LuaSpriteInfo
Example usecase

Rendering a technology sprite in a 1x1 tile square, occupying as much room as possible while keeping the x/y ratio:

Code: Select all

local sprite_path = "technology/foobar"
local sprite_info = game.get_sprite_info(sprite_path)
local scale = math.min(1 / sprite_info.size_x, 1 / sprite_info.size_y)
local id = rendering.draw_sprite{
    sprite = sprite_path,
    surface = 1,
    position = {0,0},
    x_scale = scale,
    y_scale = scale,
}

Re: Reading dimensions of sprites during control stage.

Posted: Thu Oct 21, 2021 8:56 am
by boskid
Unfortunately this is not going to happen. Sprite informations are not part of the game state and as such cannot be returned to a Lua. If such request would be performed in a multiplayer game, a headless server may not have any of the png files so an instance of the control script running on a headless would not be able to obtain those sprite details, even when every client could get them. This would be simply causing desyncs since a control script on headless not able to obtain those sprite details would most likely cause those LuaRendering requests to not happen or happen with different values than on the clients which already means the game state would desync (LuaRendering is part of the game state).

Re: Reading dimensions of sprites during control stage.

Posted: Thu Oct 21, 2021 9:21 am
by posila
boskid wrote: Thu Oct 21, 2021 8:56 amIf such request would be performed in a multiplayer game, a headless server may not have any of the png files so an instance of the control script running on a headless would not be able to obtain those sprite details, even when every client could get them.
Dimensions are specified in Lua in data stage, so even headless server has them available ... But since there is a graphics option to load sprites in different quality, different clients may use sprites with different dimensions.
Creidhne wrote: Tue Sep 28, 2021 2:24 pm I would rather have the sizes in tiles instead of pixels, since coordinates in the rendering API are already mostly in tiles. I also assume pixel size may be dependent on "Graphics -> Sprite resolution" settings, so would be player-specific.
Unfortunatelly, nothing in the control stage is allowed to be player-specific.