Page 1 of 1
[0.18.17] Observation of a player
Posted: Wed Apr 08, 2020 3:13 am
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 (362.1 KiB) Viewed 2130 times
Re: [0.18.17] Observation of a player
Posted: Wed Apr 08, 2020 5:37 am
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.
Re: [0.18.17] Observation of a player
Posted: Wed Apr 08, 2020 9:39 am
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}.
Re: [0.18.17] Observation of a player
Posted: Wed Apr 08, 2020 10:15 am
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])
Re: [0.18.17] Observation of a player
Posted: Wed Apr 08, 2020 11:33 am
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.
Re: [0.18.17] Observation of a player
Posted: Wed Apr 08, 2020 12:52 pm
by posila
You can update it in on_tick
Re: [0.18.17] Observation of a player
Posted: Fri Apr 17, 2020 8:25 am
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 )