How to get the GUI element "camera" working

Place to get help with not working mods / modding interface.
Post Reply
Subject314159
Burner Inserter
Burner Inserter
Posts: 6
Joined: Thu Oct 12, 2023 8:53 pm
Contact:

How to get the GUI element "camera" working

Post by Subject314159 »

Hi there,

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)
This creates a very basic frame and updates every tick to follow the player.

The frame being rendered looks like this, where I expect the camera element to be placed between the two lines:
Image
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?

Subject314159
Burner Inserter
Burner Inserter
Posts: 6
Joined: Thu Oct 12, 2023 8:53 pm
Contact:

Re: How to get the GUI element "camera" working

Post by Subject314159 »

Alright so I solved it. Apparently this element needs to be styled properly and doesn't work out of the box. Here's what I did;

Data.lua

Code: Select all

local styles = data.raw["gui-style"].default

styles["my_camera_style"] = {
    type = "camera_style",
    horizontally_stretchable = "on",
    vertically_stretchable = "on"
}
I think it either has to be set to stretchable on both sides or a size has to be specified in the style.

Control.lua

Code: Select all

...
    local cam = main_frame.add {
        type = "camera",
        name = "cam",
        position = {10, 10}
    }
    style = "my_camera_style"
    ...

Bilka
Factorio Staff
Factorio Staff
Posts: 3159
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: How to get the GUI element "camera" working

Post by Bilka »

Just a heads up: You don't need to set the position of the camera every tick, instead you can just point it at the player character with https://lua-api.factorio.com/latest/cla ... tml#entity.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Post Reply

Return to “Modding help”