Strange looping

Started by bx83, Sun 25/04/2021 03:23:28

Previous topic - Next topic

bx83

I'm trying to have the Transparency for 12 objects bob between 0 and 100, so they act like lights dimming and growing fuller. Each should have a sin wave pattern: growing stronger, growing weaker, growing stronger, etc.
For this example I've only picked the 10th object.

Everything except the last few lines acts as expected.


The code:

Code: ags
function room_RepExec()
...
  transCtr++;
  //every 5th loop, run this code
  if (transCtr > 5) {
    transCtr=0;
    
    //load out temporarily
    for (int z=0;z<12;z++) {
      stars[z]=object[z+6].Transparency;
      if (z==10) Display("load out: for %d, dir %d trans %d",z, stardir[z], stars[z]);
    }
    
    //do calculations
    for (int x=0; x<12; x++) {
      
      if (x==10) Display("b4 for %d, dir %d trans %d",x, stardir[x], stars[x]);
      
      if (stars[x]+stardir[x]<0) {
        stars[x]=0;
        stardir[x]=1;
      }
      if (stars[x]+stardir[x]>100) {
        stars[x]=100;
        stardir[x]=-1;
      }
      
      stars[x]+=stardir[x];
      if (x==10) Display("a4 for %d, dir %d trans %d",x, stardir[x], stars[x]);
    }
    
    //load back in
    for (int y=0;y<12;y++) {
      object[6+y].Transparency=stars[y];
      if (y==10) Display("%d %d ?",object[6+y].Transparency, stars[y]);
      if (y==10) Display("load back in: for %d, dir %d trans %d",y, stardir[y], stars[y]);
      if (y==10) Display("so %d is now trans %d - matching %d?",y, object[6+y].Transparency, stars[y]);
    }
    
    Display("trans %d", os11.Transparency);
  }
...




Example output:

note: dir, or stardir[counter], is direction - growing weaker or stronger by having 1 or -1 added to transparency.


load out: for 10, dir 1 trans 96 <--load values into array
b4 for 10, dir 1 trans 96          <--before doing calculations
a4 for 10, dir 1 trans 97          <--after doing calculations
98 97 ?                                  <-- is the value of object 10's transparency (that we got the value out of) equal to value 10 of the array of transparency counters, after we made it equal to it? Nope - wtf?
load back in: for 10, dir 1 trans 97   <-- put damaged value back in object 10, etc.
so 10 is now trans 98 - matching 97?
trans 98

load out: for 10, dir 1 trans 98
b4 for 10, dir 1 trans 98
a4 for 10, dir 1 trans 99
100 99 ?
load back in: for 10, dir 1 trans 99
so 10 is now trans 100 - matching 99?
trans 100.  <-- loop doesn't grow weaker

load out: for 10, dir 1 trans 100
b4 for 10, dir 1 trans 100
a4 for 10, dir -1 trans 99
100 99 ?
load back in: for 10, dir -1 trans 99
so 10 is now trans 100 - matching 99?
trans 100

load out: for 10, dir -1 trans 100
b4 for 10, dir -1 trans 100
a4 for 10, dir -1 trans 99
100 99 ?
load back in: for 10, dir -1 trans 99
so 10 is now trans 100 - matching 99?
trans 100

load out: for 10, dir -1 trans 100
b4 for 10, dir -1 trans 100
a4 for 10, dir -1 trans 99
100 99 ?
load back in: for 10, dir -1 trans 99
so 10 is now trans 100 - matching 99?
trans 100

ad infinitum


The problem seem to be, 1 is added always to object[6+y].Transparency on the last 4 Display() lines, but I can't see how. For instance, it's object[10].Transparency=stars[y], not object[10].Transparency+=stars[y], so i don't see how it could have happened.

note: 6+y just means object indexes are 6 more than the counter, so the 10th object (which is actually object 11 out of 12 not using zero index), is object[16]. I included this pointless complexity because I obviously don't know what's the code's doing, and you might :/

????

Crimson Wizard

#1
There's a problem in AGS that for historical reasons it internally stores transparency not as 0-100 value but 0-255 value, and when you ask it back it converts it, sometimes loosing precision.

For that reason it won't always work correctly if you will keep reading Transparency back. Also, for same reason, doing Transparency++ will not work because it will read its value first before adding 1, this may lead to getting stuck at certain number indefinitely.

EDIT: Ah, yes, this mentioned in the manual:
Quote
Some rounding is done internally when the transparency is stored -- therefore, if you get the transparency after setting it, the value you get back might be one out. Therefore, using a loop with object[0].Transparency++; is not recommended as it will probably end too quickly.

bx83

So what do I do? I just want numbers 0..100 stored as they are.

bx83

Also simpler code:

Code: ags
transCtr++;
  if (transCtr > 5) {
    transCtr=0;
    int y=0;
        
    //do calculations
    for (int x=0; x<12; x++) {
      
      y=x+6;
      
      if (x==10) Display("b4 load out: for %d, dir %d trans %d obj %d",x, stardir[x], stars[x], object[y].Transparency);
      stars[x]=object[y].Transparency;
      if (x==10) Display("a4 load out: for %d, dir %d trans %d obj %d",x, stardir[x], stars[x], object[y].Transparency);
      
      if (stars[x]+stardir[x]<0) {
        stars[x]=0;
        stardir[x]=1;
      }
      if (stars[x]+stardir[x]>100) {
        stars[x]=100;
        stardir[x]=-1;
      }
      
      stars[x]+=stardir[x];
      if (x==10) Display("a4 calcs for %d, dir %d trans %d",x, stardir[x], stars[x]);
      
      if (x==10) Display("b4 obj trans %d array %d",object[y].Transparency, stars[x]);
      object[y].Transparency=stars[x];
      if (x==10) Display("a4 obj trans %d array %d",object[y].Transparency, stars[x]);
    }
   
    Display("obj trans %d", object[16].Transparency);
  }

Crimson Wizard

#4
Quote from: bx83 on Sun 25/04/2021 04:13:59
So what do I do? I just want numbers 0..100 stored as they are.

Depends on what do you want to use these numbers for. Perhaps use a separate array of ints to store these. Then every time you change Transparency also assign same number to that array. Then read these numbers back for the array instead of an object.

Another solution, if you are already using custom properties in your project, you could add a custom property, e.g. "Transparency100", and save it there.

SMF spam blocked by CleanTalk