Jul 20, 2008

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

Free Web Hosting, No Ads > CONTRIBUTE > Tutorials

free web hosting

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

JDameron91
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

 

 

 


Reply

friiks
I gave it a quick test and it does it's job. smile.gif
Nice work man! smile.gif

Reply

JDameron91
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

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. game maker return file position - 20.38 hr back. (1)
  2. ucword php string function - 31.47 hr back. (1)
  3. delphi ucfirst - 52.72 hr back. (2)
Similar Topics

Keywords : ucfirst, ucword, functions, game, maker, php, functions

  1. [php] Clean Code Functions
    Clean up your html output from php scripts (5)
  2. Add Copy To And Move To Functions To Right Click Menu
    (3)
    In this tutorial i will explain how to put Copy to and Move to functions to right click menu... 1.
    Open registry editor...you can open it like this... START->Run...->type "regedit" hit Enter...
    2.find this directory... ->
    "HKEY_CLASSES_ROOT\AllFilesystemObjects\shellex\ContextMenuHandlers" 3.Create a
    new key in this directory... rename it to "Copy To" 4.Change it's value to this-> CODE
    {C2FBB630-2971-11d1-A18C-00C04FD75D13} 5.Create another key in the same directory...rename it to
    "Move To" 6.Change it's value to this-> CODE {....
  3. Simple Parsing Functions
    [Delphi] Tutorial (0)
    Because parsing is such an integral part of string manipulation, I took the time to make a quick and
    simple parse function. For you VBers, Delphi is a derivative of Pascal...it's powerful, simple,
    and (best of all) doesn't need external components (eg: ocx files). Hence the code: CODE
    function TForm1.SimpleParse(MainString, BeginString, EndString: string): string;
    var PosBeginString: integer; PosEndString: integer; begin PosBeginString :=
    Pos(BeginString, MainString) + Length(BeginString); PosEndString ....

    1. Looking for ucfirst, ucword, functions, game, maker, php, functions

Searching Video's for ucfirst, ucword, functions, game, maker, php, functions
advertisement



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



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE