• Array looping bullshit
    6 replies, posted
Okay, the code I am using works, except for one problem. The arrays are send back-the-front. So serverside, the array is stored for example, [code] [0] = "Watermelon" [1] = "Watermelon2" [/code] And clientside, it is stored as [code] [1] = "Watermelon" [0] = "Watermelon2" [/code] The code I am using is as follows. Serverside: [lua] local derp = 0 for k, v in pairs(ItemNameArray) do timer.Create(ply:SteamID().."asd"..derp, 0.1, 1, function() umsg.Start("SendItemInfo") umsg.Long(1) umsg.String(v) umsg.End() end) derp = derp + 1 end [/lua] Clientside: [lua] function SetItemArray( a ) if a:ReadLong() == 1 then ItemNameArray[noitem] = a:ReadString() //LocalPlayer():ChatPrint(ItemNameArray[noitem]) noitem = noitem + 1 end //Msg("["..noitem.."] = "..ItemNameArray[noitem - 1].."\n") end usermessage.Hook("SendItemInfo", SetItemArray) [/lua] I tried to change the serverside code to this: [lua] derp = 0 local derp = 0 while derp < table.Count(ItemNameArray) + 1 do timer.Create(ply:SteamID().."asd"..derp, 0.1, 1, function() umsg.Start("SendItemInfo") umsg.Long(1) umsg.String(ItemNameArray[derp]) umsg.End() end) derp = derp + 1 end [/lua] but it gave me an error for the line with umsg.String(ItemNameArray[derp]) line, stating that I had used a nil value instead of a string. The table ItemNameArray starts at 0
Start your keys at 1 then move to 2 and so on if using numerical keys. If not it will return in the order sent. I think it's returning like that because it looks for 1 first.
So, do you want me to start both arrays at 1?
[QUOTE=blackfire88;36685848]So, do you want me to start both arrays at 1?[/QUOTE] Yes.
I did that, it worked, then I added another item to the array. Clientside Array: [code] 1 = Watermelon 2 = Watermelon3 3 = Watermelon2 [/code] Serverside: [code] 1 = Watermelon2 2 = Watermelon3 0 = Watermelon [/code] [editline]9th July 2012[/editline] So yes, just to clarify, there is still a problem
Untested should work: clientside: [lua] function SetItemArray(data) local NumItems = data:ReadShort() for i = 1, NumItems do local vals = data:ReadString() print(i, vals) end end usermessage.Hook("SendItemInfo", SetItemArray) [/lua] serverside: [lua] local Watermelons = {} Watermelons[1] = "Watermelon" Watermelons[2] = "Watermelon2" function WatermelonUM(ply, cmd, args) umsg.Start("SendItemInfo", ply) umsg.Short(#Watermelons) for i = 1, #Watermelons do umsg.String(Watermelons[i]) end umsg.End() end concommand.Add("startUM", WatermelonUM) [/lua]
Your problem is that you used pairs, Not Ipairs while sending the table. pairs retrives the values in a random order, Where as ipairs retrives them alphanumericly.
Sorry, you need to Log In to post a reply to this thread.