Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: barefoot on Thu 31/12/2009 21:53:02

Title: Trying to add a condition to function
Post by: barefoot on Thu 31/12/2009 21:53:02
Hi

I have the below script which works ok. What I am trying to do, and i can't quite pull it off with the manual at the moment, is have a condition where once the isheet  (inv item) is used on the wild boar the boar changes to view 12 (with a sheet on).. (so far so good) but then you can use the ihammer.

This is what i would like to do:

When the boar has the isheet on you can use inv ihammer to stun it..  in other words you can only use the ihammer when the boar is in view 12 (with sheet on)

Am i making this sound complicated?


This is what i have so far:


function cboar_UseInv()
{
if(player.ActiveInventory == isheets) //or whatever the inventory name
  {
      Display("You throw a sheet over its head so it can't see. Now's your channce to do something, but quick, before it shakes it off!!");
     
     cboar.ChangeView(12);
     
     
            }
  else cRedpants.Say("No good using that!");

}


thanks for anyones help on this

barefoot



Title: Re: Trying to add a condition to function
Post by: Dualnames on Thu 31/12/2009 21:56:07
function cboar_UseInv(){

if(player.ActiveInventory == isheets) //or whatever the inventory name    {
       Display("You throw a sheet over its head so it can't see. Now's your channce to do something, but quick, before it shakes it off!!");
         cboar.ChangeView(12);
      player.LoseInventory(isheet);
       return;
}


if (player.ActiveInventory==ihammer) {
//message here if boar doesn't have shee on its head.
if (cboar.View==12) {//or .NormalView
//Do whatever you want to do here.
}
}      
Title: Re: Trying to add a condition to function
Post by: barefoot on Thu 31/12/2009 22:00:40
Hi

thanks for helping... getting a parse error :

GlobalScript.asc(579): Error (line 579): Parse error: unexpected 'if'

which is this line:

