What is wrong with this very basic script? (counters)

Started by Jack Sheehan, Wed 30/01/2008 20:08:33

Previous topic - Next topic

Jack Sheehan

I'm new to AGS and just trying to start some basic scripting. After following the tutorial for Counters I'm wondering whats
wrong with this section of script:

// room script file

#sectionstart hotspot2_a  // DO NOT EDIT OR REMOVE THIS LINE
function hotspot2_a() {
  // script for Hotspot 2 (Sun): Look at hotspot
int my_counter;
  if (my_counter == 0) {
     Display("Ow.");
   }
   if (my_counter == 1) {
     Display("This really hurts you know");
   }
     if (my_counter < 2) {
       my_counter += 1;
}

#sectionend hotspot2_a  // DO NOT EDIT OR REMOVE THIS LINE

NiksterG

First of all, what's the problem you're having?

Without any information, I'd guess it is because it only gives the first message, and never shows the second. The reason is because every time you look at the hotspot, you inititate a variable with a value of zero, then check if the value is zero of not. Obviously, it will always be zero.

To fix this, just put the declaration at the top of the room script in stead of in the function.

Also, I think you need to have "else if" instead of just "if" for the second boolean statement.
Check out my games! (Not all made with AGS)

OneDollar

#2
Edit: Sorry, I'm being really stupid. I'm tired. Ignore me. :(

Khris

No else ifs necessary here.
But there's a closing bracket missing.

And you don't need a {} block if there's only one command inside.

Code: ags
// room script file

int my_counter;

#sectionstart hotspot2_a  // DO NOT EDIT OR REMOVE THIS LINE
function hotspot2_a() {
  // script for Hotspot 2 (Sun): Look at hotspot
  if (my_counter == 0) Display("Ow.");
  if (my_counter == 1) Display("This really hurts you know");
  if (my_counter < 2) my_counter++;
}
#sectionend hotspot2_a  // DO NOT EDIT OR REMOVE THIS LINE


Btw, my_counter will finally increase to 2 and then the game won't react.
Either add another message or use "if (my_counter < 1)"

NiksterG

Ah, hehe, missing bracket. And here I go trying to figure this out, thinking of all the technical stuff, and it's just a missing bracket. Of course.  :-X

As far as not needing a {} block for single command, that's technically true. However, I have found that blocking code like that feels more organized and is easier to edit if future editing is needed. Then again, some people feel that it clutters their code. It's really the scripter's choice.
Check out my games! (Not all made with AGS)

Khris

The missing bracket was probably the reason why the script wouldn't compile (and I guess that's why the OP posted here), but that's really minor compared to the technical error you've pointed out. :)

As for the {} stuff, it's indeed the scripter's choice. But most beginners don't know about the possibility to omit them.

Jack Sheehan

#6
Thanks guys, I'm really new to this and scripting confuses me. Got a weeks holidays coming up so hopefully i can get to grips with it then. The problem was indeed that it would not compile and now it does. Said technical error is still there and I'm going to try and fix it now.

Edit: Problem Solved, Cheers! This is the final script:

  int my_counter;

#sectionstart hotspot2_a  // DO NOT EDIT OR REMOVE THIS LINE
function hotspot2_a() {
  // script for Hotspot 2 (Sun): Look at hotspot
  if (my_counter == 0) Display("Ow.");
  if (my_counter == 1) Display("This really hurts you know");
  if (my_counter < 2) my_counter++;
  if (my_counter == 2) my_counter = 0 ;
}

Khris

Just a minor pointer: since the last two lines make my_counter alternate between 0 and 1, you can use:
Code: ags
  my_counter = 1 - my_counter;

Jack Sheehan

Quote from: KhrisMUC on Thu 31/01/2008 17:24:19
Just a minor pointer: since the last two lines make my_counter alternate between 0 and 1, you can use:
Code: ags
  my_counter = 1 - my_counter;


What would that do?

Khris


Pumaman

Quote from: NiksterG on Wed 30/01/2008 21:13:15
As far as not needing a {} block for single command, that's technically true. However, I have found that blocking code like that feels more organized and is easier to edit if future editing is needed. Then again, some people feel that it clutters their code. It's really the scripter's choice.

I would tend to agree that always using { } is a good idea. The main reason being that it's quite easy to have some existing code like this:

if (counter == 4)
  Display("hello");

and then decide to add an extra command to it:

if (counter == 4)
  Display("hello");
  counter = 0;

and then spend hours debugging it to work out what's gone wrong :)

Gilbert

Well I'll say it depends on personal coding preference.

When there is only one line of codes after say, a 'if', I always keep it in a single line:
if (coutner == 4) Display("blah!");
So it's still clear if no braces are use. (well, I sometimes even write something like if (a==0) if (b==0) Display("blah!"); but that's another story and is not recommended for people...)
I only use braces when there're multilines of codes.

But yeah, always using the braces can be a good practice. It can reduce hard-to-spot errors.

monkey0506

I usually only put the command on a different line if the conditional is absurdly long. Often when verifying parameters or String data this is the case so I might end up with something like:

Code: ags
if ((str1 == null) || (str1 == "") || (str2 == null) || (str2 == "") || (str1 == str2))
  AbortGame("Invalid parameters supplied to function 'FUNCTION'. Game cannot continue.");


I can appreciate how this could lead into Chris's example though it's not personally an issue for me...unlike some I know how to check my braces. :P

Although I do agree that for new scripters it probably would be best just to always use braces to prevent such errors. Actually it seems kind of funny that I don't given my preference to using parentheses even when unnecessary (i.e., the way shown above instead of "if (str1 == null || str2 == "" || str2 == null || str2 == "" || str1 == str2)"). Again, it all just comes down to preference, and using parentheses even when unnecessary always reassures me that I know how the logic is being processed. ;)

SMF spam blocked by CleanTalk