Jump to content

Chat Example


Josh
 Share

Recommended Posts

41 minutes ago, DoomSlayer said:

The first thing I'll try to do is enable some sort of ghost multiplayer on Log Riders like the Trackmania series.
No player interaction, just you be able to see the other player jumping around.

It's doable?

Yes.  The commands are all documented.  Use Server:Publish to make your server public, then the clients call CountServers() and GetServer() to get a RemoteGame object.  the RemoteGame object has a member for the IP address and the server description, and you use that IP address to connect to the server.  Viola.

Your server would then just send out messages with the player and log positions / rotations and the client can watch the action.

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

4 hours ago, gamecreator said:

I assume you mean with Lua.  It's worked with C++ for years.

The missing piece was really the server list.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I'm going to post my WIP part 2 here.  This won't work when the server is behind a router. :(

--------------------------------------------------------
--Function to build user interface on each window
--------------------------------------------------------
function BuildGUI(context)
	local interface = {}
	interface.gui=GUI:Create(context)
	interface.root = interface.gui:GetBase()
	interface.root:SetScript("Scripts/GUI/Panel.lua")
	
	interface.chatpanel=Widget:Panel(0,0,interface.root:GetSize().x,interface.root:GetSize().y,interface.root)
	interface.chatpanel:SetAlignment(1,1,1,1)
	interface.chatlog = Widget:TextArea(4,4,interface.chatpanel:GetClientSize().x-8,interface.chatpanel:GetClientSize().y-30-8,interface.chatpanel)
	interface.textbox = Widget:TextField("",4,interface.chatpanel:GetClientSize().y-30,interface.chatpanel:GetClientSize().x-8-72-4,26,interface.chatpanel)
	interface.sendbutton = Widget:Button("Send",interface.chatpanel:GetClientSize().x-4-72,interface.chatpanel:GetClientSize().y-30,72,26,interface.chatpanel)
	interface.chatpanel:Hide()
	
	interface.startpanel=Widget:Panel(0,0,interface.root:GetSize().x,interface.root:GetSize().y,interface.root)
	interface.clientbutton = Widget:Button("Client",20,20,72,26,interface.startpanel)
	interface.serverbutton = Widget:Button("Server",20,60,72,26,interface.startpanel)
	
	interface.serverpanel=Widget:Panel(0,0,interface.root:GetSize().x,interface.root:GetSize().y,interface.root)
	interface.serverlist = Widget:ListBox(4,4,interface.serverpanel:GetClientSize().x-8,interface.serverpanel:GetClientSize().y-8-34,interface.serverpanel)
	interface.serverlist:SetAlignment(1,1,1,1)
	interface.joinbutton = Widget:Button("Join",4,interface.serverpanel:GetClientSize().y-28-8,interface.serverpanel:GetClientSize().x-4-4,28,interface.serverpanel)
	interface.serverpanel:Hide()
	
	return interface
end

--------------------------------------------------------
--Initialize client and server
--------------------------------------------------------
local MESSAGE_CHAT=1
local peer = nil
local port=3074
local appname = "LeadwerksChat62"
local connected=false
local lastrefreshtime = Time:Millisecs()

--------------------------------------------------------
--Create server interface
--------------------------------------------------------
window=Window:Create("Chat Server",0,0,400,300,Window.Titlebar)
--servercontext=Context:Create(serverwindow)
local interface = BuildGUI(window)

--------------------------------------------------------
--Main loop
--------------------------------------------------------
while true do
	
	--------------------------------------------------------
	--Exit the program
	--------------------------------------------------------
	if window:KeyHit(Key.Escape) then return end
	
	--------------------------------------------------------
	--Handle closed windows
	--------------------------------------------------------
	if window:GetHidden()==false then
		if window:Closed() then
			if networknode:GetClassName()=="Server" then
				networknode:Remove()
			else
				networknode:Disconnect()
			end			
			return
		end
	end
	
	--------------------------------------------------------
	--Update
	--------------------------------------------------------
	if connected then
		while true do
			
			if networknode:GetClassName()=="Server" then
				if (Time:Millisecs()-lastrefreshtime>1000*5) then
					networknode:Refresh()
					System:Print("Refresh")
					lastrefreshtime = Time:Millisecs()
				end
			end
			
			local message = networknode:Update()
			if message==nil then break end
			
			if message.id==Message.Connect then
				interface.chatlog:AddText(">> Connected to other computer.")
				
			elseif message.id==Message.Disconnect then
				interface.chatlog:AddText(">> Disconnected from other computer.")
				
			elseif message.id==MESSAGE_CHAT then
				interface.chatlog:AddText("Peer: "..message.stream:ReadString())
				
			end
			message:Release()
		end
	end
	
	--------------------------------------------------------
	--Handle GUI events
	--------------------------------------------------------
	while EventQueue:Peek() do
		local event = EventQueue:Wait()
		
		if event.id == Event.WidgetAction then
			
			if event.source == interface.sendbutton or event.source==interface.textbox then
				local s = interface.textbox:GetText()
				if networknode:GetClassName()=="Server" then
					networknode:Broadcast(MESSAGE_CHAT,s)
				else
					networknode:Send(MESSAGE_CHAT,s)
				end
				interface.chatlog:AddText("Me: "..s)
				interface.textbox:SetText("")
				interface.gui:SetFocus(interface.textbox)
				
			elseif event.source == interface.clientbutton then
				
				networknode = Client:Create()
				interface.serverpanel:Show()
				interface.startpanel:Hide()
				for n=0,networknode:CountServers(appname)-1 do
					local remotegame = networknode:GetServer(n)
					local address = remotegame.address
					interface.serverlist:AddItem(address,n==0)
				end
			
			elseif event.source == interface.joinbutton then
				local item = interface.serverlist:GetSelectedItem()
				if item>-1 then
					local address = interface.serverlist:GetItemText(item)
					if networknode:Connect(address,port)==false then
						System:Print("Failed to join server")
						return
					end
					interface.chatpanel:Show()
					interface.serverpanel:Hide()
					connected = true
				end
				
			elseif event.source == interface.serverbutton then
				
				networknode = Server:Create(port)
				networknode:Remove()
				if networknode:Publish(appname)==false then
					System:Print("Error: Failed to publish server.")
					return
				end
				interface.chatpanel:Show()
				interface.startpanel:Hide()
				connected=true
				
			end
			
		end
	end
	
	--------------------------------------------------------
	--Refresh both contexts
	--------------------------------------------------------
	--context:Sync()
end

 

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

  • 4 weeks later...

very nice :D

i am trying to recreate your example for my own purposes and get stuck at Server:Create() it always returns a nil value for me .
at some point i even added a line of code like this what gave the same result

if server == nil then server = Server:Create(port) end


my Main.lua gives an "attempt to index local 'server' (a nil value)" error i know what this means but cant realy understand why.
anybody able to help me out here ?

Main.lua

--Set the application title
title="GameServer"

--------------------------------------------------------
--Function to build user interfaca
--------------------------------------------------------
function BuildServerGUI(context)
	local interface = {}
	interface.gui=GUI:Create(context)
	interface.root = interface.gui:GetBase()
	interface.root:SetScript("Scripts/GUI/Panel.lua")
	interface.chatlog = Widget:TextArea(4,4,interface.root:GetClientSize().x-8,interface.root:GetClientSize().y-30-8,interface.root)
	interface.textbox = Widget:TextField("",4,interface.root:GetClientSize().y-30,interface.root:GetClientSize().x-8-72-4,26,interface.root)
	interface.sendbutton = Widget:Button("Send",interface.root:GetClientSize().x-4-72,interface.root:GetClientSize().y-30,72,26,interface.root)
	return interface
end

--------------------------------------------------------
--Initialize client and server
--------------------------------------------------------
local MESSAGE_CHAT=1
local peer = nil
local port=8000
--local client = Client:Create()
local server = Server:Create(port)
--local success = client:Connect("127.0.0.1",port)

--------------------------------------------------------
--Create a server window
--------------------------------------------------------
serverwindow=Window:Create(title,0,0,800,600,Window.Titlebar)
local serverInterface = BuildServerGUI(serverwindow)


--------------------------------------------------------
--Main loop
--------------------------------------------------------
while true do
	
	--------------------------------------------------------
	--Exit the program
	--------------------------------------------------------
	if serverwindow:KeyHit(Key.Escape) then return end
	
	--------------------------------------------------------
	--Handle closed window
	--------------------------------------------------------
	if serverwindow:GetHidden()==false then
		if serverwindow:Closed() then
			serverwindow:Hide()
			server:Disconnect(peer)
		end
	end
	
	--------------------------------------------------------
	--Update server
	--------------------------------------------------------
	while true do 
		if server == nil then server = Server:Create(port) end
		
		local message = server:Update() 	<<----------Gives error on this line
		if message==nil then break end
		
		if message.id==Message.Connect then
			serverInterface.chatlog:AddText(">> New client connected.")
			peer = message.peer
			
		elseif message.id==Message.Disconnect then
			serverInterface.chatlog:AddText(">> Client disconnected.")
			
		elseif message.id==MESSAGE_CHAT then
			serverInterface.chatlog:AddText("Client: "..message.stream:ReadString())
			
		end
		message:Release()
	end
	
	--------------------------------------------------------
	--Handle GUI events
	--------------------------------------------------------
	while EventQueue:Peek() do
		local event = EventQueue:Wait()
		
		if event.id == Event.WidgetAction then
			
			if event.source == serverInterface.sendbutton or event.source==serverInterface.textbox then
				local s = serverInterface.textbox:GetText()
				server:Send(peer,MESSAGE_CHAT,s)
				serverInterface.chatlog:AddText("Me: "..s)
				serverInterface.textbox:SetText("")
				serverInterface.gui:SetFocus(serverInterface.textbox)
			end
		
		end
	end
	
end

 

Link to comment
Share on other sites

11 minutes ago, GorzenDev said:

this did work thank you for that.

could you maiby explain why ?
my expertise in networking is limited

Your network card does not have that port?

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

18 minutes ago, Josh said:

Your network card does not have that port?

like i said my expertise in networking is limited so there is no way i can say you are wrong.
but i have ran gameservers for steamgames and those are in the port ranges of 27000.
anyway thanks for the help i can tinker some more now (fun fun :D)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...