Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: m0rph84 on Tue 28/02/2017 22:40:10

Title: Switch statement
Post by: m0rph84 on Tue 28/02/2017 22:40:10
Hello,
someone could tell me what's wrong with this Switch statement/call function:



function bad_Inventory_use(String objcase)
{

  switch(objcase)
  {
    case "cloud":
        .... //do something
        break;

    case "sun":
        ...... //do something
        break;

    default:
       ..... //do something else
  }


and I call it with:



function oCloud_UseInv()
{
  bad_Inventory_use("cloud");
}



but it always execute the default case....I tried to put a Display("%s",objcase) in the default case
to check the value of objcase and it returns "cloud" as it should be.

Title: Re: Switch statement
Post by: Khris on Tue 28/02/2017 22:49:05
According to the AGS release thread:
Quote- Strings do not work with switch/case script commands as intended. They were supposed to be compared by value, but are instead compared as pointers. This worked in some early BETA versions, but was broken at some point. This hopefully will be fixed in one of the future updates.
Title: Re: Switch statement
Post by: m0rph84 on Tue 28/02/2017 22:55:31

Thank you very much!
Title: Re: Switch statement
Post by: Crimson Wizard on Tue 28/02/2017 23:07:34
Yeah, that was an unfortunate oversight, but Gurok fixed this for future version (3.4.1). At the moment you should be using "if/else if" with strings.
Title: Re: Switch statement
Post by: m0rph84 on Tue 28/02/2017 23:29:39
Ok, not a problem at all.

Thanks again!