if (player.ActiveInventory==ihammer) {

barefoot
Title: Re: Trying to add a condition to function
Post by: NsMn on Thu 31/12/2009 22:05:14
We'll probably need more of the code to see what's wrong here.
Title: Re: Trying to add a condition to function
Post by: barefoot on Thu 31/12/2009 22:15:47
Hi
This is what I have that works (complete)


function cboar_UseInv(){

if(player.ActiveInventory == isheets) //or whatever the inventory name
  {
      Display("You throw a sheet over its head so it can't see. Now's your chance to do something, but quick, before it shakes it off!!");
     
      cboar.ChangeView(12);
     
     
     cRedpants.Say("Yes, Nows my chance to stun it");
     
     
                  }
  else cRedpants.Say("That won't work");

}


PS
This also works but has no 'else if' functions...  like if using something else Display("no good using that);  etc etc

function cboar_UseInv(){

if(player.ActiveInventory == isheets) //or whatever the inventory name
   {
       Display("You throw a sheet over its head so it can't see. Now's your chance to do something, but quick, before it shakes it off!!");
     
       cboar.ChangeView(12);
     
     
 
     
        return;
}


if (player.ActiveInventory==ihammer) {
Display("You can't get near it");
if (cboar.View==12) {//or .NormalView
Display("You hit it with the hammer to stun it");
}
}     

}   

barefoot

barefoot
Title: Re: Trying to add a condition to function
Post by: Dualnames on Thu 31/12/2009 22:47:18

function cboar_UseInv(){

if (player.ActiveInventory == isheets) {//or whatever the inventory name
   Display("You throw a sheet over its head so it can't see. Now's your channce to do something, but quick, before it shakes it off!!");
   cboar.ChangeView(12);
   player.LoseInventory(isheet);
}

if (player.ActiveInventory==ihammer) {
    if (cboar.View==12) {//or .NormalView
       //Do whatever you want to do here.
    }
    if (cboar.View!=12)
    cRedpants.Say("That won't work");
}      

if ((player.ActiveInventory!=ihammer) && (player.ActiveInventory != isheets)) {
cRedpants.Say("That won't work");
}
}



Happy New Year!
Title: Re: Trying to add a condition to function
Post by: Crimson Wizard on Thu 31/12/2009 22:51:56
Argh... poor monkey_05_06... I guess he will die when sees this thread.  ;D

Sorry.
Barefoot, since monkey is not here at the moment, I will take his place and tell you this. It is strongly recommended to

1. Use [ code ] [ /code ] tags (without extra spaces inside []) to encapsulate your code listings in forum posts to make them look better.
2. Use identation to make lines of code of same block level start on same position.

As an example I'll take Dualnames' code:


function cboar_UseInv() {

  if(player.ActiveInventory == isheets) //or whatever the inventory name    {
     Display("You throw a sheet over its head so it can't see. Now's your channce to do something, but quick, before it shakes it off!!");
     cboar.ChangeView(12);
     player.LoseInventory(isheet);
     return;
  }


  if (player.ActiveInventory==ihammer) {
  //message here if boar doesn't have shee on its head.
     if (cboar.View==12) {//or .NormalView
        //Do whatever you want to do here.
     }
  }    
}


As you may notice, this is much more easy and pleasant to read.

By the way, what about putting this hint to some sticky?
Title: Re: Trying to add a condition to function
Post by: barefoot on Fri 01/01/2010 15:12:34
Cheers everyone

Not everthing is as clear cut... but its good to learn new things..

barefoot
Title: Re: Trying to add a condition to function
Post by: monkey0506 on Fri 01/01/2010 20:13:34
@CW: lolwut? :D

Yes as I was just saying elsewhere it is a peeve of mine to see people who don't indent their code or are inconsistent about how they're doing it. :P

Anyway, I just wanted to make a note regarding Dual's most recent code snippet. I don't know much about the internal workings of AGS's compiler so I can't be 100% certain on this, but from a logical standpoint I would say that it stands to reason that using "else" would be faster than having to re-evaluate every one of these conditions.

Consider:

function cboar_UseInv(){
  if (player.ActiveInventory == isheets) {//or whatever the inventory name
    Display("You throw a sheet over its head so it can't see. Now's your channce to do something, but quick, before it shakes it off!!");
    cboar.ChangeView(12);
    player.LoseInventory(isheet);
  }

  if (player.ActiveInventory==ihammer) {
    if (cboar.View==12) {//or .NormalView
      //Do whatever you want to do here.
    }
    if (cboar.View!=12)
      cRedpants.Say("That won't work");
  }

  if ((player.ActiveInventory!=ihammer) && (player.ActiveInventory != isheets)) {
    cRedpants.Say("That won't work");
  }
}


Regardless of the value of player.ActiveInventory this will always have to perform at least 3 conditional checks:

1. player.ActiveInventory == isheets
2. player.ActiveInventory == ihammer
3. player.ActiveInventory != ihammer

AGS supports lazy evaluation so if condition number 2 is true then this condition:

4. player.ActiveInventory != isheets

can be omitted. That is, conditions 1-3 will always be evaluated every time cboar_UseInv() gets called whereas condition 4 may or may not. If player.ActiveInventory == ihammer then as we said condition 4 will be omitted, but there will be two additional conditions which will both be evaluated:

2a. cboar.View == 12
2b. cboar.View != 12

Although it will rarely ever be impacting in any way, from a logical standpoint this redundancy can be resolved rather simply by using the "else" keyword (as barefoot was already doing in the original code). Consider this instead:

function cboar_UseInv(){
  if (player.ActiveInventory == isheets) {//or whatever the inventory name
    Display("You throw a sheet over its head so it can't see. Now's your channce to do something, but quick, before it shakes it off!!");
    cboar.ChangeView(12);
    player.LoseInventory(isheet);
  }

  else if (player.ActiveInventory==ihammer) {
    if (cboar.View==12) {//or .NormalView
      //Do whatever you want to do here.
    }
    else
      cRedpants.Say("That won't work");
  }

  else {
    cRedpants.Say("That won't work");
  }
}


The least number of evaluations this will have to perform is 1 instead of 3 without the elses. The most number of evaluations it will have to perform is 3 instead of 5 without the elses. Surely you can understand that this would be preferable.

Anyway, enough of that for now. Happy new years everybody! :=
Title: Re: Trying to add a condition to function
Post by: Dualnames on Fri 01/01/2010 20:34:44
Correctly, done. I just had this really damn break-nerving experience with 'else' and a 'bracket', that's why I wrote the code in "Checking conditions one by one" style. As for speed, I'm not really sure if that's the reason. I'm writting my code this way, because it's foolproof (prevents fools) and it's also easier to debug for me. Not everybody codes the same way though.