Jump to content

LUA getItemsList() and how it works ?


Recommended Posts

Hi all,

 

i am trying to find out how the function getItemsList() is working.

 

I test a container M linked with a programming board and a screen.

 

My code is something like this:

 

monitor.activate();
monitor.clear();

 

cont = {}
    cont["Content:"] = cont1.getItemsList(); --cont1 is the container object name
    
content_encode = json.encode(cont);

 

system.print(content_decode);
monitor.addText(5,5,5,content_encode);

 

When running this script i get: "Content:""" but the container is not empty.

I also tested with getItemsList(itemName) f.ex. and others. Same result.

 

When using functions like 

    cont["Free Mass:"] = cont1.getMaxVolume() - cont1.getItemsVolume()

which is working as it should.

 

The script is running in "UNIT" with a "Start" -Filter

 

Is the function not working ?

Link to post
Share on other sites

Hello HomerGAdams!

There are actually 3 steps to the process of retrieving and processing the container information


The container content is not loaded by default, first you will need to request the content through:

container.acquireStorage()

This is limited to 10 calls every 5 minutes, if you go over that it will trigger a cooldown.

 

This call is not immediate and takes a (short) time to arrive.

Once that data arrives and is available to use an event is triggered on the container slot called 

storageAcquired()

You can simply set a filter on the container unit and it will be triggered everytime container.acquireStorage() has successfully returned results.

 

Inside storageAcquired() you will now likely want to use

jsonString = container.getItemsList()

or alternatively deconstruct the JSON data directly:

itemList = json.decode(container.getItemsList())

 

The item list will then be a numbered table containing the item slots in the container, and each entry will have the follow fields available to it:
name, quantity, unitVolume, unitMass, type, class

 

If you want a practical example to look at you can copy the following and use "Paste Lua Configuration from Clipboard" onto a programming board.

{"slots":{"0":{"name":"container","type":{"events":[],"methods":[]}},"1":{"name":"screen","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}},"-2":{"name":"system","type":{"events":[],"methods":[]}},"-3":{"name":"library","type":{"events":[],"methods":[]}}},"handlers":[{"code":"-- Event was fired so we now have the container content.\nitems = json.decode(container.getItemsList())\n\n-- Lets make an HTML table to display all the data.\nlocal html = [[\n<div class=\"bootstrap\" width=\"1vw\" height=\"1vh\">\n\t<table>\n\t\t<tr>\n\t\t\t<th>name</th>\n\t\t\t<th>quantity</th>\n\t\t\t<th>unitVolume</th>\n\t\t\t<th>unitMass</th>\n\t\t\t<th>type</th>\n\t\t\t<th>class</th>\n\t\t</tr>\n]] \n\n-- iterate over the container data and add the items to the table\nfor entry,itemData in ipairs(items) do\n    html = html..[[\n\t\t<tr>\n\t\t\t<td>]]..itemData[\"name\"]..[[</td>\n\t\t\t<td>]]..itemData[\"quantity\"]..[[</td>\n\t\t\t<td>]]..itemData[\"unitVolume\"]..[[</td>\n\t\t\t<td>]]..itemData[\"unitMass\"]..[[</td>\n\t\t\t<td>]]..itemData[\"type\"]..[[</td>\n\t\t\t<td>]]..itemData[\"class\"]..[[</td>\n\t\t</tr>\n    ]]\nend\n\n-- End the table\nhtml = html..[[\n\t</table>\n</div>\n]]\n\n-- Write to the screen\nscreen.setHTML(html)","filter":{"args":[],"signature":"storageAcquired()","slotKey":"0"},"key":"0"},{"code":"-- When the board starts up let the player know the data is being retrieved (This could be quick or slow)\nscreen.setHTML([[<div class=\"bootstrap\" style=\"font-size:5vw; \">Retrieving container data</div>]])\n\n-- Lets prepare an empty table for the data to go into\n-- This prevents other functions from receiving null until the data is retrieved.\nitems = {} \n\n-- Start a request for the container data \n-- this will trigger an event in the container slot when completed\n-- We can only do this 10 times per 5 minutes, or a cooldown will be triggered.\ncontainer.acquireStorage()","filter":{"args":[],"signature":"start()","slotKey":"-1"},"key":"1"}],"methods":[],"events":[]}

 

 

I hope this helps answer your question, if not please do let me know :) 

 

NQ-Deckard

Link to post
Share on other sites

Good morning Deckard.

 

Yes i already found out, after reading 100 times getItemsList(), trying to find out how to json.decode and finaly found the acquired function ^^

Now i am geting information from the container and i guess i need to format it with html ?

I have no idea of html, i stoped programming 20 years ago with C, when DOS was my OS ;)

But this all is no witchwork, if you know the functions you can use. Just html i need to learn now.

 

image.thumb.png.d8509fb29bfd83dc85ceb3c3bcb926a4.png

 

The code i used was nearly the same as you wrote:

 

image.thumb.png.71b9f20de53748b963d967633e68e0b8.png

image.thumb.png.04bb225159a0d33fbf68b326e2acb0ca.png

 

For the first run i get a null (nil) returned, the second (after 5 seconds timer) i get the informations.

What about the json.encode function ?

 

I will check out the test code soon, hopefully to clearly understand how it works.

Is there a LUA Tutorial for DU functions, events and stuff like that ?

 

Btw. the description of getItemsList() shows itemName, instead of name. Maybe you wrote wrong, or the description is wrong.

I guess i am able to point directly to one of these fields ?

 

Cheers for your help Deckard

Homer

Link to post
Share on other sites

Ah! That's for pointing that out.

 

You can find a number of player made tutorials for Lua and such on the tutorials section of the website: https://www.dualuniverse.game/tutorials/lua-guide-to-doors

 

As for json.encode() / json.decode(), they simply turn data objects in Lua into a text string and back.

 

For example:
 

-- This is a simple table in lua
luaTable = {
  itemOne = 123,
  itemTwo = 456,
  itemThree = 789
}

-- this converts that table into a text string
jsonTable = json.encode(luaTable)

-- this will print that text to the console
system.print(jsonTable) 

-- Console output should be something like: {"itemOne": 123, "itemTwo": 456, "itemThree": 789}

-- this will turn that text back into a useable table
secondLuaTable = json.encode(jsonTable)

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...