[.96] Not a bug : Both scripts should work equally

Bugs that have been investigated and resolved somehow.

Moderator: GZDoom Developers

Forum rules
Please don't bump threads here if you have a problem - it will often be forgotten about if you do. Instead, make a new thread here.
Post Reply
Costja
Posts: 188
Joined: Mon Oct 18, 2004 3:58 pm
Location: Russia, Moscow
Contact:

[.96] Not a bug : Both scripts should work equally

Post by Costja »

Both of this scripts should work equally (because they don't contain async statements), but the first script gives you 15 health points and the second script gives you 5 health point.
Spoiler: script 1
Spoiler: script 2
Last edited by Costja on Sat Jun 11, 2005 6:13 am, edited 1 time in total.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49230
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Post by Graf Zahl »

The first script does exactly what is to be expected. The second, however, suffers from synchronization issues. ACS_Execute doesn't run the executed script itself. It just queues the script for execution in thew next tic. But AFAIK the last one queued is the first one executed so they are run in order 258, 257, 256, 255.

In the end both suffer from a simple programming error: What you want is probably:

Code: Select all

script 255 (void)
{
   If(i == 0) {
      GiveInventory("HealthBonus", 5);
      Print(s: "You got a snack!");
      i++;
   }
   else If(i == 1) {
      GiveInventory("HealthBonus", 5);
      Print(s: "You got another snack!");
      i++;
   }
   else If(i == 2) {
      GiveInventory("HealthBonus", 5);
      Print(s: "You got another snack!");
      i++;
   }
   else If(i >= 3) {
      Print(s: "They ran out!");
   }
}
Without the 'else's all if branches are executed.
Costja
Posts: 188
Joined: Mon Oct 18, 2004 3:58 pm
Location: Russia, Moscow
Contact:

Post by Costja »

Thanks for explanation of ACS_Execute special.

These scripts aren't my (they are from this thread). I simply didn't understand why second script "works".
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49230
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Post by Graf Zahl »

Because the queued scripts are executed in reverse order, as I said.

if (i>=3)... first, then
if (i==2), then
if (i==1), then
if (i==0)

And logically then these side effects won't show.
User avatar
Bio Hazard
Posts: 4019
Joined: Fri Aug 15, 2003 8:15 pm
Location: ferret ~/C/ZDL $
Contact:

Post by Bio Hazard »

why not just

Code: Select all

#include "zcommon.acs"

int i=0;
script 255 (void){
	if(i<3){
		GiveInventory("HealthBonus",5);
		if(i==0){Print(s:"You got a snack!");}
		else{Print(s:"You got another snack!");}
		i++;
	}else{Print(s:"They ran out!");}
}
?
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49230
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Post by Graf Zahl »

That works as well.
Post Reply

Return to “Closed Bugs [GZDoom]”