Jump to content

Howto set replicated variable with unrealscript


Ukill

Recommended Posts

Howto set replicated variable with unrealscript:


Goals : (Create a var, that i can use to enable\disable functions )
1. Set the "var (bool) bTestMode"
2. Change it with exec function
3. If "var (bool) bTestMode" changed on client\server -> replicate value to server\all other clients
4. (optional) Keep the "var (bool) bTestMode" alive between MapChange and when not


Questions:
1. Are my goals possible\logical ?
2. Do I extend the class to the right class? (or can i better choose others ?)


Content:
The scripts does what i did planned, except that it does not change the same variable on other clients.

For my question i created 2 files :
- Rx_Mutator_TestMode.uc
- Rx_Mutator_TestMode_Controller.uc

 

File: Rx_Mutator_TestMode.uc (content)
 

Spoiler

// ***************************************************************************************************************
// * * * * * * * * * * * * * * * * * * * * * * Rx_Mutator_TestMode * * * * * * * * * * * * * * * * * * * * * * * *
// ***************************************************************************************************************
class Rx_Mutator_TestMode extends UTMutator;

var RepNotify Bool bTestMode;

replication
{
    if (bNetDirty)
        bTestMode;
}

function InitMutator(string Options, out string ErrorMessage)
{
    if (Rx_Game(WorldInfo.Game) != None)
    {
    }  
super.InitMutator(Options, ErrorMessage);        
}

function bool CheckReplacement(Actor Other)
{
    if(Other.IsA('Rx_TeamInfo')) 
    {
        Rx_Game(WorldInfo.Game).PlayerControllerClass = class'Rx_Mutator_TestMode_Controller';
    }
return true;
}

 

File: Rx_Mutator_Controller.uc (content)
 

Spoiler

// ***************************************************************************************************************
// * * * * * * * * * * * * * * * * * * * * * * Rx_Mutator_Controller * * * * * * * * * * * * * * * * * * * * * * *
// ***************************************************************************************************************
class Rx_Mutator_TestMode_Controller extends Rx_Controller;

var RepNotify Bool bTestMode;

replication
{
    if (bNetDirty)
        bTestMode;
}

exec function TestModeCheck()
{
    if (bTestMode)
    {
        ClientMessage("TestMode activated",,1);
    }
    else
    {
        ClientMessage("TestMode deactivated",,1);
    }
}       // Check TestMode

simulated function TestModeStart()
{
    if ( (PlayerReplicationInfo.bAdmin) == true )
    {
        bTestMode = true;
        TestModeCheck();
    }
    else
    {
        ClientMessage("First login as admin",,1);
    }
}       // Start TestMode

simulated function TestModeStop()
{
    if ( (PlayerReplicationInfo.bAdmin) == true )
    {
        bTestMode = false;
        TestModeCheck();
    }
    else
    {
        ClientMessage("First login as admin",,1);
    }
}   // Stop TestMode

exec function TestModeToggle()
{
    if ((PlayerReplicationInfo.bAdmin) == true )
    {
        if (bTestMode)
        {
            TestModeStop();
        }  
        else
        {
            TestModeStart();
        }
    }
    else
    {
        ClientMessage("First login as admin",,1);
    }
}       // Toggle (stop/start) TestMode

 

 

I would appriciate any solution.

Edited by Ukill
Link to comment
Share on other sites

  • Totem Arts Staff

Exec functions are usually simulated and called on the client themselves. You would have to call either a reliable, or unreliable 'server' function from one command (The command on the client), then in the actual server command is where you would take that action. Like:

exec function ToggleTestMode() 

{

//Just call for server to try and toggle

ServerToggleTestMode()

}

reliable server function ServerToggleTestMode()

{

  if ((PlayerReplicationInfo.bAdmin) == true ) //Only ever do admin checks on the actual server. 

    {

        if (bTestMode)

        {

            TestModeStop();

        }  

        else

        {

            TestModeStart();

        }

    }

    else

    {

        ClientMessage("First login as admin",,1); 

    }

}

You also don't need TestModeStop(), and TestModeStart() to be simulated, since they should always be toggled on the server or in a realm where they have ROLE_AUTHORITY. 

 

 

Link to comment
Share on other sites

Thanks @yosh56, but i did not succeed, it does set the value, but does not replicate it, i dont know what i am doing wrong. 

I also did try to change the TesetModeStart function : 

  1. function TestModeStart()
  2. {
  3.     if( Role < Role_Authority )
  4.     {
  5.         // if we're a client, Notify/synchronize server
  6.         ServerToggleTestMode();
  7.     }
  8.  
  9.     bTestMode = true;
  10.     TestModeCheck();
  11.  
  12. }   // Start TestMode

 

I got now the following 2 files below :

File: Rx_Mutator.uc (content)

Spoiler

// *****************************************************************************
//  * * * * * * * * * * * * * * * * Rx_Mutator * * * * * * * * * * * * * * * * *
// *****************************************************************************
class Rx_Mutator_TestMode extends Rx_Mutator;

function InitMutator(string Options, out string ErrorMessage)
{
    if (Rx_Game(WorldInfo.Game) != None)
    {
   
    }  
super.InitMutator(Options, ErrorMessage);        
}

