Author Topic: strReplace  (Read 2341 times)

How do you use strReplace correctly?

I was trying to make it so when a client says my and some of my friends' names the chat would recolor our names. The problem is that I can't figure it out. If someone could show me a script that uses strReplace other than Chat Emotes, that would be very helpful.


strReplace

Syntax:

Code: [Select]
%sourceString = strReplace(%sourceString, %originalChar, %newChar);

Explanation:

Quote
%sourceString: The string you're replacing characters in.
%originalChar: The characters you're replacing in the source string.
%newChar: What you're replacing the original characters with.

Example:

Code: [Select]
%myString = "Hello World!";
%myString = strReplace(%myString, "o", "Q");
echo(%myString); // Prints "HellQ WQrld!"

Code: [Select]
%myString = "Hello World!";
%myString = strReplace(%myString, "o", "Q");
echo(%myString); // Prints "HellQ WQrld!"

Would that echo "HellQ WQrld!" into the console or the string of your chat?

That was a stupid question of me to ask. I'll just try re-packaging the one I failed on. Thanks for the help Truce.
« Last Edit: May 23, 2009, 06:13:42 PM by gamefandan »


Didn't work.
How isn't it working, whats going wrong? You can't just post those two words and expect people to somehow help you.

Code: [Select]
function stringstuff(%sourcestring, %originalchar, %newchar)
{
%originalchar = "Hi";
%newchar = "Hai";
%sourcestring = strReplace(%sourcestring, "Hi", "Hai");
}
package stringtest
{
function servercmdmessagesent(%client, %msg)
{


messageall('',"\c7"@%client.clanprefix@"\c3"@%client.name@"\c7"@%client.clansuffix@"\c6:"@%sourcestring(%msg));
}
Parent::serverCmdMessageSent(%c,%msg);
};
activatepackage(stringtest);

There's the script I tried, it didn't work. What did I do wrong? :(

Eep, you really have no idea about the structure of programming. I think you should go check out some of the tutorial stickies in this forum.

Eep, you really have no idea about the structure of programming. I think you should go check out some of the tutorial stickies in this forum.

No need, I can handle scripting just fine. All I really need is a good example of a script that effectively uses strReplace.

No need, I can handle scripting just fine. All I really need is a good example of a script that effectively uses strReplace.

Trust me, you can't. You're trying to access local variables that exist in another function and you're not even returning a value from that. You even have a function call outside of a function, it's just sitting in a package. You're also trying to use a variable as a function. You're also using the non-existant %c in your invalid function call. This is so badly written infact that the interpreter should have been shot for not giving syntax errors (if you even tried to run it).
« Last Edit: May 24, 2009, 12:52:21 PM by Ephialtes »

Couldn't you just create another variable inside the new servercmdmessagesent thing?
Code: [Select]
package stringtest
{
function servercmdmessagesent(%client, %msg)
{
%replace = strReplace(%msg, "Hi", "Hai");

messageall('',"\c7"@%client.clanprefix@"\c3"@%client.name@"\c7"@%client.clansuffix@"\c6:"@%replace);
}
Parent::serverCmdMessageSent(%client,%msg);
};
activatepackage(stringtest);

I think that might work, but I didn't really spend much time on that. Cue Ephialtes:

Trust me, you can't. You're trying to access local variables that exist in another function and you're not even returning a value from that. You even have a function call outside of a function, it's just sitting in a package. You're also trying to use a variable as a function. You're also using the non-existant %c in your invalid function call. This is so badly written infact that the interpreter should have been shot for not giving syntax errors (if you even tried to run it).

Oops. Thanks for the help Ephi, I must have put %c instead of %client by accident. :P

Found a script, I'll unlock if I need more help.
« Last Edit: May 24, 2009, 01:51:57 PM by gamefandan »