Currently the developers are putting their own money into JC2-MP to keep the servers online.

Please take a few seconds of your time and disable your AdBlock plugin for our website.

Ad revenue is not going to developers, it is used purely for covering our hosting costs.

 

You are also free to donate, which removes all ads from our website!

Patch 0.3 was just released! Full changelog here: https://t.co/4A50m6IKen

2 years ago

Advertisement
September 20, 2019, 10:19:31 pm

Author Topic: couple questions  (Read 2592 times)

Whitetiger

  • Donator
  • Newbie
  • *****
  • Posts: 6
    • View Profile
couple questions
« on: December 15, 2013, 05:33:21 pm »
so I have a couple quick questions.

in the wiki, most of the events have only 1 argument, and then you use this as a table. can we use multiple arguments in the event and not use only the first one as a table?

example:

Quote
Foo = function(args)
   local message = tostring(args.attacker).." shot you for "..tostring(args.damage).." damage!"
   Chat:Print(message, Color(255, 255, 255))
end
 
Events:Subscribe("LocalPlayerBulletHit", Foo)

instead of this, can we use:

Quote
Foo = function(attacker, damage)
   local message = tostring(attacker).." shot you for "..tostring(damage).." damage!"
   Chat:Print(message, Color(255, 255, 255))
end
 
Events:Subscribe("LocalPlayerBulletHit", Foo)

?

another question I have is the server to client communication. one of the developers or beta testers posted somewhere that you guys would post a tutorial on that, but I started looking around the wiki.

client:

Quote

function Foo()
   
end

function FooWithObject(obj)
   
end

Events:Subscribe("MyCustomEvent", Foo)
Events:Subscribe("MyCustomEventWObject", FooWithObject)

server:

Quote
function playerChat(args)
   Network:Send(args.player, "MyCustomEventWObject", args); -- sends the event to ONLY the player who typed a message, to send to everyone, we use "Broadcast"?

        Network:Send(args.player, "MyCustomEvent");
end
 
Events:Subscribe("PlayerChat", playerChat)

is this how we would do the client-server communication?

if it is, what in the hell does SendNearby do(send to streamed in players near a certain player only?) or SendToPlayers(send to a group of players, but not necessarily everyone?)
« Last Edit: December 15, 2013, 06:11:08 pm by Whitetiger »

Philpax

  • Developer
  • Hero Member
  • *****
  • Posts: 563
    • View Profile
Re: couple questions
« Reply #1 on: December 16, 2013, 12:45:52 am »
No, you can't do the former as that would mean argument order is unspecified. You could just do:
Code: [Select]
function LocalPlayerBulletHit( args )
local attacker = args.attacker
local damage = args.damage

print( string.format( "You have taken %i damage at the hands of %s", damage, attacker:GetName() ) )
end

Events:Subscribe( "LocalPlayerBulletHit", LocalPlayerBulletHit )

As for network events:
Code: [Select]
-- Server
function PlayerChat( args )
-- Sends your authentication status to you and you only
if args.text == "/test" then
Network:Send( args.player, "TestMessage", args.player:IsFullyAuthenticated() )
return false
-- Sends your authentication status to everyone in the server
elseif args.text == "/test_broadcast" then
Network:Broadcast( "TestMessage", args.player:IsFullyAuthenticated() )
return false
-- Sends your authentication status to everybody near you (Everyone streamed in), but not you
elseif args.text == "/test_nearby" then
Network:SendNearby( args.player, "TestMessage", args.player:IsFullyAuthenticated() )
return false
end
end

function ClientMessage( args, sender )
print( sender:GetName() .. "'s head position is " .. tostring(args) )
end

-- Subscribe to the PlayerChat JC2-MP event
Events:Subscribe( "PlayerChat", PlayerChat )

-- Subscribe to the ClientMessage network message
Network:Subscribe( "ClientMessage", ClientMessage )

-- Client
function TestMessage( args )
-- Print out the value we got from the server
print( "Player authentication status: " .. tostring( args ) )
-- Send out a position of our own
Network:Send( "ClientMessage", LocalPlayer:GetBonePosition( "ragdoll_Head" ) )
end

-- Subscribe to the TestMessage network message
Network:Subscribe( "TestMessage", TestMessage )

This code is untested, but should work. Network:SendToPlayers sends the message to a table of Players so that you can easily send network messages to a group of people like a team.