player.cursor_stack ~= nil always true?

Place to get help with not working mods / modding interface.
User avatar
oLaudix
Filter Inserter
Filter Inserter
Posts: 285
Joined: Sun Jun 14, 2015 3:24 pm
Contact:

player.cursor_stack ~= nil always true?

Post by oLaudix »

Code: Select all

if player.cursor_stack ~= nil then
	game.player.print(player.cursor_stack.name)
else
	return
end
As long as i have item in my "hand" it works. If I change item for an item it works fine as well. But when i put item in empty slot and my hand is empty it throws exception at "game.player.print(player.cursor_stack.name)". But from what i understand it shouldn't even get to that part of code if my hand is empty?
Image
Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: player.cursor_stack ~= nil always true?

Post by Choumiko »

0.12 changes:
Changed LuaEntity::stack, LuaEntity::held_stack and LuaPlayer::cursor_stack to return LuaItemStack objects.
Changed LuaItemStack to allow reading any inventory slot even when the item in the slot is invalid. LuaItemStack::valid_for_read should be used before accessing the normal properties/methods for a given LuaItemStack.
if player.cursor_stack.valid_for_read then ...
should be the new way to do it
User avatar
oLaudix
Filter Inserter
Filter Inserter
Posts: 285
Joined: Sun Jun 14, 2015 3:24 pm
Contact:

Re: player.cursor_stack ~= nil always true?

Post by oLaudix »

It works great now. Thank you.
Image
Post Reply

Return to “Modding help”