Page 1 of 1

Solved - player.is_cursor_blueprint always returns true?

Posted: Thu Oct 12, 2023 9:20 pm
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?

Re: player.is_cursor_blueprint always returns true?

Posted: Thu Oct 12, 2023 10:23 pm
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?

Re: player.is_cursor_blueprint always returns true?

Posted: Thu Oct 12, 2023 11:31 pm
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.

Re: player.is_cursor_blueprint always returns true?

Posted: Thu Oct 12, 2023 11:55 pm
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.

Re: player.is_cursor_blueprint always returns true?

Posted: Fri Oct 13, 2023 4:23 pm
by Subject314159
I feel so stupid now..
Thanks for this insight!

Re: player.is_cursor_blueprint always returns true?

Posted: Fri Oct 13, 2023 5:08 pm
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!