Community Forum

Multiplayer-Event for Global object does not work correcly

Forum Overview >> Scripting

Created12.09.2019 20:39

Thomas H. (Patar) 12.09.2019 20:39
Hey guys,

unfortunately that forum is not what it was thought for anymore, but I hope someone can still help me.
I need to create a Multiplayer-Event for a Global Script (no vehicle Specialisation, building or something).

On Clientside everything works fine, but Server is just calling "emptyNew"-Function. As I don't know who called that function (it's not my Event-Script), thats the moment where I can just work with trial and error. Unfortunately I don't get my fault...

After one sendEvent-call on Clientside, there are the following debug-prints in logfile of Client:

2019-09-12 20:21 sendEvent on Event mit object = table: 0x01b8a019c218, item = nil, owner = 99, noEventSend = nil
2019-09-12 20:21 sendEventToServer
2019-09-12 20:21 new on Event
2019-09-12 20:21 emptyNew on Event

I expected to see something similar on Serverside, but there ist just the following in Server-log:
2019-09-12 20:21 emptyNew on Event

Hope someone can still help me here. Thank you in advance...

Regards, Thomas

-- SendMPOwnerEvent

SendMPOwnerEvent = {}

SendMPOwnerEvent_mt = Class(SendMPOwnerEvent, Event)

for k,v in pairs(Event) do
print("[X] "..tostring(k).."("..type(v)..") = "..tostring(v))

InitEventClass(SendMPOwnerEvent, "SendMPOwnerEvent")

function SendMPOwnerEvent:emptyNew()
print("emptyNew on Event")
local self = Event:new(SendMPOwnerEvent_mt)
return self

function SendMPOwnerEvent:new(object, item, owner)
print("new on Event")
local self = SendMPOwnerEvent:emptyNew()

self.object = object
self.item = item
self.owner = owner

return self

function SendMPOwnerEvent:writeStream(streamId, connection)
--NetworkUtil.writeNodeObject(streamId, self.vehicle)
--NetworkUtil.writeNodeObject(streamId, self.object)
--streamWriteBool(streamId, self.doAttach)

function SendMPOwnerEvent:readStream(streamId, connection)
--self.vehicle = NetworkUtil.readNodeObject(streamId)
--self.object = NetworkUtil.readNodeObject(streamId)
--self.doAttach = streamReadBool(streamId)


function SendMPOwnerEvent:run(connection)
print("run on Event")
ChangeBaleOwner:sendMPOwner(self.item, self.owner, true)

if not connection:getIsServer() then
g_server:broadcastEvent(self, false, connection, self.object)

function SendMPOwnerEvent.sendEvent(object, item, owner, noEventSend)
print("sendEvent on Event mit object = "..tostring(object)..", item = "..tostring(item)..", owner = "..tostring(owner)..", noEventSend = "..tostring(noEventSend));
if noEventSend == nil or not noEventSend then
if g_server ~= nil then
g_server:broadcastEvent(SendMPOwnerEvent:new(object, item, owner), nil, nil, object)
g_client:getServerConnection():sendEvent(SendMPOwnerEvent:new(object, item, owner))

Bilbo Beutlin (BBeutlin) 13.09.2019 16:26
Concerning ".. I don't know who called that function (it's not my Event-Script) ..":
It's indeed YOUR script if you call g_server:broadcastEvent(SendMPOwnerEvent:new(object, item, owner), nil, nil, object)
The 'new()' function in turn calls an 'emptyNew()'.

I'm not sure why your debug print "new on Event" doesn't appear. However I've found that 'print()' doesn't work properly in all cases. Seems that console output is supressed in certain functions or instances.

Thomas H. (Patar) 14.09.2019 00:55
Print() works fine for me in every case so far. I never had problems with that function and even on Clientside every debug-print appears as expected. So I'm pretty sure on serverSide 'new()' is never called, but 'emptyNew()' is called.

Quote: "It's indeed YOUR script if you call g_server:broadcastEvent(SendMPOwnerEvent:new(object, item, owner), nil, nil, object)"

Again here, if he does call that function, there had do appear an "broadcastEvent" in log.

After all, my sendMPOwner-Function is not called on Server, so there have to be something wrong in that Event... Do you know what all that parameters for 'g_server:broadcastEvent()' are? Especially that last one, what I used "object" (which is my global Script-table) for?

Bilbo Beutlin (BBeutlin) 14.09.2019 06:42
According to your code: if you send an event by client, the server repeats it as broadcast to all.
I'd suggest instead print()'ing use a global variable which you evaluate later, eg. a bitmask which reports the passed states.

What I found so far about "broadcastEvent(table event, boolean flag, integer connection, table object)"
'event' is the table created by 'new()'
'flag' (don't know exact meaning) mostly unused 'nil', sometimes 'false'
'connection' ID
'object' subject of event - can be vehicle, player, any other object the event refers to

I found broadcastEvent() used with 3 more parameters in 'VehicleEnterRequestEvent'

Thomas H. (Patar) 16.10.2019 19:19
I just found my problem! I thought, writeStream() and readStream() was just used for synchronization at the beginning of the game. But now there is an other Problem. If Client calls

g_client:getServerConnection():sendEvent(SendMPOwnerEvent:new(object, item, owner))

then Server is calling an emptyNew(), readStream() and run(). Thats okay so far!

But if Server starts to use the Event and calls

g_server:broadcastEvent(SendMPOwnerEvent:new(object, item, owner), nil, nil, object)

then Client has no reaction on it. I checked all of that with an overwritten HonkEvent-Script with very much debug-prints.
On that Honk-Event, if Server does a broadcast, then Client is reacting like the Server: emptyNew(), readStream(), run()

I just think it has something to do with the paramters of g_server:broadcastEvent. I tried "false" instead of "nil" as the second paramteter, but its still the same... That line in HonkEvent-Script just look the same, but is working great, while my broadcast won't work...

Bilbo Beutlin (BBeutlin) 17.10.2019 07:05
I'd assume, if you expect an answer or other reaction, the 'broadcastEvent' third parameter 'connection' mustn't be nil, but a valid connection ID.

PinchTheFarmer 15.11.2019 16:57
Where did you find arguments for the function Bilbo Beutlin ? :O
By the way, FLAGs are Enumerate created for differents states to recognize / setup more precisely what you want to do, Network FLAGs are probably these, well for your function it's only a boolean, don't know yet difference between true/false :

Note: Log in to post. Create a new account here.