Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Herooftime1000 on Tue 24/12/2013 20:39:39

Title: Trouble with Text.CompareTo
Post by: Herooftime1000 on Tue 24/12/2013 20:39:39
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.
Title: Re: Trouble with Text.CompareTo
Post by: Scavenger on Tue 24/12/2013 21:39:16
Code (AGS) Select

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.
Title: Re: Trouble with Text.CompareTo
Post by: Crimson Wizard on Tue 24/12/2013 21:56:24
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 (http://en.cppreference.com/w/c/string/byte/strcmp).

So:
Code (ags) Select

if(Magicwords.Text.CompareTo("Magica Aqua Fillerupa", false) == 0){
  object[0].Visible = true;
  gCauldrontext.Visible = false;
}
Title: Re: Trouble with Text.CompareTo
Post by: Herooftime1000 on Tue 24/12/2013 22:06:21
Quote from: Scavenger on Tue 24/12/2013 21:39:16
Code (AGS) Select

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.