Jump to content




[Solved] How to properly use the "turtle_inventory" event

turtle api

3 replies to this topic

#1 areuz

  • Members
  • 11 posts

Posted 25 August 2019 - 02:06 PM

EDIT: Please move this into the proper category, I have no idea when I clicked on "tutorials" but this is definitely the wrong section! sorry

I'm using the latest CC: Tweaked 1.83.1
To optimize miners, I want to map the entire inventory into a table, and keep track of it during mining without manually looping over all of the slots with 'turtle.getItemDetail([slot])' as I don't know what impact that may have on server performance with 20+ turtles running.

The official Wiki page for this event shows a very basic unrealistic use case of this event, but does tell us, that it has no return values.
The in-game help message is a little bit more useful, suggesting that we use "comparison operations" to see what changes have been made.

Quote

Events fired by the Turtle API: "turtle_inventory" when any of the items in the inventory are changed. Use comparison operations to inspect the changes.

What "comparison operations" does this have in mind? It can't be 'turtle.compareTo(number slot)', or any of the other 'turtle.compare' methods. The same goes for all of the other API methods. If this just goes back "keep the old state of the inventory and on this event, iterate through the whole thing again and compare the slots"... then that's dissapoiting.


Note:
If this can't be done within the current API, could anyone willing and knowledgeable enough do a quick test if a return value (slot number / table slots that changed / etc) can be added to the event?
From a quick glance at the source, it seems plausible enough assuming the 'm_previousInventory' has not changed yet.

The calling function in 'main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java' does have access to the 'm_previousInventory', and 'm_inventory' - I just couldn't find where this function is called from and don't know if the inventories weren't synced by this point.
@Override
	public void update()
	{
		super.update();
		m_brain.update();
		if( !getWorld().isRemote && m_inventoryChanged )
		{
			ServerComputer computer = getServerComputer();
			if( computer != null ) computer.queueEvent( "turtle_inventory" );
			m_inventoryChanged = false;
			for( int n = 0; n < getSizeInventory(); n++ )
			{
				m_previousInventory.set( n, InventoryUtil.copyItem( getStackInSlot( n ) ) );
			}
		}
	}

Edited by areuz, 31 August 2019 - 11:08 PM.


#2 EveryOS

    Resident Necromancer

  • Members
  • 566 posts
  • LocationBOO!!

Posted 26 August 2019 - 06:53 PM

From the wiki, it looks like you could do comparisons using the `getItemDetail` method of `turtle` (and that is the only way).
https://www.computer...e.getItemDetail

As far as server impact, it should be near-0 for any server not running Windows 95 (:

Trust me, the code executes uber-fast

Edited by EveryOS, 26 August 2019 - 06:58 PM.


#3 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,451 posts

Posted 27 August 2019 - 10:28 PM

Moved to Ask a Pro.

#4 areuz

  • Members
  • 11 posts

Posted 31 August 2019 - 11:05 PM

View PostEveryOS, on 26 August 2019 - 06:53 PM, said:

From the wiki, it looks like you could do comparisons using the `getItemDetail` method of `turtle` (and that is the only way).
https://www.computer...e.getItemDetail

As far as server impact, it should be near-0 for any server not running Windows 95 (:

Trust me, the code executes uber-fast

Yep, thought about that, and might do some testing. Though the event still looks very unfinished without any return values. It's as if Windows didn't send info about a file change, but sent out a "Yeah something on the disk changed, figure it out".

Anyway, thanks for the confirmation!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users