Limit the length of save name sentence

Started by Joseph, Mon 28/09/2009 23:18:43

Previous topic - Next topic

Joseph

Hi DUDES...

I wont even pretend I know where to start on this one...so Ill just say what I want to get done, somehow...I figure it has to do with the length property though:

Code: ags

Length property
(Formerly known as global function StrLen, which is now obsolete)

readonly int String.Length;

Returns the length of the string, in characters.
Example: 

String text = "This is my string.";
Display("Length: %d", text.Length);



What Im trying to do is limit the player's save game names to a certain length, like I dont want people to be able to save titles longer than say...10 characters (for example).

How would I go about making this a reality?

PS: I pee in the shower
Spread the love...One.Turtle.At.A.Time.
http://www.youtube.com/watch?v=y0A77rohcyg

RickJ

I think this is what you want ...

Quote from: AGS Manual
String.Truncate(int length)

Returns a version of the string that has been truncated down to length characters.

NOTE: The new string is returned from this funcAtion; it does NOT modify the original string.

Example:

String mystring = "Hello World!";
String truncated = mystring.Truncate(4);
Display("Original: %s, Truncated: %s", mystring, truncated);

will display "Original: Hello World!, Truncated: Hell".

See Also: String.Append, String.Substring

Wonkyth

You could probably do something with ClaimEvent...
"But with a ninja on your face, you live longer!"

monkey0506

ClaimEvent really wouldn't be relevant. ClaimEvent is for preventing other scripts from handling certain events like on_mouse_click and on_key_press.

What Joseph is wanting to do is limit the number of characters available for save game names. So he would want to do something like:

Code: ags
// global script
function repeatedly_execute() {
  if (gSave.Visible) {
    if (txtSavename.Text.Length > 10) txtSavename.Text = txtSavename.Text.Truncate(10);
  }
}


That way if the user tried to type in more than 10 characters into the save name text box, the text would be automatically truncated down to only 10 characters.

Joseph

Ok these are all great tricks...I will try em out.

However, its not exactly what I was meaning, but I think I just thought of somethin that might work:

When the player enters a savegame name (for example I Love Turtles), instead of truncating the I Love Turtles to 10 characters...I will have a dialog pop-up saying "please limit your save names to 10 characters or less"...the reason for this is I dont want characters overlapping over my gui window thing...it can only display 10 characters cause thtas the way I like it!

Ill just do an "if/else" thing to check the length of the inputted text I guess.

I like the truncate (10) command though...and will try that too if I cant get the other way to work.
Spread the love...One.Turtle.At.A.Time.
http://www.youtube.com/watch?v=y0A77rohcyg

monkey0506

You could call truncate and then at the same time call a Display command to let the player know what's happening (as if it's not obvious). Just put braces around both commands and throw it in with the code above.

If you don't truncate the text if it exceeds the limit then you're not actually enforcing the limit and just suggesting it. So the truncate command should be considered more vital than just the Display. ;)

SMF spam blocked by CleanTalk