function bool CheckReplacement(Actor Other)
{
    if(Other.IsA('Rx_TeamInfo'))
    {
        Rx_Game(WorldInfo.Game).PlayerControllerClass = class'Rx_Mutator_TestMode_Controller';
    }
return true;
}

defaultproperties
{
}

 

 

File: Rx_Mutator_Controller.uc (content)

Spoiler

// *****************************************************************************
//  * * * * * * * * * * * * * Rx_Mutator_Controller * * * * * * * * * * * * * *
// *****************************************************************************
class Rx_Mutator_TestMode_Controller extends Rx_Controller;

var RepNotify Bool bTestMode;

replication
{
    if (bNetDirty)
        bTestMode;
}

exec function TestModeCheck()
{
    if (bTestMode)
    {
        ClientMessage("TestMode activated",,1);
    }
    else
    {
        ClientMessage("TestMode deactivated",,1);
    }
}       // Check TestMode

/* You also don't need TestModeStop(), and TestModeStart() to be simulated, since they should always be toggled on the server or in a realm where they have ROLE_AUTHORITY. */

function TestModeStart()
{
    bTestMode = true;
    TestModeCheck();
}   // Start TestMode

function TestModeStop()
{
    bTestMode = false;
    TestModeCheck();
}   // Stop TestMode

exec function TestModeToggle()
{
    if ((PlayerReplicationInfo.bAdmin) == true )
    {
        if (bTestMode)
        {
            TestModeStop();
        }  
        else
        {
            TestModeStart();
        }
    }
    else
    {
        ClientMessage("First login as admin",,1);
    }
}       // Toggle (stop/start) TestMode

exec function ToggleTestMode()
{
    ServerToggleTestMode();
}   //Just call for server to try and toggle

reliable server function ServerToggleTestMode()
{
  if ((PlayerReplicationInfo.bAdmin) == true )
    {
        if (bTestMode)
        {
            TestModeStop();
        }  
        else
        {
            TestModeStart();
        }
    }
    else
    {
        ClientMessage("First login as admin",,1);
    }
}   //Only ever do admin checks on the actual server.



defaultProperties
{
    bTestMode=false;
}

 

What do i need to add or remove ?

Edited by Ukill
Link to comment
Share on other sites

Are you trying to set bTestMode on every Controller? If so then you're not having a replication issue; you have to iterate through every controller and set the variable on the server, which then replicates to the client for their respective controller. Every player has a separate Controller.

I'm sure you can find an example of iterating through AllControllers in Rx_Game somewhere.

  • Like 1
Link to comment
Share on other sites

 

Successful :)


File: Rx_Mutator_Controller.uc (content)

Spoiler

// *****************************************************************************
//  * * * * * * * * * * * * * Rx_Mutator_Controller * * * * * * * * * * * * * *
// *****************************************************************************
class Rx_Mutator_TestMode_Controller extends Rx_Controller;

var RepNotify Bool bTestMode;

replication
{
    if(bNetDirty)
//    if(bNetDirty && ROLE == ROLE_Authority)
        bTestMode;
}

exec function TestModeCheck()
{   // Check TestMode
    if (bTestMode)
    {
        ClientMessage("TestMode activated",,1);
    }
    else
    {
        ClientMessage("TestMode deactivated",,1);
    }
}

function TestModeToggleSet()
{   // Stop TestModeSet
    if (bTestMode)
    {
        bTestMode=false; //false
    }  
    else
    {
        bTestMode=true; //true
    }
}

exec function TestModeToggle()
{   //Just call for server to try and toggle
    ServerTestModeToggle();
}

reliable server function ServerTestModeToggle()
{
    local Rx_Mutator_TestMode_Controller C;
   
    if (PlayerReplicationInfo.bAdmin)
    {  
        foreach WorldInfo.AllControllers(class'Rx_Mutator_TestMode_Controller', C)
        {
        C.TestModeToggleSet();
        C.TestModeCheck();
        }
    }
    else
    {
        ClientMessage("First login as admin",,1);
    }
}   //Only ever do admin checks on the actual server.

DefaultProperties
{
    bTestMode=false;
}

 

Now i like to find out how i do Keep the variable alive between Map Changes ?
Do i need then to extend to an other class ?

Any idea's @yosh56  or @Agent ? :)

 

Edited by Ukill
Link to comment
Share on other sites

Other than seamless travel, it is absolutely impossible to transfer anything that extends from Actor between levels, because an Actor exists on the level itself. I've been passing certain non-Actor variables such as the connection to the server list through Rx_GameEngine lately, but this isn't at all intended to be used by modders since including an Actor in there literally crashes the game client. I'm sure I could create some sort of interface for this though if it's genuinely useful -- just be aware that however I implement it, it wouldn't accept Actors in any way.

The options available to you are:

  • Write variables to a config file -- this is probably the most straight forward way to do what you want
  • Pass variables as a server travel parameter -- this requires extending Rx_Game and using a config file would generally be cleaner
  • Use seamless travel -- this is not recommended or supported in any way, and could have screwy results since we haven't tested anything with it in a very long time.
  • Like 1
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.

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.

×
×
  • Create New...