Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Crafter38

What table is it in Tank_Contorller Upgrade?

Question

First of all, thank you, Sangar and other developers, for updating this incredible mod! I love it!

So I tried to print values of table (just in educational purpose) which gives the method 

 component.tank_controller.getFluidInTank(4)

4 is the left side of the Adapter where I put Tank Controller Upgrade. As I understand, this method contains so called "nested table". It gives us this:

{{amount=1000, capacity=4000, hasTag=false, label="Water", name="water"}, {amoun=0, capacity=4000}}

Ok, I tried to print nested table in ordinary Lua interpreter. This code works fine and prints 222:

su=222
su2 = 333
th=452
th2 = 349

sec = {su, su2}
third = {th, th2}
first = {sec, third}

print(first[1][1])

But when I tried the same way  in the game I have got nil:

co=component.tank_controller.getFluidInTank(4)
print(co[1][1])

By hard googling I finally found that if we print

co=component.tank_controller.getFluidInTank(4)
amount = co[1].amount

then it's possible to get that value and print it (thanks Harkole!). But when I tried that way of getting value of nested table in Lua interpreter, I got nil:

su=222
su2 = 333
th=452
th2 = 349

sec = {su, su2}
third = {th, th2}
first = {sec, third}

print(first[1].su)

As I understand getFluidInTank() gives some special type of nested table, which is impossible to print like "normal" nested tables? This is complete riddle and mystery for me, please enlighten me!

Edited by Crafter38
*uPdating
Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

su is set to 222 and su2 is set to 333

When you create the table "sec", you are initializing it to {222,333}, not {["su"] = 222, ["su2"] = 333}

So in this case, the table first is initialized to {{222,333},{452,349}}, which is why print(first[1][1]) works.

 

The tank controller is returning dictionaries, which indexing with a number does not work with. That's why print(co[1][1]) won't work, but print(co[1]["amount"]) and print(co[1]["amount"]) will.

Link to post
Share on other sites
  • 0
6 hours ago, cadergator10 said:

{["su"] = 222, ["su2"] = 333}

Thank you so much! I didn't know about this notation. Now works perfectly. Let's assume that I know nothing about the content of the tank_controller component. So how do I get all the parameters of it with

for 1, 5 do
	print()
end

 cycle for example? How do I put strings (e.g. "amount") into indexes of this cycle?

Link to post
Share on other sites
  • 0
16 hours ago, Crafter38 said:

Thank you so much! I didn't know about this notation. Now works perfectly. Let's assume that I know nothing about the content of the tank_controller component. So how do I get all the parameters of it with


for 1, 5 do
	print()
end

 cycle for example? How do I put strings (e.g. "amount") into indexes of this cycle?

I think the only way to do that is a foreach loop. If that loop is named myTable, then

for key, value in pairs(myTable) do
	print(value)
	print(myTable[key])
end

both of these print the same thing for each element in the table

Link to post
Share on other sites
  • 0
1 hour ago, cadergator10 said:

I think the only way to do that is a foreach loop. If that loop is named myTable, then



for key, value in pairs(myTable) do
	print(value)
	print(myTable[key])
end

both of these print the same thing for each element in the table

I tried your code in the game and that's I have got:

2.jpg

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.