Please help, getting: Error (line 16):Parse error:unexpected if?

Started by BlueAngel, Wed 03/03/2010 07:55:38

Previous topic - Next topic

BlueAngel

I don’t want to open a new topic for this, please look at post 6 for my problem, thanks!


Hi
I’m got an idea for a small game for a friend but the more I look at it the harder it seems. So I’m wondering if someone could be kind a point me in the right direction from the start.  ;)

Game idea is like this:
Player has to get from one end of the room to the other where there is a hotspot to go the next room (end of game).
To “open” (activate) the hotspot he need to remove the obstacle in the way by shooting bullets at the start of the room. The player only got 3 bullet and 3 lives. If he hits a NPC he loses a bullet and a life. After he has hit the hotspot he only got a limit of time to move to the spot. In the middle of the room there is NPC: s moving, if the player collides with NPC: s he loses a life and has to start over again.
There is also a NPC moving behind the hotspot.

So I’m thinking like this:
Some kind of gui for heath (lives)
A timer for hotspot
Characters for the moving NPC: s in the middle, Object or Character for NPC moving in the background, random event, region for moving area.
Player starts with 3 bullet in inventory, use bullet on hotspot for shooting, if/else statement for missing or hitting the hotspot.

The more I think of this, maybe I should use an object for the obstacle (oDoor) then I can use animate object?

All ideas are greatly appreciated.

RickJ

Everything you describe sounds perfectly reasonable and not all that difficult. 

You will be doing most of the scripting in the room's repeatedly execute function and so it can get disorganised rather quickly.  You may find it helpful to make calls to custom functions from there. 
Here is a little framework to get you started.

Code: ags

//=====================================
//  Constant Definitions
//=====================================
#define WALKABLEAREA_EXIT  5

//=====================================
   function BulletHitObject(int id) {
//
// This function returns the id of any object the bullet 
// collides with.  If it does not collide with any object
// it returns a  -1
======================================
}

//=====================================
   function BulletHitCharacter(int id) {
//
// This function returns the id of any character the bullet 
// collides with.  If it does not collide with any character
// it returns a  -1
======================================
}

//=====================================
   int ExitCount = 0;
   import function ExitIsActive(int count=-1);
   function ExitIsActive(int count) {
//
// This function decrements the ExitCount by one each 
// one game cycle until it reaches a value of zero.  The
// count parameter is optional.  ExitCount is set to 
// the value of the optional count parameter if it is specified.
//=====================================
   bool status;

   if (count>=0) {
      ExitCount = count;
      status = true;
   }
   else if (ExitCount>0)  
      ExitCount--;
      status = true;
   }
   else {
      status = false;
   }
   return status;
}


//=====================================
   function room_RepExec() {
//=====================================
   int i, oid, cid;

   // Detect bullet collisions
   i = 0;
   while (i<3) {
      // Get collision
      oid = BulletHitObject(i);
      cid = BulletHitObject(i);
 
      // Respond to collision wilth object 
      if (oid>=0) {
         // Object collission,   do something (i.e. call function)
      }      RemoveWalkableArea(WALKABLEAREA_EXIT); 
   }

      // Respond to collision wilth character 
      else if (cid>=0) {
         // Character collission,   do something (i.e. call function)
      }
      // Respond to no collision 
      else  {
         // No collission,   do something (i.e. call function)
      }
      i++;
   }

   // Activate Exit
   if (ExitIsActive()) {
      RestoreWalkableArea(WALKABLEAREA_EXIT); 
   }
   else {      
      RemoveWalkableArea(WALKABLEAREA_EXIT); 
   } 
}


Another thing to keep in mind is that collisions are detected at the base of objects and characters.   So to make things look right you may wont to fiddle with the z position of the bullets.  This will displace the sprite a pixel distance from the ground, however, it''s base, for the purpose of colision detection, will be the same.

