0

I am using JSON:encode to prepare a lua table for udp transmition. On the other end I am using JSON.parse to to decode and then define the 3 variables. On the transmition side I verify that the table is created and has all three variables by printing botMsg[0], botMsg[1] and botMsg[3] to the logs before running JSON:encode(botMsg). On the receiving end I run obj = JSON.parse(msg) and then define the 3 variables for use in the code. What I end up with is the first variable obj[0] is botMsg[1] and obj[1] is botMsg[2] and obj[2] is undefined.

Looking for ideas why this is happening?

Transmition side LUA code:

PandaBoss.sendBossMessage = function(msg)


    botMsg = {}
    botMsg[1] = msg
    botMsg[2] = guild_ID
    botMsg[3] = channel_ID

    env.info(botMsg[1]) --prints msg to data log
    env.info(botMsg[2]) --prints guild_id to data log
    env.info(botMsg[3]) --prints channel_id to data log

    json_string = JSON:encode(botMsg)

    socket.try(PandaBoss.UDPSendSocket:sendto(json_string, ip, 31090))

end

Receiving node.js code:

server.on('message', function(msg) {
obj = JSON.parse(msg)
bossMsg = obj[0]
guild_ID = obj[1]
channel_ID = obj[2]
console.log(bossMsg); //prints guild_id
console.log(guild_ID); //prints channel_id
console.log(channel_ID); //prints undefined

});

New Change to receiver

server.on('message', function(msg) {

console.log(msg.toString());
obj = JSON.parse(msg)
bossMsg = obj[0]
guild_ID = obj[1]
channel_ID = obj[2]
console.log(bossMsg);
console.log(guild_ID);
console.log(channel_ID);

});
XenPanda
  • 137
  • 1
  • 9
  • 2
    Does this answer your question? [Why do Lua arrays(tables) start at 1 instead of 0?](https://stackoverflow.com/questions/2785704/why-do-lua-arraystables-start-at-1-instead-of-0) – Joseph Sible-Reinstate Monica May 03 '20 at 19:37
  • Thanks for the info. I assumed because I created the table and then printed the value of botMsg[0] prior to encoding the table that was created using [0] [1] and [2]. I am not sure if I need to define the LUA table as botMsg[1][2][3] or just leave as defined and then retrieve obj[1][2][3]...I will try both. – XenPanda May 03 '20 at 22:40
  • On the Lua side, you'll use 1, 2, and 3. The JSON won't have index numbers in it. On the Node side, you'll use 0, 1, and 2. – Joseph Sible-Reinstate Monica May 03 '20 at 22:40
  • I changed the code as reflected in the above edited code. Same problem. I also tried changing the obj to obj[1], obj[2], and obj[3]... Same problem. The bossMsg has the guild_ID data and the guild_ID has the channel_ID data and the channel_ID ends up undefined. – XenPanda May 04 '20 at 02:18
  • Take a look at what the JSON string is after Lua encodes it but before Node decodes it. Add it to your question too. – Joseph Sible-Reinstate Monica May 04 '20 at 02:20
  • Should I just be able to `console.log(msg);` to see its contents? I didn't think it would be readable. – XenPanda May 04 '20 at 02:34
  • Yes, why wouldn't it be readable? – Joseph Sible-Reinstate Monica May 04 '20 at 02:34
  • I think I did the same thing originally on the LUA side and it output hex code or bytes or something that I couldn't read. Still learning what encode and decode and all these functions are doing. – XenPanda May 04 '20 at 02:36
  • I'll give it a shot. Thanks for the help. This function is called from a running program and it takes a certain event to get called, so it takes a while to test each iteration. – XenPanda May 04 '20 at 02:38
  • 1
    Hmm, Node thinks it's binary for some reason, but it's actually just ASCII. Try `console.log(msg.toString());` instead. – Joseph Sible-Reinstate Monica May 04 '20 at 02:54
  • In the meantime, does `CSG-3 | Panda | 312, FA-18C_hornet, (CUT), 0 pts` look familiar? Is that the boss message or guild ID? If it's the guild ID, are you sure that your change to the Lua code actually took effect? – Joseph Sible-Reinstate Monica May 04 '20 at 02:55
  • That is bossMsg – XenPanda May 04 '20 at 02:59
  • Okay, that's really weird. Are you sure your NodeJS code is what you think it is? (And it's still worth redoing the test with the `.toString()`.) – Joseph Sible-Reinstate Monica May 04 '20 at 03:00
  • Ok I added a block to the original question showing what I have changed the code to. Here is the result of the console.log outputs: ``` 63**************42 69**************04 undefined – XenPanda May 04 '20 at 03:39
  • 1
    Uhh, even with the `toString` that's what you get? And your receiver change was wrong. Lua should start at 1, but JavaScript should start at 0. – Joseph Sible-Reinstate Monica May 04 '20 at 03:40
  • Yes, even with the .tostring(). What do you mean it was wrong? BTW I asterisked out some of the data because I don't know what can be read from it and I don't need real ID#'s on the web. – XenPanda May 04 '20 at 03:42
  • oh. yeah, I just changed that cause it wasn't working starting at 0 so I thought I would try it at 1. – XenPanda May 04 '20 at 03:43
  • 1
    How about `console.log(obj)`? Is it any more informative? – Joseph Sible-Reinstate Monica May 04 '20 at 03:46
  • ok here is the line from the LUA edited a bit...it appears to have all the data as expected. `: ["CSG-3 | Panda | 312, FA-18C_hornet, ((OK)), 3 pts, HLULX HLULIM H(LUL)IC H(LUL)AR, 1-wire, 14.1 seconds in the groove, (CASE I)","636************901342","69*************04"] – XenPanda May 04 '20 at 04:02
  • And actually this time the node side has the same data on both the .tostring() and console.log(obj) `[ 'CSG-3 | Panda | 312, FA-18C_hornet, ((OK)), 3 pts, HLULX HLULIM H(LUL)IC H(LUL)AR, 1-wire, 14.1 seconds in the groove, (CASE I)', '63*******************42', '69*******************04' ]` I think it is possible that I didn't save the code change the first time with the .tostring() which may be why it looked the same as before. – XenPanda May 04 '20 at 04:05
  • So it appears all the info is there...I just can't seem to get it out to the variables correctly. – XenPanda May 04 '20 at 04:05
  • "I think it is possible that I didn't save the code change the first time with the .tostring() which may be why it looked the same as before" That seems like the most likely explanation to me. If you change back to start with 0 on the Node.js side, and save this time, then does it work? – Joseph Sible-Reinstate Monica May 04 '20 at 04:43
  • Just finished trying that and yep. Changing it back to [0],[1],[2] did the trick. Man I swear I had run it that way before with no luck. Thanks for all your help. I really appreciate it. – XenPanda May 04 '20 at 04:56

0 Answers0