[0.18.17] Observation of a player

Bugs that are actually features.
Post Reply
XaLpHa89
Fast Inserter
Fast Inserter
Posts: 119
Joined: Wed Jan 24, 2018 6:59 am
Contact:

[0.18.17] Observation of a player

Post by XaLpHa89 »

I would like to enable players who just want to watch, to use a camera to watch a player who is constantly following the other player. See the screenshot. In the screenshot I noticed two details. First, although the time of day is night, the camera is brightly lit. Second, the position values from the function are very accurate, only the picture jerky when I move.

Code: Select all

function draw_frame_camera( player )
local focus_of_the_camera = player.position
if player.gui.center[ name_frame_camera ] then player.gui.center[ name_frame_camera ].destroy() end
local element_frame = player.gui.center.add( { type = 'frame', name = name_frame_camera, direction = 'vertical' } )
element_frame.style.padding = 0
local element_camera = element_frame.add( { type = 'camera', name = 'name_camera', position = focus_of_the_camera, zoom = 0.3 } )
element_camera.style.width = 720
element_camera.style.height = 480
local element_flow = element_frame.add( { type = 'flow' } )
element_flow.style.horizontal_align = 'right'
element_flow.style.horizontally_stretchable = true
element_flow.add( { type = 'button', name = name_button_camera_close, style = 'rounded_button', caption = 'CLOSE' } )
end
local function on_player_changed_position( event )
if global.game_stage ~= 'ongoing_game' then return end
local player = game.players[ event.player_index ]
if player.gui.center[ name_frame_camera ] then
player.gui.center[ name_frame_camera ].children[ 1 ].position = player.position
end
end
20200408044045_1.jpg
20200408044045_1.jpg (362.1 KiB) Viewed 1700 times

posila
Factorio Staff
Factorio Staff
Posts: 5201
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

Re: [0.18.17] Observation of a player

Post by posila »

If we still had Known issues section I'd put it there. Camera widget just takes sprites the game renderer would render and draws them plainly onto a GUI surface. It doesn't have ability to effects passes, so most visibly lights and night are not properly rendered.

I don't know whats up with the jerky movement, it could be just frame drops due to extra load camera widget adds to the GUI rendering.

XaLpHa89
Fast Inserter
Fast Inserter
Posts: 119
Joined: Wed Jan 24, 2018 6:59 am
Contact:

Re: [0.18.17] Observation of a player

Post by XaLpHa89 »

posila wrote:
Wed Apr 08, 2020 5:37 am
If we still had Known issues section I'd put it there. Camera widget just takes sprites the game renderer would render and draws them plainly onto a GUI surface. It doesn't have ability to effects passes, so most visibly lights and night are not properly rendered.

I don't know whats up with the jerky movement, it could be just frame drops due to extra load camera widget adds to the GUI rendering.
I can still accept that the night will not be rendered. And the camera image also runs smoothly, only the camera tracking, seems as if the position cannot have decimal places. For example, the position jumps from {x=0,y=0} to {x=0,y=1}.

posila
Factorio Staff
Factorio Staff
Posts: 5201
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

Re: [0.18.17] Observation of a player

Post by posila »

So I made the code work for me and saw the problem.

on_player_changed_position even is fired only when player moves from one tile to another, so you can't use that even to update the camera position.

Fixed up code if anyone needs to return to this

Code: Select all

/c  name_frame_camera = "name_frame_camera"

function draw_frame_camera( player )
  local focus_of_the_camera = player.position
  if player.gui.center[ name_frame_camera ] then player.gui.center[ name_frame_camera ].destroy() end
  local element_frame = player.gui.center.add( { type = 'frame', name = name_frame_camera, direction = 'vertical' } )
  element_frame.style.padding = 0
  local element_camera = element_frame.add( { type = 'camera', name = 'name_camera', position = focus_of_the_camera, zoom = 0.3 } )
  element_camera.style.width = 720
  element_camera.style.height = 480
  local element_flow = element_frame.add( { type = 'flow' } )
  element_flow.style.horizontal_align = 'right'
  element_flow.style.horizontally_stretchable = true
  element_flow.add( { type = 'button', name = name_button_camera_close, style = 'rounded_button', caption = 'CLOSE' } )
end
function on_player_changed_position( event )
  local player = game.players[ event.player_index ]
  if player.gui.center[ name_frame_camera ] then
    player.gui.center[ name_frame_camera ].children[ 1 ].position = player.position
  end
end

Code: Select all

/c script.on_event(defines.events.on_player_changed_position, on_player_changed_position)
draw_frame_camera(game.players[1])

XaLpHa89
Fast Inserter
Fast Inserter
Posts: 119
Joined: Wed Jan 24, 2018 6:59 am
Contact:

Re: [0.18.17] Observation of a player

Post by XaLpHa89 »

posila wrote:
Wed Apr 08, 2020 10:15 am
on_player_changed_position even is fired only when player moves from one tile to another
Well, it would have been too nice if it was easy. Then I will build a camera tracking system that will only move the camera when the player threatens to leave the window.

posila
Factorio Staff
Factorio Staff
Posts: 5201
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

Re: [0.18.17] Observation of a player

Post by posila »

You can update it in on_tick

XaLpHa89
Fast Inserter
Fast Inserter
Posts: 119
Joined: Wed Jan 24, 2018 6:59 am
Contact:

Re: [0.18.17] Observation of a player

Post by XaLpHa89 »

posila wrote:
Wed Apr 08, 2020 12:52 pm
You can update it in on_tick
You can mark it as [SOLVED], the function works very well.

Code: Select all

local function on_tick( event )
if global.game_stage ~= 'ongoing_game' then return end
for supervisor_index, player_index in pairs( table_of_observations ) do
local supervisor = game.players[ supervisor_index ]
local player = game.players[ player_index ]
if supervisor.gui.center[ name_camera_frame ] then supervisor.gui.center[ name_camera_frame ].children[ 1 ].position = player.position end
end
end
event.add( defines.events.on_tick, on_tick )

Post Reply

Return to “Not a bug”