Modify String that is passed as parameter, is it possible?

Started by eri0o, Sat 13/02/2021 23:14:31

Previous topic - Next topic

eri0o

I want to do something like:

Code: ags
function modifyString (String s)
{
  // some way to modify the string
  s = s.Append("world");
}


And then run it like
Code: ags
 String str = "hello ";
  modifyString(str);
  Display(str);


Unfortunately the above gives me "hello " instead of "hello world". Is there a way in AGS to modify a String in place? The problem I have is my function has to return additional information - an integer with some status information. Unfortunately, I can't return a pointer to struct that contains the String since this would be pointer of pointer... So if I could modify the string in place it would be perfect.

So far my only solution is to return the String and have the function receive a pointer to a struct that has the int so it can modify the integer in place instead...

Crimson Wizard

AGS Strings are immutable, and this is why all of the modifying functions return new String.

you use them like
Code: ags
s = s.Append("more");


If you write your own functions then the use method should be similar.
Code: ags
String modifyString (String s)
{
  // some way to modify the string
  return s.Append("world");
}

Code: ags

  String str = "hello ";
  str = modifyString(str);
  Display(str);

eri0o

Ah, ok, thanks! Then it's settled!  :)

I ended up going with the route

Code: ags
managed struct Result
{
  int value;  
};


String modifyString (String s, Result* res)
{
  // some way to modify the string
  res.value = 5;
  return s.Append("world");
}


And run like
Code: ags
String str = "hello ";
  Result* res = new Result;
  str = modifyString(str, res);
  Display(str);


Got the job done, so it's alright! (it has more lines but that is the spirit, it works, I am happy!)

SMF spam blocked by CleanTalk