crash in my function, why?

Posted By: Muhsin

crash in my function, why? - 06/27/10 14:51

Hi!

I have tried to convert the Stratego 2 code from AUM11 to lite-c. but I get an crash in function "multiple_selection()". I think I have converted it wrong. can someone please help?

here is the original function:
--------------------------------
function multiple_selection()
{
while (1)
{
if (mouse_left == 1)
{
upper_left.visible = on;
upper_right.visible = on;
lower_left.visible = on;
lower_right.visible = on;
if (first_click == 0) // make sure that this "if" branch is executed only once
{
first_click = 1;
upper_left.pos_x = pointer.x; // store panel's position
upper_left.pos_y = pointer.y;
}
lower_right.pos_x = pointer.x; // store panel's position
lower_right.pos_y = pointer.y;

lower_left.pos_x = upper_left.pos_x; // store panel's position
lower_left.pos_y = lower_right.pos_y;

upper_right.pos_x = lower_right.pos_x; // store panel's position
upper_right.pos_y = upper_left.pos_y;

}
else // finished multiple selection
{
upper_left.visible = off;
upper_right.visible = off;
lower_left.visible = off;
lower_right.visible = off;
first_click = 0;

upleft_coords.x = upper_left.pos_x; // project upper_left's panel coords on the map
upleft_coords.y = upper_left.pos_y;
upleft_coords.z = camera_height;
vec_for_screen (upleft_coords, camera);

upright_coords.x = upper_right.pos_x; // project upper_left's panel coords on the map
upright_coords.y = upper_right.pos_y;
upright_coords.z = camera_height;
vec_for_screen (upright_coords, camera);

downleft_coords.x = lower_left.pos_x; // project upper_left's panel coords on the map
downleft_coords.y = lower_left.pos_y;
downleft_coords.z = camera_height;
vec_for_screen (downleft_coords, camera);

downright_coords.x = lower_right.pos_x; // project upper_left's panel coords on the map
downright_coords.y = lower_right.pos_y;
downright_coords.z = camera_height;
vec_for_screen (downright_coords, camera);
}
wait (1);
}
}
--------------------------------

and here is how I converted it:

--------------------------------
function multiple_selection()
{
while(1)
{
if(mouse_left == 1)
{
set(upper_left, SHOW);
set(upper_right, SHOW);
set(lower_left, SHOW);
set(lower_right, SHOW);
if(first_click == 0)
{
first_click = 1;
upper_left.pos_x = mouse_cursor.x; //pointer
upper_left.pos_y = mouse_cursor.y;
}
lower_right.pos_x = mouse_cursor.x; //pointer
lower_right.pos_y = mouse_cursor.y;

lower_left.pos_x = upper_left.pos_x;
lower_left.pos_y = lower_right.pos_y;

upper_right.pos_x = lower_right.pos_x;
upper_right.pos_y = upper_left.pos_y;
}
else
{
reset(upper_left, SHOW);
reset(upper_right, SHOW);
reset(lower_left, SHOW);
reset(lower_right, SHOW);
first_click = 0;

upleft_coords.x = upper_left.pos_x;
upleft_coords.y = upper_left.pos_y;
upleft_coords.z = camera_height;
vec_for_screen(upleft_coords, camera);

upright_coords.x = upper_right.pos_x;
upright_coords.y = upper_right.pos_y;
upright_coords.z = camera_height;
vec_for_screen(upright_coords, camera);

downleft_coords.x = lower_left.pos_x;
downleft_coords.y = lower_left.pos_y;
downleft_coords.z = camera_height;
vec_for_screen (downleft_coords, camera);

downright_coords.x = lower_right.pos_x;
downright_coords.y = lower_right.pos_y;
downright_coords.z = camera_height;
vec_for_screen (downright_coords, camera);

}
wait(1);
}
}
--------------------------------

can someone please help?

thanks!

- Muhsin Kaymak
Posted By: Rackscha

Re: crash in my function, why? - 06/27/10 17:05

try this:

comment out everythng INSIDE the function.

And then put it in step by step.
Posted By: Muhsin

Re: crash in my function, why? - 06/27/10 18:16

I found out that if I comment out this part:
-----------
upleft_coords.x = upper_left.pos_x;
upleft_coords.y = upper_left.pos_y;
upleft_coords.z = camera_height;
vec_for_screen(upleft_coords, camera);

upright_coords.x = upper_right.pos_x;
upright_coords.y = upper_right.pos_y;
upright_coords.z = camera_height;
vec_for_screen(upright_coords, camera);

downleft_coords.x = lower_left.pos_x;
downleft_coords.y = lower_left.pos_y;
downleft_coords.z = camera_height;
vec_for_screen (downleft_coords, camera);

downright_coords.x = lower_right.pos_x;
downright_coords.y = lower_right.pos_y;
downright_coords.z = camera_height;
vec_for_screen (downright_coords, camera);
-----------
then the engine stops working, but if I comment out everything else and don't comment that part, then the crash happens.

what can be wrong?

thanks!

- Muhsin Kaymak
Posted By: Rackscha

Re: crash in my function, why? - 06/27/10 18:22

so, the part above is the part with the crash reason?
(your sentence is a bit confusing^^)
Posted By: Muhsin

Re: crash in my function, why? - 06/27/10 18:27

yes and sorry for my bad english
Posted By: Joozey

Re: crash in my function, why? - 06/27/10 19:41

How did you define the vectors? They may not be pointers.
Posted By: Muhsin

Re: crash in my function, why? - 06/27/10 20:54

here is the full code I converted:

http://dl.dropbox.com/u/5394772/stratego%20demo.rar

please try it and help me fix the problem.

thanks!

- Muhsin Kaymak
Posted By: Widi

Re: crash in my function, why? - 06/27/10 21:01

VECTOR* unit_speed = 2; --> ???
VECTOR* upleft_coords; --> here you define a pointer to a vector and not the vector itself.
VECTOR upleft_coords; --> use this instead.

EDIT: remove all "*" by all VECTOR`s and it no more crashes


Posted By: Muhsin

Re: crash in my function, why? - 06/27/10 21:49

thank you very much. it works now!

thanks!

- Muhsin Kaymak
Posted By: Joozey

Re: crash in my function, why? - 06/28/10 21:06

Do you know why? It's important to know wink.
When random crashes occur, you can be pretty sure it's a pointer issue.
For everyone else having the same problem, here explanation:

You defined the vectors as pointers, but pointers do not yet point to the actual object. You either need to make an object and assign the pointer:
Code:
VECTOR *upleft_coords;
upleft_coords = vector(0, 0, 0);


Or dont make a pointer:
Code:
VECTOR upleft_coords;



When you do not make a pointer (latter example), be absolutely sure you do NOT read the values before setting them:
Code:
VECTOR upleft_coords;
my.x = upleft_coords.x;
//this will definitely cause troubles which are not easy to find!
//the vector has not yet assigned a value, and holds random pointer values.
//using those will cuase these strange values to flow into your functions ;)



This is the correct way:
Code:
VECTOR upleft_coords;
vec_set( upleft_coords, nullvector );
my.x = upleft_coords.x;


© 2024 lite-C Forums