Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   replacetext() (https://forums.graalonline.com/forums/showthread.php?t=82820)

DustyPorViva 11-18-2008 10:07 PM

replacetext()
 
I tried Tig's replacetext, but I couldn't get it to work, so I had to create my own. I thought I'd post it here in case anyone wanted to use it.

PHP Code:

function replacetext(txt,a,b) {
  if (
txt.pos(a)<0) return txt;
  
temp.txtpos txt.positions(a);
  
temp.newtxt txt.substring(0,txtpos[0]);
  for (
temp.i=0;i<txtpos.size();i++) {
    
newtxt @= b;
    
newtxt @= txt.substring(txtpos[i]+a.length(),txt.substring(txtpos[i]+a.length()).pos(a));
  }
  return 
newtxt;


Functions simply via:

temp.test = replacetext("Hello\n \"world how\"\n\n are you?","\n",",");

will return Hello, "world how",, are you?(sorry I stole your thing, Tig!)

I don't know how efficient it is, but I got it working so I was happy with it.

[email protected] 11-18-2008 10:13 PM

i don't get it =o

Clockwork 11-18-2008 10:17 PM

Quote:

Originally Posted by [email protected] (Post 1443264)
i don't get it =o

Lets you use quotes and such in chat :o

Chompy 11-18-2008 10:24 PM

Quote:

Originally Posted by [email protected] (Post 1443264)
i don't get it =o



PHP Code:

function onCreated() {
  
temp.str "epic_underscores^4_life";
  
temp.str replacetext(str"_"" ");  // replace _ with " " (space)
  
temp.str replacetext(str"^"" "); // replace ^ with " " (space)

  
echo(str); // "epic underscores 4 life"



DustyPorViva 11-18-2008 10:28 PM

Quote:

Originally Posted by [email protected] (Post 1443264)
i don't get it =o

Used to replace text within a string. I originally needed it because I was reading lists of weapons from the WEAPONS folder, and I needed to replace the %057 and such with the appropriate characters.

Tigairius 11-18-2008 10:31 PM

Great job Dusty, I don't really like my replacetext() function anyways. This is probably much better.

[email protected] 11-18-2008 10:54 PM

ooooh coool =D

cbk1994 11-18-2008 11:46 PM

Good job. :)

Codein 01-12-2009 04:43 PM

Just what I needed, cheers :D

Cubes 06-19-2009 04:32 PM

I cannot for the life of me get this to work serverside. I was trying to use it to replace text in a .gmap file. If I trigger serverside -> clientside ->serverside it works but it lags the client for a moment.

PHP Code:

function onActionServerside()
{
  if(
params[0] = "test")
  {
    
temp.testload.loadlines("levels/gmap/temptest.gmap");
    
temp.testload2 replacetext(temp.testload"rofltest""loltest");
    
temp.testload2.savelines("levels/gmap/lolxdd.gmap"0);
  }
}
function 
replacetext(txt,a,b) {
  if (
txt.pos(a)<0)
    return 
txt;
  
temp.txtpos txt.positions(a);
  
temp.newtxt txt.substring(0,txtpos[0]);
  for (
temp.i=0;i<txtpos.size();i++)
  {
    
newtxt @= b;
    
newtxt @= txt.substring(txtpos[i]+a.length(),txt.substring(txtpos[i]+a.length()).pos(a));
  }
  return 
newtxt;
}

//#CLIENTSIDE
function onPlayerChats(){
  
temp.tokens player.chat.tokenize();
  switch(
temp.tokens[0])
  {
    case 
"test":
        
triggerserver("gui"name"test");
      break;
  }



DustyPorViva 06-19-2009 05:20 PM

loadlines will return an array, I believe, which is the reason what you are doing is not working. You'd need to do something like...
PHP Code:

temp.testload.loadlines("levels/gmap/temptest.gmap");
for (
temp.i=0;i<temp.testload.size();i++) {
  
temp.testload[i] = replacetext(temp.testload[i], "rofltest""loltest");
}
temp.testload.savelines("levels/gmap/lolxdd.gmap"0); 

Also, you can just use shared.replacetext(x,y,z) instead, as you don't need to add the function.

Cubes 06-19-2009 05:35 PM

I couldn't get the shared.replacetext to work serverside but it would work clientside just fine without looping through it. I'd just trigger back and fourth but it causes to much lag renaming a 30 by 30 gmap and freezes the client for like 2 seconds.

DustyPorViva 06-19-2009 05:37 PM

NPC-server probably doesn't have write-rights to the file. Also, have you debugged any? Made sure testload is actually returning anything, to make sure it's not the replacetext? Do some echoes and stuff first.

Cubes 06-19-2009 06:39 PM

Oh I fixed it by doing what you said and I went to let you know but the forums went down again.

Skyld 06-19-2009 08:56 PM

shared.replacetext() will not work serverside because it is a part of the clientside function pack; this is a weapon added to players when they log in and pass through the login server, but there is no way for the serverside portion of the script to be carried across servers too.


All times are GMT +2. The time now is 11:35 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.