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));
}
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);
}