Jump to content

setElementData or triggerServer/ClientEvent


Recommended Posts

Hello, i doubt with choosing setElementData or triggerServerEvent.

As i can see on source code, setElementData is nothing but "OnElementDataChange" event + triggerServerEvent function made in sourcecode by developers themself.

https://code.google.com/p/mtasa-blue/source/browse/trunk/MTA10_Server/mods/deathmatch/logic/CElement.cpp

void CElement::SetCustomData ( const char* szName, const CLuaArgument& Variable, CLuaMain* pLuaMain, bool bSynchronized, CPlayer* pClient, bool bTriggerEvent ) 
{ 
    assert ( szName ); 
    if ( strlen ( szName ) > MAX_CUSTOMDATA_NAME_LENGTH ) 
    { 
        // Don't allow it to be set if the name is too long 
        CLogger::ErrorPrintf ( "Custom data name too long (%s)\n", *SStringX ( szName ).Left ( MAX_CUSTOMDATA_NAME_LENGTH + 1 ) ); 
        return; 
    } 
  
    // Grab the old variable 
    CLuaArgument oldVariable; 
    const SCustomData * pData = m_pCustomData->Get ( szName ); 
    if ( pData ) 
    { 
        oldVariable = pData->Variable; 
    } 
  
    // Set the new data 
    m_pCustomData->Set ( szName, Variable, pLuaMain, bSynchronized ); 
  
    if ( bTriggerEvent ) 
    { 
       [size=6][b] // Trigger the onElementDataChange event on us 
        CLuaArguments Arguments; 
        Arguments.PushString ( szName ); 
        Arguments.PushArgument ( oldVariable ); 
        Arguments.PushArgument ( Variable ); 
        CallEvent ( "onElementDataChange", Arguments, pClient );[/b][/size] 
    } 
} 

My questions

1)If so, even user can make absolutly the same in lua script using tables+register some event like "onCustomMadeDataChangeEvent"+triggerServerEvent/triggerClientEvent functions ?

2) And main: what should i prefer: ready to use "setElementData" or selfmade but doing exactly the same system with triggerServerEvent/triggerClientEvent?

First desire is to use setElementData becouse it made in source via c++.

BUT!

As we can see here

  
CLuaArguments Arguments; 
        Arguments.PushString ( szName ); 
        [b]Arguments.PushArgument ( oldVariable );[/b] 
        Arguments.PushArgument ( Variable ); 
        CallEvent ( "onElementDataChange", Arguments, pClient ); 

Arguments.PushArgument ( oldVariable ); means we transfer old value EVERYTIME to EVERYCLIENT when element data changed?

So, if so, in selfmade triggerServerEvent + arguments i can send only new value value (skip oldValue), saving some bandwidth in each event, each client.

Someone can say that it is not critical, but it means that we save 50% bandwidth.

Did i understand it correctly?

Thanks!

Link to comment

Some new investigation gives answers :)

Looks like setElementData does NOT transfer oldValue.

https://code.google.com/p/mtasa-blue/source/browse/trunk/MTA10/mods/deathmatch/logic/rpc/CElementRPCs.cpp

void CElementRPCs::SetElementData ( CClientEntity* pSource, NetBitStreamInterface& bitStream ) 
{ 
    unsigned short usNameLength; 
    if ( bitStream.ReadCompressed ( usNameLength ) ) 
    { 
        // We should never receive an illegal name length from the server 
        if ( usNameLength > MAX_CUSTOMDATA_NAME_LENGTH ) 
        { 
            CLogger::ErrorPrintf ( "RPC SetElementData name length > MAX_CUSTOMDATA_NAME_LENGTH" ); 
            return; 
        } 
        SString strName; 
        CLuaArgument Argument; 
        if ( bitStream.ReadStringCharacters ( strName, usNameLength ) && Argument.ReadFromBitStream ( bitStream ) ) 
        { 
            pSource->SetCustomData ( strName, Argument, NULL ); 
        } 
    } 
} 

Its just set new value, then triggers on(Client)DataChange event giving to that event new and old value (on that machine where event called).

I still need confirmation and advice...

Link to comment

Keep talking to myself :)

I prefer to use custom system based upon triggerClientEvent.

I thought that "setElementData VS trigger*Event" is "bandwidth VS CPU" (in case all another things 100% the same - mean in case i really need to sync data with every client does not matter using trigger*event or setElementData), but...

Using setElementData we not only transfer more bytes, we need to process them, so every byte(packet, message?) needs CPU time. So CPU "argument" suffering too)))

Even trigger*Event force u to write your overcode, debug it, maintain it, even the same already done in setElementData its better way to prefer trigger*Event becouse it let u save bandwith+some CPUtime wasting to deal with that packets (in case you really have possibility to do it in your mode - not everytime its possible, but in 99% cases i think...).

I still need confirmation and advice...

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...