I'm having trouble to get a "camera" GUI element on my frame. I have tried literally everything up until the point that I disabled all my mods and created a new test mod with only the mandatory info.json and following control.lua:
Code: Select all
script.on_init(function()
--Create a global variable to store our gui
global.gui = {}
end)
script.on_event(defines.events.on_player_created, function(event)
-- Get some variables to work with
local player = game.get_player(event.player_index)
local screen_element = player.gui.screen
-- Create the main frame
local main_frame = screen_element.add {
type = "frame",
name = "main_frame",
caption = {"hello_world"},
direction = "vertical"
}
main_frame.style.size = {385, 385}
main_frame.auto_center = true
-- Add individual elements
local ticker = main_frame.add {
type = "textfield",
name = "ticker",
text = "Hello, world!",
enabled = false
}
local line_before = main_frame.add {
type = "line"
}
local cam = main_frame.add {
type = "camera", --This is the element that is giving all the trouble
name = "cam",
position = {10, 10}
}
local line_after = main_frame.add {
type = "line"
}
local map = main_frame.add {
type = "minimap",
name = "map",
position = {0, 0}
}
-- Store the frame in global so that we can update it later
global.gui = main_frame
end)
script.on_event(defines.events.on_tick, function(event)
-- Get some variables to work with
local gui = global.gui
local player = game.players[1]
local x, y = player.position.x, player.position.y
-- Update the camera to the player position
if gui.cam then
gui.cam.position = {x, y}
else
game.print("Camera object not found")
end
-- Update the minimap to the player position
if gui.map then
gui.map.position = {x, y}
else
game.print("Minimap object not found")
end
-- Update the ticket textfield with the current ticker
if gui.ticker then
gui.ticker.text = "Tick: " .. event.tick
end
end)
The frame being rendered looks like this, where I expect the camera element to be placed between the two lines:
As you can see the tick counter and the minimap updates just fine, and also the "Camera object not found" message (as per the else condition if the GUI camera child would be nil) isn't thrown. So I know quite sure that above code works as expected, except for the camera element of course.
According to the API docs this is how it should be implemented. Also if I look at other mods (for example how Ultracube implemented the camera) similar methods are used and I don't see why above code shouldn't work.
What am I doing wrong here?