Client to server net message - Destiny's Player - 17-09-2015
For some reason I can't get it to work. It simply returns nothing. Am I missing something important?
Here's the code.
--@name Warp Control Screen
--@author The Next Decade
Self = ents.self()
Owner = ents.owner()
WarpGroups = {"Milky Way","Pegausus","Universe","Other"}
WarpDests = {}
WarpDests.MilkyWay = {}
WarpDests.MilkyWay.Names = {"Earth","Moon"}
WarpDests.MilkyWay["Earth"] = Vector(109,10616,8305)
WarpDests.MilkyWay["Moon"] = Vector(-4314,13676,10349)
WarpDests.Pegausus = {}
WarpDests.Pegausus.Names = {"Shakurus"}
WarpDests.Pegausus["Shakurus"] = Vector(-9539,-7141,7044)
WarpDests.Universe = {}
WarpDests.Universe.Names = {"Hiigara","Desert","Lava"}
WarpDests.Universe["Hiigara"] = Vector(3335,10297,-5790)
WarpDests.Universe["Desert"] = Vector(8654,-9989.8347)
WarpDests.Universe["Lava"] = Vector(-10887,634,-3813)
WarpDests.Other = {}
WarpDests.Other.Names = {"Build Room"}
WarpDests.Other["Build Room"] = Vector(12533,14255,-15678)
if SERVER then
if ents.self():class() ~= "starfall_screen" then
error("Not a Starfall screen")
else
hook("net","FromClient",function(Length,Player)
ClientStuff = net.readTable()
end)
function getWarpDrive()
ShipEnts = Self:getConstraints()
for i=1, #ShipEnts do
local Cur = ShipEnts[i]
if Cur:class() == "quantum_slipstream_drive" then
WarpDrive = Cur
elseif Cur:class() == "ship_drive" then
WarpDrive = Cur
end
end
printConsole(WarpDrive:class())
WiredWD = WarpDrive:getWirelink()
end
function getGroup()
if ClientStuff[2] == 1 then
Group = WarpDests.MilkyWay
elseif ClientStuff[2] == 2 then
Group = WarpDests.Pegausus
elseif ClientStuff[2] == 3 then
Group = WarpDests.Universe
elseif ClientStuff[2] == 4 then
Group = WarpDests.Other
end
end
getWarpDrive()
timer.create("Run",0.5,0,function()
gerGroup()
Dest = Group[Group.Names[ClientStuff[3]]]
WiredWD["Destination"] = Dest
end)
end
else
--Cursor Stuff
function Cursor(ply)
local CurPosX,CurPosY = render.cursorPos(ply)
if CurPosX ~= nil and CurPosY ~= nil then
return CurPosX,CurPosY
end
end
function Pointer(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)
end
end
function Press(ply)
return ply:keyDown(input.IN_USE)
end
--Render Stuff
function Draw4(press,p1x,p1y,p2x,p2y,p3x,p3y,p4x,p4y,col)
local Polygon = render.createPoly({
render.vertex(p1x,p1y),
render.vertex(p2x,p2y),
render.vertex(p3x,p3y),
render.vertex(p4x,p4y)
})
if press == 1 then
render.setColor(col[1]+20,col[2]+20,col[3]+20)
else
render.setColor(col[1],col[2],col[3])
end
render.drawPoly(Polygon)
render.setColor(255,255,255)
render.drawLine(p1x,p1y,p2x,p2y)
render.drawLine(p2x,p2y,p3x,p3y)
render.drawLine(p3x,p3y,p4x,p4y)
render.drawLine(p4x,p4y,p1x,p1y)
end
function Draw6(press,p1x,p1y,p2x,p2y,p3x,p3y,p4x,p4y,p5x,p5y,p6x,p6y,col)
local Polygon = render.createPoly({
render.vertex(p1x,p1y),
render.vertex(p2x,p2y),
render.vertex(p3x,p3y),
render.vertex(p4x,p4y),
render.vertex(p5x,p5y),
render.vertex(p6x,p6y)
})
if press == 1 then
render.setColor(col[1]+20,col[2]+20,col[3]+20)
else
render.setColor(col[1],col[2],col[3])
end
render.drawPoly(Polygon)
render.setColor(255,255,255)
render.drawLine(p1x,p1y,p2x,p2y)
render.drawLine(p2x,p2y,p3x,p3y)
render.drawLine(p3x,p3y,p4x,p4y)
render.drawLine(p4x,p4y,p5x,p5y)
render.drawLine(p5x,p5y,p6x,p6y)
render.drawLine(p6x,p6y,p1x,p1y)
end
function Draw8(press,p1x,p1y,p2x,p2y,p3x,p3y,p4x,p4y,p5x,p5y,p6x,p6y,p7x,p7y,p8x,p8y,col)
local Polygon = render.createPoly({
render.vertex(p1x,p1y),
render.vertex(p2x,p2y),
render.vertex(p3x,p3y),
render.vertex(p4x,p4y),
render.vertex(p5x,p5y),
render.vertex(p6x,p6y),
render.vertex(p7x,p7y),
render.vertex(p8x,p8y)
})
if press == 1 then
render.setColor(col[1]+20,col[2]+20,col[3]+20)
else
render.setColor(col[1],col[2],col[3])
end
render.drawPoly(Polygon)
render.setColor(255,255,255)
render.drawLine(p1x,p1y,p2x,p2y)
render.drawLine(p2x,p2y,p3x,p3y)
render.drawLine(p3x,p3y,p4x,p4y)
render.drawLine(p4x,p4y,p5x,p5y)
render.drawLine(p5x,p5y,p6x,p6y)
render.drawLine(p6x,p6y,p7x,p7y)
render.drawLine(p7x,p7y,p8x,p8y)
render.drawLine(p8x,p8y,p1x,p1y)
end
--Sound Stuff
function Beeper()
Beep:play()
timer.simple(0.2,function() Beep top() end)
end
--Button Stuff
function Button(xpos1,ypos1,xpos2,ypos2)
local num = 0
if CurX ~= nil and CurY ~= nil then
if CurX >= xpos1 and CurX <= xpos2 then
if CurY >= ypos1 and CurY <= ypos2 then
if Used then
num = 1
Beeper()
return num
end
end
end
end
end
--Prettification Stuff
Basic = render.createFont("default",20,1000,1,1,0,0,0)
Big = render.createFont("default",50,1000,1,1,0,0,0)
Ancient = render.createFont("anquietas",15,1000,1,1,0,0,0)
Beep = sounds.create(Self,"st/console/beep06.wav")
--Pre-render stuff
DestNum = 1
GroupNum = 1
DestPressed = 0
GroupPressed = 0
WarpBut = 0
NetTable = {}
timer.create("ClientSide",0.5,0,function()
hook("render","Rendering",function()
CurX,CurY = Cursor(Owner)
Used = Press(Owner)
render.clear()
--Background
render.setColor(150,150,150)
render.drawRect(0,0,512,512)
--Group BG
BGCol = {50,50,50}
Draw8(0,40,10,470,10,500,30,500,60,470,80,40,80,10,60,10,30,BGCol)
--Group Buttons
GroupPrev = Button(15,17,50,73)
GroupNext = Button(459,17,494,73)
--Group Select
if GroupNum > 4 then
GroupNum = 1
elseif GroupNum < 1 then
GroupNum = 4
end
if GroupPressed == 0 then
if GroupPrev == 1 then
GroupNum = GroupNum - 1
GroupPressed = 1
elseif GroupNext == 1 then
GroupNum = GroupNum + 1
GroupPressed = 1
end
end
if GroupNext ~= 1 and GroupPrev ~= 1 then
GroupPressed = 0
end
--Group Table Selector
if GroupNum == 1 then
SelectedGroup = WarpDests.MilkyWay
elseif GroupNum == 2 then
SelectedGroup = WarpDests.Pegausus
elseif GroupNum == 3 then
SelectedGroup = WarpDests.Universe
elseif GroupNum == 4 then
SelectedGroup = WarpDests.Other
end
--Group Buttons Draw
SelBtCol = {160,120,50}
Draw4(ButPrev,15,38,50,17,50,73,15,52,SelBtCol)
Draw4(ButNext,459,17,494,38,494,52,459,73,SelBtCol)
--Group Name
render.setColor(255,255,255)
if WarpGroups[GroupNum] ~= nil then
render.drawText(Big,256,25,"[ "..WarpGroups[GroupNum].." ]",render.TEXT_ALIGN_CENTER)
render.drawText(Ancient,256,15,WarpGroups[GroupNum],render.TEXT_ALIGN_CENTER)
end
--Selector BG
BGCol = {50,50,50}
Draw8(0,40,90,470,90,500,110,500,140,470,160,40,160,10,140,10,110,BGCol)
--Selector Buttons
DestPrev = Button(15,97,50,153)
DestNext = Button(459,97,494,153)
--Selector Select
if DestNum > #SelectedGroup.Names then
DestNum = 1
elseif DestNum < 1 then
DestNum = #SelectedGroup.Names
end
if DestPressed == 0 then
if DestPrev == 1 then
DestNum = DestNum - 1
DestPressed = 1
elseif DestNext == 1 then
DestNum = DestNum + 1
DestPressed = 1
end
end
if DestNext ~= 1 and DestPrev ~= 1 then
DestPressed = 0
end
--Selector Buttons Draw
SelBtCol = {160,120,50}
Draw4(DestPrev,15,118,50,97,50,153,15,132,SelBtCol)
Draw4(DestNext,459,97,494,118,494,132,459,153,SelBtCol)
--Destination Name
DestName = SelectedGroup.Names[DestNum]
render.setColor(255,255,255)
if DestName ~= nil then
render.drawText(Big,256,105,"[ "..DestName.." ]",render.TEXT_ALIGN_CENTER)
render.drawText(Ancient,256,95,DestName,render.TEXT_ALIGN_CENTER)
end
--Warp Button
WarpBut = Button(50,256,460,376)
--Warp Button Draw
WpBtCol = {0,100,200}
Draw8(WarpBut,80,226,430,226,460,296,460,366,430,426,80,426,50,366,50,296,WpBtCol)
render.setColor(255,255,255)
render.drawText(Big,256,300,"WARP",render.TEXT_ALIGN_CENTER)
--Just the cursor render
Pointer(Owner)
end)
end)
--Network stuff
hook("starfall_used","Used",function()
if ents.player() == Owner then
if net.start() then
if NetTable ~= nil then
--NetTable =
net.writeTable({WarpBut,GroupNum,DestNum})
net.sendToServer()
end
end
end
end)
end
RE: Client to server net message - Person8880 - 17-09-2015
This is a really convoluted and odd way of trying to accomplish pressing a button.
Problems:
- Your initial timer is practically certain to run before any network message can be received.
- You're sending a table for 3 values, this is wasteful and really OTT.
You do not need to use a timer to do something with what you're sent. You have the net event, use it!
Client:
hook( "starfall_used", "Used", function()
if ents.player() ~= Owner then return end
if not net.start() then return end
-- You should change WarpBut to be a boolean, not a number. You'll have to change your Button() function.
net.writeBool( WarpBut )
-- 3 bits = values 0-7, up the bit count if you need more.
net.writeUInt( GroupNum, 3 )
net.writeUInt( DestNum, 3 )
net.sendToServer()
end )
Server:
hook( "net", "FromClient", function( Length, Player )
local WarpBut = net.readBool()
-- Remember to read the same amount of bits as you write.
local GroupNum = net.readUInt( 3 )
local DestNum = net.readUInt( 3 )
-- Now perform warp destination stuff right here, there's no need for a timer!
end )
RE: Client to server net message - Destiny's Player - 17-09-2015
Welp, I'm a stupid for not figuring out that I could just stick the warping stuff into the hook. Thanks a lot for that, and for the actual message suggestion.
RE: Client to server net message - Destiny's Player - 18-09-2015
Doesn't seem to work. This is the code right now, neither the print nor the printConsole work, and for me, that means that the net hook simply doesn't react to the message. Maybe the message itself is not sent or something?
Client
hook("starfall_used","Used",function()
if ents.player() ~= Owner then return end
if not net.start() then return end
if WarpBut == 1 then
WarpButBool = true
else
WarpButBool = false
end
net.writeBool(WarpButBool)
net.writeUInt(GroupNum, 3)
net.writeUInt(DestNum, 3)
net.sendToServer()
end)
Server
hook("net","FromClient",function(Length,Player)
local WarpButton = net.readBool()
local GroupNumber = net.readUInt(3)
local DestNumber = net.readUInt(3)
GetGroup()
DestName = Group.Names[DestNumber]
Destination = Group[DestName]
print("AAAAH"..DestName)
end)
RE: Client to server net message - LocInt - 18-09-2015
(18-09-2015, 08:21 PM)Destinys Player Wrote: Doesn't seem to work. This is the code right now, neither the print nor the printConsole work, and for me, that means that the net hook simply doesn't react to the message. Maybe the message itself is not sent or something?
Client
hook("starfall_used","Used",function()
if ents.player() ~= Owner then return end
if not net.start() then return end
if WarpBut == 1 then
WarpButBool = true
else
WarpButBool = false
end
net.writeBool(WarpButBool)
net.writeUInt(GroupNum, 3)
net.writeUInt(DestNum, 3)
net.sendToServer()
end)
Server
hook("net","FromClient",function(Length,Player)
local WarpButton = net.readBool()
local GroupNumber = net.readUInt(3)
local DestNumber = net.readUInt(3)
GetGroup()
DestName = Group.Names[DestNumber]
Destination = Group[DestName]
print("AAAAH"..DestName)
end)
You check if the net is started but you're missing an actual line to start it.
RE: Client to server net message - Destiny's Player - 18-09-2015
From what I remember, checking "if net.start then" actually starts the net message.
RE: Client to server net message - LocInt - 18-09-2015
(18-09-2015, 08:40 PM)Destinys Player Wrote: From what I remember, checking "if net.start then" actually starts the net message.
But you're not doing that. You're doing a return if net is not started. This is why it doesn't return anything.
RE: Client to server net message - Person8880 - 18-09-2015
(18-09-2015, 08:41 PM)LocInt Wrote: (18-09-2015, 08:40 PM)Destinys Player Wrote: From what I remember, checking "if net.start then" actually starts the net message.
But you're not doing that. You're doing a return if net is not started. This is why it doesn't return anything. That's not the problem. It's returning if the starting of the message failed. If starting succeeds it proceeds to send the message.
The problem is you're missing --@shared at the top. Your code is only running on the client.
RE: Client to server net message - Destiny's Player - 18-09-2015
(18-09-2015, 08:54 PM)Person8880 Wrote: (18-09-2015, 08:41 PM)LocInt Wrote: (18-09-2015, 08:40 PM)Destinys Player Wrote: From what I remember, checking "if net.start then" actually starts the net message.
But you're not doing that. You're doing a return if net is not started. This is why it doesn't return anything. That's not the problem. It's returning if the starting of the message failed. If starting succeeds it proceeds to send the message.
The problem is you're missing --@shared at the top. Your code is only running on the client.
...
...
...
GAAAH.
Always forget that. I'm a ninny. Thanks a lot.
RE: Client to server net message - Person8880 - 18-09-2015
For what it's worth, attempting to start a net message on the client when the instance is not shared will now throw an error so you won't be left scratching your head next time.
|