string "special" functions

Started by kantor_98, Wed 16/03/2011 16:42:24

Previous topic - Next topic

kantor_98

I need to use some more "deep" function concerning the string isues (my child like a lot the wheel of fortune game, so I try to make one).
1. I want to have the possibility to search a  consonant  inside a string called SENTENCE. For that, I have imagined that I can use the function "substring"
if (list.Substring (i, 1) == "S")
than do somenthing or something, inside a cycle (while i< 10.....i++).
But I obtain error, when I try to use that "indexed" use of i.
How can I solve this, inside a cycle ? I can do it the hard way, an "if" for each letter in the sentence, but I imagine there can be something less brutal
2. More, I like to have the letter to be searched stored in a string variable, like TO_BE_SEARCHED. Is that possible ?

If I can solve this 2 issues in some "indexed call" mode, the things will me more easy later.
Thank you !  

PS I am using the last version of AGS

Khris

Weird, I was sure there's a String.Contains function but I couldn't find it in the online manual on the spot (I'm at work).
I can even remember that in the example, the search string was called needle.

In any case, here's a replacement:

Code: ags
int Search(this String*, char needle) {

  int i;
  while (i < this.Length) {
    if (this.Chars[i] == needle) return i;
    i++;
  }
  return -1;
}


Usage:
  String sentence = "HELLO GUISE";
  int found_at = sentence.Search('L');

found_at is now 2, since the first 'L' is at position 2 (chars of a string are numbered 0 - length-1)

If it isn't found, the function returns -1.

Note that the parameter is a char, with single quote marks.

Since you want to code wheel of fortune, I imagine you'll need a loop like in my function, going through the chars one by one, to find all occurrences. So while my function probably isn't going to be particularly useful, it's a working example of how to go through a string's characters.

Knox

#2
If Im not mistaken, I think it might be this?

IndexOf:

Code: ags


(Formerly known as global function StrContains, which is now obsolete) 
(Formerly known as String.Contains, which is now obsolete)

String.IndexOf(string needle)

Checks to see if NEEDLE is contained within the specified string. Returns the character position of the match if it is, or -1 if it is not.
This function is not case sensitive; ie. testing "test string" for "sTRiN" would match.

Example: 

String haystack = "The haystack had a needle in it somewhere.";
int result = haystack.IndexOf("a needle");

if (result == -1) {
  Display("The string didn't contain the needle.");
}
else {
  Display("a needle was found starting at character %d in the string.", result);
}

See Also: String.EndsWith, String.StartsWith
--All that is necessary for evil to triumph is for good men to do nothing.

kantor_98

Thank you - I will try with the fist proposal, I think it can work.
I will keep informed you about the volution.
BTW - now I find out that "C" is a string and 'S' is a character !!!!!!!

monkey0506

#4
It might be a bit heavier than what you actually need it for, but the StringPlus module (which I wrote :P) has functions like String.IndexOfChar (which does the same thing as Khris' Search function), String.IndexOfNth (for finding the Nth instance of the search needle within a String), and String.ContainsHowManyOf (for finding how many instances of one or more characters (not instances of a String) are contained within a given String).

It might also be useful at some point for me to add functions for finding the number of instances of a whole String within another String and/or the Nth instance of an individual char.

And yes, "C" (with double-quotes) is what we call a "string-literal", whereas 'C' (with single-quotes) would be what we call a "character-literal". It's also important to note that some programming languages support "escape" characters (in either string- or character-literals). AGS does not support escape characters within a character literal, but you can use the following within a string-literal:
"\" - gives a single backslash within a string-literal
""" - gives a single double-quote character within a string-literal
"[" - gives a single opening bracket character within a string-literal (masking the character AGS uses for newlines)
Any other backslash is ignored (so long as it's not the last character in the string-literal, masking the closing quote!).

kantor_98

#5
WOW ! Thank you again - I was just working on that !
Something more, please...
In my language, there are some special characters, called "diacritics". There are usual letters but also the usual letters with some points or cedillas or commas under or on the top of these letters. I have them on my keyboard (when installing windows), but I was not able to display them in on the screen , during the game.
Can anybody tell how can I print some special user-defined characters in AGS (something like "Ş" or "Ă" ?
(as you see, in the forum they are OK displayed !!!!)
When installing the regional keyboard, I have them on the "[];'\" keys.
Thank you again

PS I have searched for
String.ContainsHowManyOf
but I did not found it in the help. I found only IndexOf. Maybe it is hidden in the help ? I try also to write it in the script, but the "automatic helper" did not recognise it. ???

monkey0506

#6
To use that function you will have to import the StringPlus module into your game. If you look you should see that I linked you to it..;) (The forum thread for the module is here)

As for using those special characters, AGS's char type only supports ASCII characters 0-255, and AGS fonts only use the first 128 characters. For displaying other special characters, you can substitute a "normal" character that you might not be using in your game's text (like the brackets [] or braces {}) with a special character, directly in the font. Then in the script you would type the normal character, which the font would render as the special character when displayed (I hope that makes sense..).

I think SSH also made a script module for this purpose, but I could be confusing the purpose(s) of one (or more) of the modules he's written. ::)

kantor_98

tHANK YOU FOR THE QUICK REPLY !
uNFORTUNATELY, i NOT REALLY SUCCEEDED TO MAKE IT. i HAVE ALSO READ THE TOPIC

http://www.adventuregamestudio.co.uk/yabb/index.php?topic=23639.0

I in the national keyboard. I was succesfuly initialised in the global variables a string variable with the word
ACASĂ
But when I try IN THE SCRIPT to write ACASĂ, appear ACASA. I also try to "force" directly the letter Ă in the word (initially i copied from the global variable definition and appear ACASA). But when I display this ACASà (you notice the different Ă - à ; one is written in the global variables, the other one in the script... strange, there was the same key used !), there is displayed ACAS?.
I think just define the diacritics as objects (a brutal method, but....) and use them as an object. It is not very intelligent, but...

Wyz

Yes I've seen this problem many times before. I'll add to my list: "unicode plugin" but for now the work around.

Let's say you replace [ \ ] ` ; with ă î â ș ț and { | } ~ : with Ă ÃŽ Ã, Ș Ț in the font definition use by AGS. Then whenever you use let's say ~ in a script string it will as Ș in game. So you still need to use [ \ ] `etc. in the strings at the scripts. Also beware to put slashes before [ and \, like ĂCASA would become "\[CASA".
I hope this helps you. :)
Life is like an adventure without the pixel hunts.

monkey0506

#9
Yes, I'm sorry if I was not very clear on that point, but within the AGS script files you cannot use any extended characters (only the displayable ASCII characters within the range 32 ((white)space) to 126 (hyphen) are allowed in AGS script files). You can use any of the characters from 1-127 (ASCII value) within a String by using String formatting:

Code: ags
String escapeKeyString = String.Format("%c", 27);


If you then in turn displayed that String on a label for instance, you would have access to the font's representation of the escape key (whatever that may be, or, whatever glyph exists in slot 27 of the font).

You could also put it into a char variable like this:

Code: ags
char escapeKey = 27;


That way if you replace a certain key in the font with a different glyph, you could easily keep track of which character codes translate into which glyphs (in your case, your diacritics). You could then use that char along with the String formatting, like this:

Code: ags
String escapeKeyString = String.Format("%c", escapeKey);


Or you could even use it with an existing String by using the AppendChar function:

Code: ags
String text = "Some text with an escape key glyph at the end ";
text = text.AppendChar(escapeKey);
text = text.AppendChar('!');


I think saying that you have access to the first 128 characters of a font is actually misleading though because ASCII 128 is in the extended character set, and ASCII 0 (the NULL character) is actually used to terminate the AGS String type, so it cannot be inserted into a String..ever. So really you have access, via String formatting, to slots 1-127 in any font.

If you are using a TTF font then a translation text file (a separate text file created by AGS which is then imported back to create the TRA translation file for distribution) then you can use the extended ASCII characters 128-255, which will be displayed according to the corresponding slots in the TTF font, but you still can't type them into an AGS script file.

Just as a point of reference, you can view the ASCII table online, which could help you in creating a custom font file which you could then use in conjunction with String formatting to take advantage of a broader range of characters (specifically the non-displayable characters 1-31 would probably be a good place to insert your diacritics, since they don't have a unique glyph representation anyway and you probably wouldn't need them for anything else).

kantor_98

OK. What I have succeed until now it was that I can recognise the usiual characters, (probably I will be use objects for each letter to be find out, but that is another problem) and I succeeded to create a new font for AGS in which I put the diacritics (capitals) on the desired unused positions ( {}|:" ).
I imported that font, I can display this special ckharacters.

PROBLEM:
I want to create a script in which I want to introduce new puzzles. That means that I MUST introduce thje diacrirics in tha same format as there are in the font that I have created - otherwise there are not recognised.
How can I use  the  current font of AGS in the input text / parse script ?????
Thank you all for your replies ! :D

Khris

The short answer is: you can't.

What you can do is write all the lines in e.g. Word, then replace all diacritics with the respective { or |, then copypasta that into the script editor.

kantor_98

I wanted to make an integrated game, that allow me to change any time the puzzle so I am not anymore determinated by predefined puzzles. More, my kid likes to be the "judge" sometimes, so I want to create him the possibility to introduce, via the software (game) what puzzle he wants.
The solution that you propose is a little bit difficult for a 9 year kid.....

Khris

You could use an external file containing the puzzles that's also editable and expandable from within the game.

I'm not sure how much workarounds would be involved; can you type those special characters inside a game (e.g. using a Textbox on a GUI)?

kantor_98

Well, at least I will try. Both sugestions seems to be more or less OK (I must dig a little bit about importing a file inside the ASGS), but I think is wothing !
Thank you - I will keep you all in touch with my success - or failure !
;D

SMF spam blocked by CleanTalk