For the hotspot you probably want to use  a region and a walkable area.  In the example above I remove the walkable area if the player is not allowed to exit.  If you want the PC to be able to walk in this area even if he can't exit then you will need to set a variable and then test it's value in the region's interaction (event handler ) function to allow/disallow the exit function.

You probably want to use characters and objects for everything else.

This ought to give you a start anyway.

BlueAngel

thanks for the encouraging words and your help! :D
This will sure get me starting.

Apocalyptic

#3
This sounds like a classic side scroller? You could go this in AGS, but it would take a while. With a program like game maker or construct you could do it a lot quicker and it would be easier. For example to make a character experience gravity in construct its one click  :D
"Imagination is more important than knowledge." Albert Einstein

BlueAngel

This is probably true but I hoping to learn more and more about AGS with every game so I’m sticking to this. :)
If this doesn’t work I will just try one other of my ideas, I really got too many!  ;D

BlueAngel

RickJ or someone please help me, I cant get past this error:
Error (line 16):Parse error:unexpected if
Code: ags

// room script file

#define WALKABLEAREA_EXIT 2

function RubberbandHitObject (int id){ //this function returs the id of any object the rubberband collides with. If it does not collide with any object it returns a -1
}

function RubberbandHitCharacter (int id){ //this function returs the id of any character the rubberband collides with. If it does not collide with any character it returns a -1
}


int ExitCount = 0;
import function ExitIsActive (int count=-1);

bool status;
if(count>=0){
   ExitCount=count;
   status=true;
}
else if (ExitCount>0){
   ExitCount--;
   status=true;
}
else{
   status=false;
}
return status;


at GlobalScript.asc I have placed this:
Code: ags

// main global script file

function ExitIsActive(int count){ //this function decrements the ExitCount by one each one game cycle until it reaches a value of zero.
//the count parameter is optional. ExitCount is set to the value of the optional count parameter if it is specified.
}


and at the end this:

Code: ags


export ExitIsActive (int count)()


If i put the "function ExitIsActive(int count) {" under "import function ExitIsActive(int count=-1);
" like in RickJ example I get another error.

???


Matti

It seems you've put the if-lines into the plain room script, not in a certain event (like rep-ex, room_load etc). Could that be the problem?

EDIT: Ah, I see. Those lines are supposed to be part of the function. In that case they should be placed within the function in the global script, not loosely in the room's script. In the room's script you just import and call functions you declared somewhere else.

This should be in the global script:

Code: ags

function ExitIsActive(int count) {

   bool status;

   if (count>=0) {
      ExitCount = count;
      status = true;
   }
   else if (ExitCount>0)  
      ExitCount--;
      status = true;
   }
   else {
      status = false;
   }
   return status;
}


And - as RickJ already wrote - this in the room's rep-exe:

Code: ags

   // Activate Exit
   if (ExitIsActive()) {
      RestoreWalkableArea(WALKABLEAREA_EXIT); 
   }
   else {      
      RemoveWalkableArea(WALKABLEAREA_EXIT); 
   } 


And, as you already did, this at the beginning of the room's script:

Code: ags

import function ExitIsActive (int count=-1);


Hm, something like that. Going through RickJ's code I'm getting a little confused I have to admit.

Khris

And since Mr. Matti didn't explicitly mention it, I'll add that functions don't have to be exported; importing them is enough.

BlueAngel

Thanks both of you, now I got by that but got one new error
Error (line108):PE04:parse at else
Code: ags

