Trouble with Text.CompareTo

Started by Herooftime1000, Tue 24/12/2013 20:39:39

Previous topic - Next topic

Herooftime1000

function Magicwords_OnActivate(GUIControl *control)
{
if(Magicwords.Text == "Magica Aqua Fillerupa"){
object[0].Visible = true;
gCauldrontext.Visible = false;
}


This is a nice simple code, but I want to make sure the player doesn't have to worry about capitalization. I tried the code...

{
if(Magicwords.Text.CompareTo("Magica Aqua Fillerupa")){
object[0].Visible = true;
gCauldrontext.Visible = false;
}

...but it didn't work. I admit I don't understand how this works. I just would like the player to say the magic words to fill the burning cauldron so that they might choose the spells. How could I get this to work? I'm at a loss biting off more than I can chew.

Scavenger

Code: AGS

function Magicwords_OnActivate(GUIControl *control)
    {
        String words = Magicwords.Text;
        words = words.LowerCase ();
        if(words == "magica aqua fillerupa")
            {
                object[0].Visible = true;
                gCauldrontext.Visible = false;
            }    
    }


This should work if you don't want to worry about capitalisation. Just have the code remove all the capitalisation first. Also you forgot to close some of your brackets, namely the if block. Careful of that.

Crimson Wizard

#2
Modifying string to compare without case is overcomplicated.

http://www.adventuregamestudio.co.uk/wiki/String_functions#String.CompareTo
1) CompareTo function takes a second parameter that lets you to compare case-insensitive.
2) CompareTo returns not bool, but int. Strings are equal if return value = 0 and not equal if return value is either <0 or >0.
This corresponds to traditional C-string comparison.

So:
Code: ags

if(Magicwords.Text.CompareTo("Magica Aqua Fillerupa", false) == 0){
  object[0].Visible = true;
  gCauldrontext.Visible = false;
}

Herooftime1000

Quote from: Scavenger on Tue 24/12/2013 21:39:16
Code: AGS

function Magicwords_OnActivate(GUIControl *control)
    {
        String words = Magicwords.Text;
        words = words.LowerCase ();
        if(words == "magica aqua fillerupa")
            {
                object[0].Visible = true;
                gCauldrontext.Visible = false;
            }    
    }


This should work if you don't want to worry about capitalisation. Just have the code remove all the capitalisation first. Also you forgot to close some of your brackets, namely the if block. Careful of that.

Thank you so much, sir. This is much easier.

SMF spam blocked by CleanTalk