char* to String* [solved]

Started by Lt. Smash, Thu 06/03/2008 18:15:45

Previous topic - Next topic

Lt. Smash

Hi,
I have this line inside a function:
Code: ags
StrCopy(Tmode[mode].name, name);

and I want to convert it to new style-strings

name is String.
Tmode[mode].name is a char array. Exactly:
Code: ags
char name[200];


Code: ags
Tmode[mode].name=name;
doesn't work. Error: "cannot assign to cursormode::name". I don't know what I typed but one time I got: "cannot convert char* to String*"

so my question is: How can I translate this old code into new one?
Hope someone can help me.

ps: this is code from the foa-template and I'm using AGS 3.0.1.29.

naltimari

Have you tried to change 'char name[200]' for 'String name' inside your struct? Then the assignment will surely work.

If you dont want to change it, AFAIK there's no way to circumvent StrCopy, because you can not assign a new-style String to a char array without copying every single position (which is what StrCopy does).

Lt. Smash

I actually was looking for a solution without changing the char array but since there is no easy way for that, I will keep that String.
Many thanks!

naltimari

Well, if you think it's worth the trouble, here's an idea:

Code: ags

struct yourstruct {
  char buffer[200];
  import function copyStringToBuffer(String s);
};

function yourstruct::copyStringToBuffer(String s)
{
  int i = 0;
  while (i < s.Length && i < 199) {
    this.buffer[i] = s.Chars[i];
    i++;
  }
  this.buffer[i] = 0; // put an 'end-of-string' in last char of the array
}


Then, in your code, you'll do:

Code: ags

String s = "my new style string";
structInstance.copyStringToBuffer(s);


Anyway, it's an approach like StrCopy, with the difference that you are going to use new-style strings, and can check 'enforce new-style strings' in the preferences...

Pumaman

What error were you getting originally? If "name" is a String, and "Tmode[mode].name" is a char[200], then you should be able to use StrCopy without a problem.

Lt. Smash

Quote from: Pumaman on Thu 06/03/2008 20:04:14
What error were you getting originally? If "name" is a String, and "Tmode[mode].name" is a char[200], then you should be able to use StrCopy without a problem.
Originally there was no problem. The error only occoured when I check "enforce new-style strings". Because I have a mix of old and new style functions, I want to make all to be new-style.


SMF spam blocked by CleanTalk