function room_RepExec()
{
int i,  oid,  cid; //Detect bullet collisions
i=0;
while (i<3){ //Get collision
  oid = RubberbandHitObject (i);
  cid = RubberbandHitCharacter (i);
  //respond to collision with object

  if (oid>=0){ //object collission, do something (i.e. call function)
  }
  RemoveWalkableArea(2);
  
  //respond to collision with character
  else if (cid>=0){ //character collission, do something (i.e. call function)
  }

  //respond to no collision
  else{ //no collission, do something (i.e. call function)
    if (player.ActiveInventory(iRuberband_1)||(player.ActiveInventory(iRubberband_2)||(player.ActiveInventory(iRubberband_3))
    Display("You missed!);
  }
  i++;
}

//activate Exit
if (ExitIsActive()){
  RestoreWalkableArea(2);
}
else{
  RemoveWalkableArea(2);
}
}


line 108 is:   else if (cid>=0){ //character collission, do something (i.e. call function)

its too early in the morning, may I just missing a bracket somewhere...  ::)

CShelton

You can't put a command  between an if and an else if.

To use an else if, it has to follow an if's closing curly brace:

Code: ags

if (condition)
{
  Do Stuff
}
// NOTHING CAN GO HERE BUT COMMENTS!
else if (condition)
{
  Do stuff
}


You put RemoveWalkableArea(2) where it doesn't belong. It needs to go after the if...else if section, or needs to be within somebody's curly braces.

I think you missed a closing curly brace just above the i++ as well. Proper formatting will usually show you when something is out of place so use your form!

Also, hitting CTRL-B while highlighting one brace or bracket will show you where it's paired brace or bracket is. It gets confused sometimes, but I use it now an then to find where I missed something.

RickJ

Just to expand a bit upon what CShelton says the curly braces are used to enclose blocks of code.   Without the braces it would only be possible to execute one instruction per if/else.  For example ..l.
Code: ags

function IsZero(int i)  {
     bool status;

     if (i==0) status = true;
     else status = false;

    return status;
}


Now to do more than one thing for each if/else case we need to use curly brackets to enclose the code that goes with each?
Code: ags

bool Global Status;

function IsZero(int i)  {
     bool status;

     if (i==0) {
          status = true;
          GlobalStatus = true;
     }
     else {
          status = false;
          GlobalStatus = false;
     }
    return status;
}


So the generalized structure looks like this ...
Code: ags

     if (condition) {
          code
          :
     }
     else if (condition) {
          code
          :
     }
     else {
          code
          :
     }


I have formatted your code using the Programming Convention that I use.  It is described in the DemoQuest documentation available from this thread linked below.

http://www.adventuregamestudio.co.uk/yabb/index.php?topic=34048.msg443220#msg443220

It's helpful to follow a programming convention but it doesn't much matter which one you follow or if you make up your own as long as you are consistent.  Monkey for example uses a much different programming convention than I do.  The one I use works for me and the one he uses works for him.  You should choose or invent one that works for you.

Code: ags

function room_RepExec() {

     int i,  oid,  cid;

     // Detect bullet collisions
     i=0;
     while (i<3) { 
          // Get collision
          oid = RubberbandHitObject (i);
          cid = RubberbandHitCharacter (i);

           // Respond to collision with object
          if (oid>=0) { 
               // Object collission, do something (i.e. call function)
              RemoveWalkableArea(2);
          }  
          // Respond to collision with character
          else if (cid>=0) { 
              // Character collission, do something (i.e. call function)
          }
          // Respond to no collision
          else { 
               // No collission, do something (i.e. call function)
               if (player.ActiveInventory(iRuberband_1)||(player.ActiveInventory(iRubberband_2)||   
                  (player.ActiveInventory(iRubberband_3)) {
                    Display("You missed!);
               }
          }
     }
     i++;

     // Activate Exit
     if (ExitIsActive()) RestoreWalkableArea(2);
     else RemoveWalkableArea(2);
}


I am sorry if I have ended up explaining the really obvious but sometimes it's a helpful exercise.  Someone can be missing some small point somewhere, not know what, where nor how to ask a question about it and end up totally  messed up.   Anyway hope I have been helpful to you BlueAngel or others lurking about.   ;)

BlueAngel

I’m grateful for all help and don’t mind being told the obvious as I really haven’t grasped the scripting concept yet but hopefully with help from all nice people here I will.
:D
I just hope you will not get fed up with all my questions along the way.
I really try to work it out and search the forum before asking.
I will try all this when I get home.

SMF spam blocked by CleanTalk