[CODE] String synchroniser (server -> client)

Looking for Resources for your mod/project? Want to share some of your creations so others can use them? Post them here!
Post Reply
User avatar
Fused
Contributor
Posts: 658
Joined: Sat Nov 09, 2013 9:47 am
Location: Netherlands
Contact:

[CODE] String synchroniser (server -> client)

#1

Post by Fused » Tue Dec 27, 2016 10:17 pm

Thanks to TZK's brilliance I have found a way to synchronise strings from serversided scripts to clientsided scripts.

You know, you can't send strings as arguments over to clients from the server because it will turn into a pile of poop. I'm sure you made this mistake atleast once, admit it.
So this basically syncs strings so you can get that working. Why? Well think about simulating logbold for example, which means you need to go clientside on people in order for log to work on them.
There's more reasons but that's the biggest one basically.

The code is extremely simple. Thanks to TZK for telling me serverside Cvars sync to clients (and yet they can't do that in reverse with clientside cvars for GetUserCvar, meh).
So it basically saves your string into a dedicated cvar, and grabs it from that in the clientside script. Simple as that.
I also added slots in case you need multiple strings inside a clientside cvar, so have 6 for now (I actually needed 2 myself)

USAGE
int string_send(int player, str string)
Use this to send the string to the specified player(s)
player - the player to receive the string. -1 for everyone.
string - your string.

string_receive (int slot)
Use this to grab the string inside the specified slot
slot - the slot to check for a string.

ACS

Code: Select all

/*
** ----------------------------------------------------------------
** @author : Fused / The Zombie Killer
** @email  : FusedQyou@hotmail.com / bennyboy.private@hotmail.com.au
** @date   : 13/01/2017
** @desc   : String synchroniser
**			 Supports 5 slots (for now)
** ----------------------------------------------------------------
*/

#define STRINGRECEIVER_SIZE 5

bool takenStrings[STRINGRECEIVER_SIZE +1];

function int string_send (str string)
{
	// accumulate a free slot
	int i;
	for (i = 0; i < STRINGRECEIVER_SIZE; ++i)
	{
		if (takenStrings[i] == TRUE)
			continue;
			
		takenStrings[i] = TRUE;
		SetCVarString(StrParam(s:"__packet", d:i), string);
		break;
	}
	
	ACS_NamedExecuteAlways("stringdeleter", 0, i);
	return i;
}

function str string_receive (int slot)
{
	return GetCVarString(StrParam(s:"__packet", d:slot));
}

script "stringdeleter" (int slot)
{
    delay(1);
	takenStrings[slot] = FALSE;
	SetCVarString(StrParam(s:"__packet", d:slot), "");
}

CVARINFO

Code: Select all

server noarchive string __packet0 = "";
server noarchive string __packet1 = "";
server noarchive string __packet2 = "";
server noarchive string __packet3 = "";
server noarchive string __packet4 = "";
server noarchive string __packet5 = "";

Example code

Code: Select all

Script "StringSync" (int string1, int string2)
{
	int slot1 = string_send(-1, string1);
	int slot2 = string_send(-1, string2);
	ACS_NamedExecuteAlways("StringCatcher", 0, slot1, slot2);
}

Script "StringCatcher" (int slot1, int slot2) CLIENTSIDE
{
	str string1 = string_receive(slot1);
	str string2 = string_receive(slot2);
	log(StrParam(s:string1, s:", ", s:string2));
}
Logbold example

Code: Select all

/*
** ----------------------------------------------------------------
** @author : Fused
** @email  : FusedQyou@hotmail.com
** @date   : 13/01/2017
** @desc   : Custom logbold function
** ----------------------------------------------------------------
*/

function void LogBold (str string)
{
	LogData(-1, string);
}

function void LogTo (int player, str string)
{
	LogData(player, string);
}

function void LogData (int player, str string)
{
	// Log to the server first
	// This should only be triggered online. Offline player is also the server.
	if(player == -1 && isMultiplayer())
		Log(s:string);
		
	int slot = string_send(string);
	ACS_NamedExecuteAlways("Log_Main", 0, player, slot);
}

Script "Log_Main" (int player, int slot) CLIENTSIDE
{
	// we only want a certain player to receive it (unless it's -1 for all players)
    if (player != -1 && PlayerNumber() != player)
        terminate;
	
	str logString = string_receive(slot);
	Log(s:logString);
}
My mods
Image Image

My socials
Image Image

Catastrophe
Retired Staff / Community Team Member
Posts: 2558
Joined: Sat Jun 02, 2012 2:44 am

Re: [CODE] String synchroniser (server -> client)

#2

Post by Catastrophe » Wed Dec 28, 2016 1:40 am

Very nice, got any integer synchronizers handy? I'd like to see a standard way of doing it.

User avatar
Fused
Contributor
Posts: 658
Joined: Sat Nov 09, 2013 9:47 am
Location: Netherlands
Contact:

Re: [CODE] String synchroniser (server -> client)

#3

Post by Fused » Wed Dec 28, 2016 5:33 pm

Catastrophe wrote:Very nice, got any integer synchronizers handy? I'd like to see a standard way of doing it.
Integer synchronizers? Those work fine without synchronising, so I'm not sure what you mean with that.

Anyways, I added an LogBold example. It's really that simple. Although you should probably add a log for the server to it and also LogTo(), but I'm lazy so I'll just do it when I have time to work on Zombie Horde or something.
My mods
Image Image

My socials
Image Image

User avatar
Fused
Contributor
Posts: 658
Joined: Sat Nov 09, 2013 9:47 am
Location: Netherlands
Contact:

Re: [CODE] String synchroniser (server -> client)

#4

Post by Fused » Fri Jan 13, 2017 9:58 am

Updated the synchroniser.
It now gives you the slot it saved the string at rather than asking for one. This should fix all the string replacing if you used the same slot twice before taking the synchronised string, as long as you added enough packets yourself. You will also need to grab the string in the same tic in order to make this work.

This should be added to ACSutils in the future when zan 212 support is added.

Also, since GetUserCvar is fixed now, it will be possible to sync client -> server now. I'll see about doing that sometime.

EDIT: Updated Logbold aswell.
Now used the updated sync, and also added a LogTo function for those that want it.
My mods
Image Image

My socials
Image Image

Post Reply