Jump to content
xisto Community
Sign in to follow this  
JDameron91

Ucfirst() And Ucword() Functions In Game Maker Just like the PHP functions

Recommended Posts

Hello all! :(

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. :P

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:

/////////////////////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. :(

 

Make another script, name it "ucwords" and put the following in:

///////////////////////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:

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. :(

 

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. :(

ucfirstanducwords.zip

Edited by JDameron91
typo in title as per member request (see edit history)

Share this post


Link to post
Share on other sites

I gave it a quick test and it does it's job. :P Nice work man! :(

Share this post


Link to post
Share on other sites

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. :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.