I am using some code to centre some given text, then parse the new string back to Lua so it can send the new message to the destination (the game console of a player, or players). When I use this code, I always get formatting errors of weird characters appearing on the right side of the console. I have been told that this is a string memory error but the person that told me this doesn't know how to fix this.
The string is cleared before we put the new message into it. I have tried with and without the console colour symbols (a $ followed by an integer of 0-9), and the same problem persists. With normal non-centred console messages, I do not get this problem. See the image for an example.
Code:
int CScriptBind_GameRules::CentreTextForConsole(IFunctionHandler *pH, const char *msg)
{
if (msg)
{
const int linelength=113;
char newmsg[linelength+1];
for(int i=0;i<linelength;i++)
newmsg[i]=0;
for(int i=0;i<linelength;i++)
newmsg[i]=' ';
int msglen=strlen(msg);
int startpos=linelength/2-msglen/2;
for(int i=msglen-1;i>=0;i--)
newmsg[startpos+i]=msg[i];
return pH->EndFunction(newmsg);
}
else
{
CryLog("[System] Error in CScriptBind_GameRules::CentreTextForConsole: Failed to align");
return pH->EndFunction();
}
return pH->EndFunction();
}
To Send:
CMCCPlayer(player, "================================================================================================================");
CMCCPlayer(player, "$4#### ### ### ###### ####");
CMCCPlayer(player, "$4## ### ### ## ## ##");
CMCCPlayer(player, "$4## ### ### ## ##");
CMCCPlayer(player, "$4## ### ##### ###### ### ##");
CMCCPlayer(player, "$4## ### ### ## ##");
CMCCPlayer(player, "$4## ### ### ## ## ## ");
CMCCPlayer(player, "$4#### ### ### ###### ####");
CMCCPlayer(player, "================================================================================================================");
CMCCPlayer(player, "$4");
CMCCPlayer(player, "$4Your Name: "..player:GetName());
CMCCPlayer(player, "$4Your Country: "..Omega.GetCountry(player));
CMCCPlayer(player, "$4Your Domain:"..player.actor:GetHostname());
CMCCPlayer(player, "$4Your Profile ID: "..ProfId(player));
if not player.exp then player.exp = 0 end
CMCCPlayer(player, "$4Your EXP: "..player.exp);
CMCCPlayer(player, "$4");
CMCCPlayer(player, "$4o0o");
And the function:
function CMCCPlayer(player, msg)
g_gameRules.game:SendConsoleMessage(player.id, g_gameRules.game:CentreTextForConsole(msg));
end
My impression is that you do not end your strings properly, so you exhaust your console width, which displays some "too long" indicating characters.
Try a
newmsg[linelength] = 0;
before the line with the msglen initialization, if you want to fill the full 113 characters line, otherwise add the zero at
startpos + msglen
Drop the funny loop which zeroes everything in the message area, as this is rewritten with space characters anyway. Use rather
startpos=(linelength-msglen)/2
to minimize truncation errors.