working function but with unexpected result (solved)

Started by riseryn, Fri 14/12/2007 15:36:11

Previous topic - Next topic

riseryn

Hi

my function works but results are unexpected.
Code: ags

function Weighing()
{
int i = 0;

int w_left = 0;
int w_right = 0;
int big_w = 0;


while(i<5){

if (ball[i].side == 1)
{w_left += ball[i].weight;}

if (ball[i].side == 2)
{ w_right += ball[i].weight;}
i++;
}

big_w = Compareweight( w_left,  w_right);
Display("Left %d", w_left);
Display("Right %d", w_right); 
return big_w;
}	

ihave five balls
ball 1 to 4     ball.weight =1
ball 5            ball.weight =3


my problem
imagine a roberval balance where B1 B2.. are  ball[1].weight  ball[2].weight ..
 
|_B1_B2_|       |_B3_B5_|
        |___________|

first time calling function
    (2)                          (4)

No problem result expected

second time

|_B3___|       |___B5_|
        |___________|

results sometimes
   (1)                       (7)
sometimes
(3)                         (3)


Any idea  what I'm doing wrong?

Thanks

Khris

Without seeing the rest of the code, I can only imagine why it doesn't work. The function looks fine.
My guess is you don't set the unused balls' .side to 0.

Also make sure you don't confuse number and index array.
Remember that B1 = ball[0] and B5 = ball[4].

riseryn

Thanks for your reply.

I have check all my code million times.
by default side =0;
only selected ball have side 1 or 2.

all my functions are in global script and imported in room script file.

Basically I click on hotspot to attribute a side and a ball.
one hotspot for each side and a global int for each side.
left GI 46 and right GI 47

the code when interacting each ball is:
(exemple with ball[4])

Code: ags


//in room script file
if(GetGlobalInt(46) == 0  && GetGlobalInt(47) == 0)

{Display("you must select a side first.");}

else{
  
  if(GetGlobalInt(46) == 1)
  {
    ball_side(4,1);    
    SetGlobalInt(47, 0);
    SetGlobalInt(46, 0);
    }
  
  if(GetGlobalInt(47) == 1)
  {
    ball_side(4,2);
    SetGlobalInt(46, 0);
    SetGlobalInt(47, 0);
    }
}   



//in main global script

function ball_side(int ball,int side)
{
ball[ball].side = side;

}

function Compareweight( w_left,  w_right)
{
  if(w_right < w_left){ return 1;}

  if(w_right > w_left){ return 2;}
}



function Weighing()
{
int i = 0;

int w_left = 0;
int w_right = 0;
int big_w = 0;


while(i<5){

if (ball[i].side == 1)
{w_left += ball[i].weight;}

if (ball[i].side == 2)
{ w_right += ball[i].weight;}
i++;
}

big_w = Compareweight( w_left,  w_right);
Display("Left %d", w_left);
Display("Right %d", w_right); 
return big_w;
}	


The last hotspot is used to calculate the weight on each side.

the code when interacting is:

Code: ags


int Bigger;

Bigger = Weighing();

if(Bigger == 1)
{
 Display("Side left."); 
 }
 

if(Bigger == 2)
{
 Display("Side right.");
 
}




everything seems to be ok but.....
:'(



Khris

Wow, you're using arrays and at the same time two global ints to store the side?
Why don't you set GI 46 to either 1 or 2, then do:
Code: ags
  if (GetGlobalInt(46)==0) Display("You must select a side first.");
  else {
    ball_side(4, GetGlobalInt(46));
    SetGlobalInt(46, 0);
  }

If your select-scale-hotspot code looked like this:   SetGlobalInt(4X, 1); it would be possible to add the ball to both sides of the scale. Unless you set the other GlobalInt back to 0 at the same time, of course.
Avoid things like that from the start by only using exactly as much variables as necessary.

The thing is, while the code in general doesn't look faulty, all depends on how you reset the variables before you're testing it a second time.
Did you restart the game or did you use a custom reset function? In the latter case, make sure you've reset every value to 0.

Also, you still haven't posted all the custom code you're using. I'm sorry, but in this case all I can do is guess.

riseryn

#4
Thanks for your help

I've finally solved the problem.
I was so focused on the weight  :-\

Problem was  that I dont reset ball side between two call of the function.

With that reset it works fine.

Thanks for your answers that helps met to think differently of my problem and finally find a solution.

SMF spam blocked by CleanTalk