Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Ucfirst() And Ucword() Functions In Game Maker, Just like the PHP functions!
Rating 5 V
JDameron91
post Mar 6 2007, 07:07 PM
Post #1


Member [Level 1]
****

Group: Members
Posts: 53
Joined: 23-February 07
Member No.: 39,103



Hello all! biggrin.gif
I have just finished two scripts in GML(Game Maker Language) that I find to be useful: ucfirst() and ucwords().
If you have been around PHP for anytime, you probably have run across these functions.
Simply put, ucfirst() takes the first letter of a sentence and uppercases it. While upwords() takes the first letter of every word in a sentence and uppercases it. Nice time-saver for stories. wink.gif
This tutorial requires Game Maker, if you do not have it, you can download it at it's site, located here.

First, add a new font. It doesn't matter what kind it is, just be sure to name it "fnt_text" so that this works.
Then, in a script, place this code:
CODE
///////////////////
//ucfirst(text);//
///////////////////
var oldtext, letter, text;//These define what varables we will be using and that they should only stay in this script.
oldtext = argument[0];//This variable now holds the data that the user types in when executing this script.
letter = string_upper(string_char_at(oldtext, 1));//This uppercases the letter at the start of the sentence.
text = string_replace(oldtext, string_char_at(oldtext, 1), letter);//This replaces the lowercase letter with our uppercase one.
return (text);//Tell the script opener what our text now says.
//NOTE: string_char_at() finds a variable at the given spot in a string, in this case we said 1 was the position.

Name that script "ucfirst" without the quote marks. wink.gif

Make another script, name it "ucwords" and put the following in:
CODE
/////////////////////
//ucwords(text);//
/////////////////////
var text, a;//Once again, defining our variables that we should only use HERE.
text = argument[0];//Same as before, our text is now what the person puts in.
text = ucfirst(text);//Use our other script to upppercase the first letter, nice how GM can do that, eh?
for (a = 1; a <= string_length(text); a += 1)/*This makes a new variable called "a", it starts with the value of 1 because 1 is the starting position in strings; It then checks to see if our "a" variable is smaller than the length of our text to go through; And lastly, if it is, we will increase "a" by one.*/
{
    if (string_char_at(text, a) == " ") then//If the letter in the string at our "a" variable's value is a blank spot:
    {
        text = string_insert(string_upper(string_char_at(text, a + 1)), text, a + 1);/*Then put our uppercased    letter in front of      
        that  spot.*/
        text = string_delete(text, a + 2, 1);//Finally, we delete the old lowercased letter from our string.
    }
}
return(text);/*Now that that the string is finished, and the loops is over(a became equal to our text length) we can tell the user what the new text is.*/


Then, make a new object, call it "obj_test" or whatever you want.
In it's Draw event, put this code:
CODE
draw_set_font(fnt_text);
draw_text(32, 16, "here is what the text looks like without ucfirst().");
draw_text(32, 32, ucfirst("Here is what it looks like after ucfirst()."));
draw_text(32, 70, "this is what the string looks like without ucwords().");
draw_text(32, 86, ucwords("here is what it looks like with ucwords()."));
/*You can put whatever text you want in the codes above, the scripts will handle it. You can also put more text in below this line.*/


There you have it, the scripts work. smile.gif

I uploaded the file, to show you how it worked.
And if case the file that I uploaded messes up, here is a link to the file.


If you have any questions, comments or suggestions feel free to PM here on this forum, or email me. smile.gif

This post has been edited by JDameron91: Mar 7 2007, 12:07 AM
Reason for edit: typo in title as per member request

Attached File(s)
Attached File  ucfirstanducwords.zip ( 2k ) Number of downloads: 1
 
Go to the top of the page
 
+Quote Post
friiks
post Mar 6 2007, 08:26 PM
Post #2


Member [Level 1]
****

Group: Members
Posts: 53
Joined: 24-February 07
From: Latvia
Member No.: 39,133



I gave it a quick test and it does it's job. smile.gif
Nice work man! smile.gif
Go to the top of the page
 
+Quote Post
JDameron91
post Mar 6 2007, 08:29 PM
Post #3


Member [Level 1]
****

Group: Members
Posts: 53
Joined: 23-February 07
Member No.: 39,103



Thanks dude, I think I may make another tutorial, or just edit this one with a few more functions. Such as lcfirst/lcwords and a script that wil uppercase only certain letters, or certain characters will be changed. smile.gif
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. [php] Clean Code Functions(5)


 



- Lo-Fi Version Time is now: 12th October 2008 - 09:42 PM