Playername in a secondary table - Destiny's Player - 25-04-2015
So, another one of my problem threads. I'm trying to make a table, which has a secondary table for each player on the server/in the faction, which then has two values on 1 and 2, which correspond to that player's cursor position on the screen.
The problem is...
"SF tarfall/Selena_AI.txt:290: attempt to index field 'The Next Decade' (a nil value)"
And I don't know what changed between now and before I relogged on the server, as it worked perfectly before I relogged.
Here's the code.
--@name Selena AI
--@author TheNextDecade
--@sharedscreen
AIname = "Selena"
Shipname = "Tzar"
Self = ents elf()
Owner = ents:owner()
OwnName = Owner:name()
if SERVER then
Players = {}
Players.All = find.allPlayers()
Players.Faction = faction.getOnlineMembers()
timer.create("PlayerUpd",5,0,function()
Players.All = find.allPlayers()
Players.Faction = faction.getOnlineMembers()
end)
timer.create("NetSlow",1,0,function()
--hook("think","Repeat",function()
if net.start() then
net.writeTable(Players)
net.broadcast()
end
--end)
end)
else
Basic = render.createFont("default",13,400,1,0,0,1,0)
Head = render.createFont("default",26,400,1,0,0,1,0)
Ancient = render.createFont("anquietas",13,400,1,0,0,1,0)
MenuCol = {235,210,120}
PlayersCl = {}
PlOn = {}
MemOn = {}
Sound = {}
Sound.beep = sounds.create(Self,"st/console/beep05.wav")
function Cursor(ply)
local CurPosX,CurPosY = render.cursorPos(ply)
render.setColor(255,255,255)
if CurPosX ~= nil and CurPosY ~= nil then
render.drawRectOutline(CurPosX-3,CurPosY-3,6,6)
render.drawRect(CurPosX-1,CurPosY-1,2,2)
return CurPosX,CurPosY
end
end
function FacCursor(fac)
facCurs = {}
for i=1,#fac do
facCurs[fac[i]:name()] = {}
facCurs[fac[i]:name()][1],facCurs[fac[i]:name()][2] = Cursor(fac[i])
end
return facCurs
end
function Press(ply)
return ply:keyDown(input.IN_USE)
end
function Mouse(num,butx,butxlen,buty,butylen,moux,mouy,ply)
if moux ~= nil and mouy ~= nil then
if mouy >= buty-butylen and mouy <= buty+butylen then
if moux >= butx-butxlen and moux <= butx+butxlen then
if Press(ply) then
Beeper()
--print(num)
Pg = num
end
end
end
end
end
function Background(col)
render.setColor(col[1]-200,col[2]-200,col[3]-200)
render.drawRect(0,0,512,512)
render.setColor(col[1]+70,col[2]+70,col[3]+70)
render.drawLine(5,5,5,507)
render.drawLine(5,5,507,5)
render.drawLine(507,5,507,507)
render.drawLine(5,507,507,507)
end
function Header(col)
local TLPoly = render.createPoly({
render.vertex(10,15),
render.vertex(80,15),
render.vertex(90,60),
render.vertex(25,60),
render.vertex(15,50)
})
local AINPoly = render.createPoly({
render.vertex(15,19),
render.vertex(75,19),
render.vertex(80,34),
render.vertex(20,34)
})
local SNPoly = render.createPoly({
render.vertex(19,35),
render.vertex(80,35),
render.vertex(84,50),
render.vertex(24,50)
})
local TRPoly = render.createPoly({
render.vertex(420,15),
render.vertex(480,15),
render.vertex(500,30),
render.vertex(500,50),
render.vertex(460,60),
render.vertex(430,60)
})
local OPPoly = render.createPoly({
render.vertex(430,28),
render.vertex(492,28),
render.vertex(497,44),
render.vertex(435,44)
})
render.setColor(col[1],col[2],col[3])
render.drawPoly(TLPoly)
render.drawPoly(TRPoly)
render.drawRect(70,15,360,5)
render.drawRect(80,35,350,5)
render.drawRect(85,55,350,5)
render.setColor(col[1]-200,col[2]-200,col[3]-200)
render.drawPoly(AINPoly)
render.drawPoly(SNPoly)
render.drawPoly(OPPoly)
render.setColor(col[1]+70,col[2]+70,col[3]+70)
--Top Left
--Main
render.drawLine(10,15,80,15)
render.drawLine(80,20,84,35)
render.drawLine(85,40,89,55)
render.drawLine(90,60,25,60)
render.drawLine(25,60,15,50)
render.drawLine(15,50,10,15)
--Inner
render.drawLine(19,34,80,34)
render.drawLine(15,19,23,50)
render.drawLine(15,19,75,19)
render.drawLine(76,19,84,50)
render.drawLine(24,50,84,50)
--Rects
--Top
render.drawLine(80,15,420,15)
render.drawLine(80,20,421,20)
--Middle
render.drawLine(84,35,424,35)
render.drawLine(85,40,426,40)
--Bottom
render.drawLine(89,55,428,55)
render.drawLine(90,60,431,60)
--Top Right
--Main
render.drawLine(420,15,480,15)
render.drawLine(480,15,500,30)
render.drawLine(500,30,500,50)
render.drawLine(500,50,460,60)
render.drawLine(460,60,430,60)
render.drawLine(428,54,426,40)
render.drawLine(424,35,421,20)
--Inner
render.drawLine(430,28,492,28)
render.drawLine(492,28,497,44)
render.drawLine(497,44,435,44)
render.drawLine(435,44,430,28)
end
function TopText(col,mess)
render.setColor(col[1]+70,col[2]+70,col[3]+70)
render.drawText(Ancient,253,23,mess,render.TEXT_ALIGN_CENTER)
render.drawText(Basic,20,20,"AI: " .. AIname,render.TEXT_ALIGN_LEFT)
render.drawText(Basic,25,35,"Ship: " .. Shipname,render.TEXT_ALIGN_LEFT)
OPs = 100/opsMax()*opsUsed()
render.drawText(Basic,490,30,"OP's: "..OPs.."%",render.TEXT_ALIGN_RIGHT)
end
function TopButton(col,leng,posx,posy,pname,press,page)
local partLeng = leng/8
if press == page then
render.setColor(col[1]-50,col[2]-50,col[3]-50)
else
render.setColor(col[1],col[2],col[3])
end
local But = render.createPoly({
render.vertex(posx-partLeng-4,posy-6),
render.vertex(posx+partLeng-3,posy-6),
render.vertex(posx+partLeng-1,posy+5),
render.vertex(posx-partLeng-2,posy+5)
})
render.drawPoly(But)
render.setColor(col[1]+70,col[2]+70,col[3]+70)
render.drawLine(posx-partLeng-4,posy-6,posx-partLeng-2,posy+6)
render.drawLine(posx+partLeng-3,posy-6,posx+partLeng-1,posy+6)
render.drawLine(posx-partLeng-3,posy-6,posx+partLeng-3,posy-6)
render.drawLine(posx-partLeng-1,posy+5,posx+partLeng-1,posy+5)
render.setColor(col[1]+120,col[2]+120,col[3]+120)
render.drawText(Basic,posx,posy-7,pname,render.TEXT_ALIGN_CENTER)
end
function Beeper()
Sound.beep:play()
timer.simple(0.2,function() Sound.beep top() end)
end
function Page(col)
render.setColor(col[1],col[2],col[3])
render.drawRect(20,70,472,422)
end
function Greet(col)
Greet0 = render.createPoly({
render.vertex(40,170),
render.vertex(472,170),
render.vertex(482,180),
render.vertex(482,280),
render.vertex(472,290),
render.vertex(40,290),
render.vertex(30,280),
render.vertex(30,180)
})
render.setColor(col[1],col[2],col[3])
render.drawPoly(Greet0)
render.setColor(col[1]+70,col[2]+70,col[3]+70)
render.drawLine(40,170,472,170)
render.drawLine(472,170,482,180)
render.drawLine(482,180,482,280)
render.drawLine(481,280,471,290)
render.drawLine(471,290,40,290)
render.drawLine(40,290,30,280)
render.drawLine(30,280,30,179)
render.drawLine(30,179,40,169)
Greet1 = "Good time of the day to you, "..OwnName.."!"
Greet2 = "Everything's in prime condition\n and is awaiting your command."
render.drawText(Head,256,180,Greet1,render.TEXT_ALIGN_CENTER)
render.drawText(Head,256,220,Greet2,render.TEXT_ALIGN_CENTER)
end
hook("net","NetRec",function()
PlayersCl = net.readTable()
PlOn = PlayersCl.All
MemOn = PlayersCl.Faction
end)
timer.create("Slow",0.2,0,function()
hook("render","Rendering",function()
render.clear()
Background(MenuCol)
Header(MenuCol)
TopText(MenuCol,"The1Next2Decade3is4the5creator6of7this8AI9and10he11is12really13cool")
TopButton(MenuCol,328,133,48,"Status",Pg,1)
TopButton(MenuCol,327,218,48,"Tactical",Pg,2)
TopButton(MenuCol,328,302,48,"Engineering",Pg,3)
TopButton(MenuCol,327,387,48,"Warp",Pg,4)
Mouse(1,133,41,48,6,MePosX,MePosY,Owner)
Mouse(2,218,41,48,6,MePosX,MePosY,Owner)
Mouse(3,302,41,48,6,MePosX,MePosY,Owner)
Mouse(4,387,41,48,6,MePosX,MePosY,Owner)
--PageNum = 0
if Pg == 1 then
Page(MenuCol)
elseif Pg == 2 then
Page({100,100,100})
elseif Pg == 3 then
Page({100,150,100})
elseif Pg == 4 then
Page({100,100,150})
else
Greet(MenuCol)
end
MePosX,MePosY = Cursor(Owner)
FactionCursors = FacCursor(PlOn)
--for i=1,#FactionCursors do
-- render.drawText(Basic,50,300+i*20,tostring(FactionCursors[i]),render.TEXT_ALIGN_LEFT)
--end
Test1 = FactionCursors["The Next Decade"][1]
Test2 = FactionCursors["The Next Decade"][2]
render.drawText(Basic,50,300,tostring(Test1).."\n"..tostring(Test2),render.TEXT_ALIGN_LEFT)
--render.drawText(Basic,50,300,tostring(FactionCursors["The Next Decade"[1]]).."\n"..tostring(FactionCursors["The Next Decade"[2]]),render.TEXT_ALIGN_LEFT)
for i=1, #MemOn do
if MemOn[i]:isValid() then
if MemOn[i]:class() == "player" then
local WriteMem = MemOn[i]:name()
render.drawText(Basic,254,275+i*20,WriteMem,render.TEXT_ALIGN_RIGHT)
end
else
render.drawText(Basic,180,275+i*20,"DISCONNECTED",render.TEXT_ALIGN_RIGHT)
end
end
for i=1, #PlOn do
if PlOn[i]:isValid() then
if PlOn[i]:class() == "player" then
local WritePl = PlOn[i]:name()
render.drawText(Basic,258,275+i*20,WritePl,render.TEXT_ALIGN_LEFT)
end
else
render.drawText(Basic,180,275+i*20,"DISCONNECTED",render.TEXT_ALIGN_LEFT)
end
end
end)
end)
end
RE: Playername in a secondary table - Person8880 - 25-04-2015
Again with the write table .
Anyway, the reason is again the asynchronous nature of the code. You can't guarantee that you have the data you want all the time. You're sending it once per second, so the error is because you haven't received the player data yet.
RE: Playername in a secondary table - Destiny's Player - 25-04-2015
That's what I thought, but I don't know what to do with it, cause if I remove the timer that slows the sending down, it exceeds the net quota, and checking for if FactionCursors["The Next Decade"]:isValid() doesn't help, I think that it just makes the error appear earlier.
Any ideas of what I can do with this problem?
RE: Playername in a secondary table - Person8880 - 25-04-2015
I think you're getting a little confused about validity and existence.
The :isValid() method applies to entities (and VGUI panels), it's to determine if they still exist. This is not the same as their Lua value existing in a Lua table. Even if :isValid() returns false their Lua value still exists (and is truthy), just representing a null value in the engine code.
If you want to check if your name has an entry in the FactionCursors table, it's as easy as:
if FactionCursors[ "The Next Decade" ] then
This statement will only evaluate if the entry under "The Next Decade" is truthy in Lua, which is any value except for false and nil.
RE: Playername in a secondary table - Destiny's Player - 25-04-2015
Yup, it works. Thank you! But now there's a different problem: I can't get the X and the Y out of there. Or something else, but I think that it's the first one.
Here's the part I'm working on.
function MouseMul(ply,plynm,tab,pg,pgtab)
if tab[plynm] then
for i=1,#tab do
local MX = tab[plynm][1]
local MY = tab[plynm][2]
Mouse(pg,pgtab[pg],41,48,6,MX,MY,ply)
end
--Test1 = FactionCursors["The Next Decade"][1]
--Test2 = FactionCursors["The Next Decade"][2]
end
end
TButXes = {133,218,302,387}
FactionCursors = FacCursor(PlOn)
MouseMul(Owner,"The Next Decade",FactionCursors,1,TButXes)
RE: Playername in a secondary table - Person8880 - 25-04-2015
This bit:
for i=1,#tab do
is doing nothing. Your table is a key-value mapping of string to table, so there's no number keys and thus #tab == 0.
There's also the fact that you're not wanting to loop at all here, as you're passing in the specific player you're after.
function MouseMul(ply,plynm,tab,pg,pgtab)
if tab[plynm] then
local MX = tab[plynm][1]
local MY = tab[plynm][2]
Mouse(pg,pgtab[pg],41,48,6,MX,MY,ply)
end
end
RE: Playername in a secondary table - Destiny's Player - 25-04-2015
Again, thanks a lot! With your changes, and this loop, it works. Now, each client sees his own thing, but that's not really important at this point.
Also, I thought about using a second for loop, but decided against it for simplicity's sake.
for i=1,#PlOn do
local Player = PlOn[i]
local Playername = Player:name()
MouseMul(Player,Playername,FactionCursors,1,TButXes)
MouseMul(Player,Playername,FactionCursors,2,TButXes)
MouseMul(Player,Playername,FactionCursors,3,TButXes)
MouseMul(Player,Playername,FactionCursors,4,TButXes)
end
|