Solved - player.is_cursor_blueprint always returns true?

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

Solved - player.is_cursor_blueprint always returns true?

Post by Subject314159 »

So I'm probably the gazillionth person trying to read blueprints from libraries and dove into the API docs to find player.is_cursor_blueprint and player.cursor_stack.is_blueprint, which should return a true/false whether the player is holding a blueprint item.

Now I tried and tested a million variants of the following piece of code:

Code: Select all

script.on_event(defines.events.on_player_cursor_stack_changed, function(event)
    local player = game.get_player(event.player_index)

    player.print("---")
    if player.cursor_stack.valid then
        player.print("Stack is valid")
    else
        player.print("Stack is NOT valid")
    end

    if player.cursor_stack.valid_for_read then
        player.print("Stack is valid for read")
    else
        player.print("Stack is NOT valid for read")
    end

    if player.cursor_stack.is_blueprint then
        player.print("Stack is a blueprint")
    else
        player.print("Stack is NOT a blueprint")
    end
    if player.is_cursor_blueprint then
        player.print("Cursor is a blueprint")
    else
        player.print("Cursor is NOT a blueprint")
    end
end)
For some reason the output is always "Cursor is a blueprint", no matter which item I select. Now on_player_cursor_stack_changed always triggers correctly, even when picking up/putting back blueprints from the library. Next is_blueprint only returns true when picking a blueprint item from the inventory and not from the library.

Is this a known issue? And how can I read a blueprint item from the library?
Last edited by Subject314159 on Fri Oct 13, 2023 4:25 pm, edited 2 times in total.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: player.is_cursor_blueprint always returns true?

Post by Pi-C »

Subject314159 wrote:
Thu Oct 12, 2023 9:20 pm
For some reason the output is always "Cursor is a blueprint", no matter which item I select.
See the description of LuaControl::is_cursor_blueprint:
Note that both this method and LuaControl::get_blueprint_entities refer to the currently selected blueprint, meaning a blueprint book with a selected blueprint will return the information as well.
Could that be the reason?
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Subject314159
Manual Inserter
Manual Inserter
Posts: 4
Joined: Thu Oct 12, 2023 8:53 pm
Contact:

Re: player.is_cursor_blueprint always returns true?

Post by Subject314159 »

With 'no matter what item I select' I mean any item, being it a dumb stick to a rocket silo, selection tool, cusom mod items, you name it.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: player.is_cursor_blueprint always returns true?

Post by Pi-C »

Subject314159 wrote:
Thu Oct 12, 2023 9:20 pm
Now I tried and tested a million variants of the following piece of code:

Code: Select all

    if player.is_cursor_blueprint then
For some reason the output is always "Cursor is a blueprint", no matter which item I select.
Your condition is wrong! See the definition of LuaControl::is_cursor_blueprint:

Code: Select all

is_cursor_blueprint()  → boolean 
Notice something? Otherwise try this:

Code: Select all

    if player.is_cursor_blueprint then
       game.print(type(player.is_cursor_blueprint))
    end
This will output "function". The method 'is_player_blueprint' is always there, so the test condition will always be true. What you're interested in is not the function but its return value, so you must call the function, not read it!

Code: Select all

    if player.is_cursor_blueprint() then
will do what you expect.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Subject314159
Manual Inserter
Manual Inserter
Posts: 4
Joined: Thu Oct 12, 2023 8:53 pm
Contact:

Re: player.is_cursor_blueprint always returns true?

Post by Subject314159 »

I feel so stupid now..
Thanks for this insight!

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: player.is_cursor_blueprint always returns true?

Post by Pi-C »

Subject314159 wrote:
Fri Oct 13, 2023 4:23 pm
I feel so stupid now..
No need, I've fallen into the same trap often more than once myself. :-D
Thanks for this insight!
Glad I could help you out!
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Post Reply

Return to “Modding help”