• Print

Author Topic: Passing a table with a value as a table into fancyLog?  (Read 4787 times)

0 Members and 1 Guest are viewing this topic.

Offline VoodooNecro

  • Newbie
  • *
  • Posts: 3
  • Karma: -1
Passing a table with a value as a table into fancyLog?
« on: October 08, 2015, 02:17:43 pm »
Greetings,

I've recently been working on a command where I need to print a table out silently. However after quite a bit of attempts I can't seem to get it to work (Perhaps fancyLog isn't capable of accepting a table with a value as a table as such?). I couldn't find any documention on the function and had to guess by looking at the code declared in log.lua. Which I admit, I probably did pretty bad at properly reading.  :P

Code: [Select]
-- standard ulx command stuff
local tb =  { }
for _ , v in pairs( player.GetHumans( ) ) do
     table.insert ( tb , {  ["Fruit"] = "Apple" , ["Amount"] = 0 } )
end

ulx.fancyLog(calling_ply, true ,"Fruits:  #s[#i]", tb.Apple, tb.Amount )

--ending ulx command stuff


An output would look something like:

Code: [Select]
>Fruits: Apple[1], Banana[3], Corn[0]


And my lastest attempt with code similar to the example resulted in an error:

Quote

[ERROR] addons/ulx/lua/ulx/log.lua:440: attempt to call method 'gsub' (a nil value)
  1. fancyLogAdmin - addons/ulx/lua/ulx/log.lua:440
   2. fancyLog - addons/ulx/lua/ulx/log.lua:504



Any help would greatly be appreciated. Thanks for your time!

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Passing a table with a value as a table into fancyLog?
« Reply #1 on: October 10, 2015, 12:10:10 am »
Your first problem is that tb.Apple and tb.Amout are both nil.

If you look at the structure of your table, you would see why this is the case. Your for loop goes through each 'Human' and inserts a table in tb. table.insert inserts the value into the table at the last position in the table, that is to say one space past the last used space in the table. This table contains two values, "Apple" and 0 which are stored at the "Fruit" and "Amount" keys.

Here is a visual representation of your table structure:

Code: Lua
  1. tb
  2.   1
  3.     "Fruit"  : "Apple"
  4.     "Amount" : 0
  5.   2
  6.     "Fruit"  : "Apple"
  7.     "Amount" : 0
  8.   3
  9.     "Fruit"  : "Apple"
  10.     "Amount" : 0
  11.   4
  12.     "Fruit"  : "Apple"
  13.     "Amount" : 0
  etc...

As you can see, tb.Apple and tb.Amount do not exist.

I do not fully understand what output you want, as you don't specify where the values of the fruit are coming from in the example (are they from the calling player?). However, a first step would be to structure your table so you can actually refer to the values. Since table.insert simply assigns each player a number in no meaningful order, you might want to use the player as the key for their fruits table. Then you could do tb.<ply>.Fruit and tb.<ply>.Amount.

Another solution is to use the player's tables themselves. The player variable is a table, so you could do <ply>.Fruit or <ply>.Amount instead.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline VoodooNecro

  • Newbie
  • *
  • Posts: 3
  • Karma: -1
Re: Passing a table with a value as a table into fancyLog?
« Reply #2 on: October 10, 2015, 07:48:46 am »
Quote
I do not fully understand what output you want, as you don't specify where the values of the fruit are coming from in the example (are they from the calling player?). However, a first step would be to structure your table so you can actually refer to the values. Since table.insert simply assigns each player a number in no meaningful order, you might want to use the player as the key for their fruits table. Then you could do tb.<ply>.Fruit and tb.<ply>.Amount.

I didn't explain it too well so that is my fault.

The values come from declaring in the table.insert when looping through each player.

Code: [Select]
table.insert ( tb , {  ["Fruit"] = ply:Nick() , ["Amount"] = ply:GetPData("Num") } )

I was hoping to simplify by including what would be, a <string> for "Fruit" and a <int> for "Amount" so I apologize for that.

Quote
you might want to use the player as the key for their fruits table.

That is a good point, I'll try that first instead of passing a whole table value into it. Thanks!


Edit:

So I've managed to fix everything except the fancyLog accepting the table and printing out.

I made the table add a value as so:

Code: [Select]
tb[v:Nick()] = getNumberStuff or 0

Printing it out manually

Code: [Select]
for k,v in pairs(tb) do
     print  ( v .. " - " .. k )
end

Would print as

Quote
Voodoo - 1

And trying to make that, into

Code: [Select]
ulx.fancyLog(calling_ply, true ,"Output: #s[#i]", tbl[1], [2] )
« Last Edit: October 10, 2015, 08:23:43 am by VoodooNecro »

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Passing a table with a value as a table into fancyLog?
« Reply #3 on: October 10, 2015, 09:09:49 am »
Can you give me the entire code?

From the code snippets you've given me, you're referencing table values that don't exist. Your table structure is now this:

Code: Lua
  1. tb
  2.   Nick1 : 4
  3.   Nick2 : 2
  4.   Nick3 : 8
  5.   etc...

First of all, you're referencing tbl which doesn't exist. Your table is called tb. Secondly, you're referencing [2]. I'm not sure how that would be interpreted, but I assume you're trying to get a table value.

If you want to just print the data with fancyLog, then loop through the table tb and use:
Code: Lua
  1. ulx.fancyLog(calling_ply, true ,"#s - #i", k, v )
« Last Edit: October 10, 2015, 09:15:05 am by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline VoodooNecro

  • Newbie
  • *
  • Posts: 3
  • Karma: -1
Re: Passing a table with a value as a table into fancyLog?
« Reply #4 on: October 10, 2015, 09:19:39 am »
Can you give me the entire code?

From the code snippets you've given me, you're referencing table values that don't exist. Your table structure is now this:

Code: Lua
  1. tb
  2.   Nick1 : 4
  3.   Nick2 : 2
  4.   Nick3 : 8
  5.   etc...

First of all, you're referencing tbl which doesn't exist. Your table is called tb. Secondly, you're referencing [2]. I'm not sure how that would be interpreted, but I assume you're trying to get a table value.

If you want to just print the data with fancyLog, then loop through the table tb and use:
Code: Lua
  1. ulx.fancyLog(calling_ply, true ,"#s - #i", v, k )


Condensed it but here is what I have

Code: [Select]
function ulx.myCommand(calling_ply)
local plytbl = {}
for _,v in pairs(player.GetHumans()) do
local amt = v.PlayerVariable --this exists just under a different name

if amt > 0 then
                        --For plytbl, need to set "amt" for the player name, not necessarily the player table, just need their name
plytbl[v:Nick()] = amt

end
end

        --Printing normally is okay, but passing into fancyLog as plytbl[1] and plytbl[2] results in the error mentioned earlier
ulx.fancyLog(calling_ply, true ,"Amt: #s[#i]", plytbl[1], plytbl[2] )
end

Thanks for all the help so far.

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Passing a table with a value as a table into fancyLog?
« Reply #5 on: October 12, 2015, 08:56:44 pm »
You'd have to do the following:
Code: Lua
  1. ulx.fancyLog( { calling_ply }, "#s - #i", v, k )

That will send the formatted message to all players in the table (first parameter) and add it to the log file.

  • Print