Convert char to string || count elements in dynam.array [SOLVED]

Started by rbaleksandar, Sun 12/09/2010 12:32:27

Previous topic - Next topic

rbaleksandar

Hi, guys.


Read my second post in this thread for more information about my second problem!

 Yet another (dull) question. I looked in the forum but there appear to be thread(s) only about problem converting strings to char.  ;D Here we go:

 I have a function that takes a given string as parameter. Using Overlay (textual) it is supposed to display each character of the given string on the screen one after the other AND the previously displayed characters have to remain on the screen. Right now I'm facing a (very basic) problem that doesn't allow me to use text-Overlay.  :-[

 Example: We have a string TEXT = "Hello!"
 The function has to display TEXT on the screen as follows:
 H
 HE
 HEL
 HELL
 HELLO
 HELLO!

 
 I know how I can do this but I can't find the proper function(s) that goes with the structure String.  :-\

Code: ags


Overlay *textOverlay
textOverlay = Overlay.CreateTextual(x, y, textWidth, Game.NormalFont, 20, TEXT);


 The problem is that the CreateTextual(..., String) takes ONLY a string as parameter (here we have TEXT). The function String.Chars[int] returns a char. Using this I can get every single character in TEXT using a loop and accordingly use it to achieve my ultimate goal. Alas!, since CreateTextual(..., String) takes only a string I have to convert char to string in order to use it with this function. I can't find anything in the manual that resembles a convert-function.  ???

 I hope you understand what I mean and possibly give some kind of backup.  ;)

Cheers!

PS: Easiest way is to make this as an animation but I want to use code instead.


EDIT: Uuuuuuuuuuuups. :D I just realized that

Code: ags

String tempString = "text.Chars[i]";


 works, when you put the "". Problem solved. Sorry for this.  ;D Now I face another problem - converting a char array into string. Is there an equivalent of String.valueOf(char[]) in AGS?
I am a mighty pirate. Arrrrgh!

rbaleksandar

LOL There's no need to ask, when I answer my own questions.

String.AppendChar(char c)

 That's the answer! ;D
I am a mighty pirate. Arrrrgh!

GarageGothic

You solved it while I was writing, but I might as well post this. You  could also have used String.Truncate, like so:

Code: ags
String text = "Hello!";
Overlay *textOverlay;
int i = 1;
while (i < text.Length) { 
  String text_trunc = text.Truncate(i);
  textOverlay = Overlay.CreateTextual(x, y, textWidth, Game.NormalFont, 20, text_trunc);
  Wait(20);
  i++;
  }
textOverlay = Overlay.CreateTextual(x, y, textWidth, Game.NormalFont, 20, text);

rbaleksandar

#3
  Another more scientific looking conversion from a single character to a string representative of this character is to append the character (using the AppendChar()) to an empty string.
btw You pointed out something I forgot and was about to write about - Wait().  :D I was cursing the damn thing why it shows the whole String at once but now I see that it's just because the chars are displayed so fast that it looks like the whole TEXT is displayed. ;)

Thanks for you help. :)


Here is the second problem:

 Let's say we have a function that displays (using Overlay) a text with each character in a different color. It has 2 parameters:
1)String TEXT (the text, which characters we want to display on the screen)
2)int COLORS[] (an array of colors' indices defined by the user that will be used as a palette for the coloring of the characters)

 I've decided to use an array for the following reason: I use the Random()-function to pick up a random index of my array and to assign the color that goes along with this index to the Overlay.CreateTextual(..., int COLOR). Besides, using an array give the opportunity to specify the exact colors you want to use. Imagine you have 3 rooms with red,green and blue background. Using this function to display a fancy colorful text will give you the choice to exclude red, gree or blue according to which room you use it in (blue text is unreadable on a blue background etc.). Since I'll probably use it in more than one room, this is a very handy feature. I want a dynamic array because I don't how many colors I want to use as a color-pool for the Random()-function.
 Right now I'm really confused on how to determine the upper bound (that is - biggest index) of my array so that I can use it with Random().
 I tried the following:

Code: ags

int i = 0;
int count = 0;

while(COLORS[i]!=null){
  count++;
  i++;
}

 It says I can't compare INT with NULL*. Okay, fine and quite normal. So I tried to create an array of integer pointer. Nah, this time I get: "Cannot create pointer to basic type". I searched a little bit in the forums and found this post by RickJ, in which he says that in AGS you can't create pointers to basic types:
http://www.adventuregamestudio.co.uk/yabb/index.php?topic=41280.msg546068#msg546068

Any ideas?
I am a mighty pirate. Arrrrgh!

Khris

At one point you're going to have to decide how many elements the array is going to have; even if you use a dynamic array and set its dimension to a random value, you can store that value for later use. It might not be elegant, but at least it's possible that way (and the only way).

No offense, but you seem to have a tendency to over-complicate stuff a bit :)

Also:
Code: ags
String tempString = "text.Chars[i]";

will definitely not turn a char into a String. Displaying this String, as expected, results in

text.Chars
i]

(since [ is a line break.)

A generic way of putting a char into a string is this:
Code: ags
  String message = String.Format("ASCII code 65 is the letter %c", 65);

rbaleksandar

  So actually dynamic isn't dynamic. :D I guess I'll have to cope with this bitter sip. And besides since I have a limited number of colors in my palette that I can use (with a 256 colors it's quite a reasonable size), it's not such a problem to use a normal array. Yet these are resources that can be used for something else. :) As for the char->string conversion - you're of course right. :D I'll use either Truncate() or AppendChar() (it depends what I need to do exactly).

Thanks a lot for your help. ^^
I am a mighty pirate. Arrrrgh!

monkey0506

No actually dynamic is dynamic.

A static array must have a specific, non-variable, static size defined at the time the array is defined.

A dynamic array must still have an exact size specified, but the size may be variable in value. For example you can use variables like Game.CharacterCount, Game.GUICount, or even a value taken from user-input as the size. The size is still exact at the time that the array's memory is allocated, but it is a non-exact value until that point, when the value of the variable is determined.

rbaleksandar

I am a mighty pirate. Arrrrgh!

SMF spam blocked by CleanTalk