see if a text is still valid

Posted By: FoxHound

see if a text is still valid - 05/31/14 20:43

In a while loop I want to see if a text is still valid. So far the only way I have found to see if this is true is with this

while(ptr_for_handle(handle(my_text)) != NULL)

ptr_for_handle is a medium speed function and if I can help it I do not want to use it in a while loop, handle says it is fast but that still seems like a lot of waster resources here.

(my_text) (my_text != NULL) do not work.

The text is removed with ptr_remove()

Any faster ideas then what I have tried?
Posted By: 3run

Re: see if a text is still valid - 05/31/14 20:52

you could still check for it in a while loop but not every frame, take a look at 'total_frames' f.e.
in older aums (ai tutorials) this idea was used to shoot bullets at player.


greets
Posted By: Uhrwerk

Re: see if a text is still valid - 05/31/14 20:57

ptr_for_handle(handle(my_text)) is pointless. It will crash if my_text is a vagabonding pointer.

The clean way is to set all pointers to a specific element to null. Then you can check in the other code if its valid (non-null) or removed (null).
Posted By: FoxHound

Re: see if a text is still valid - 05/31/14 21:47

When I set the my_text to NULL and in the other function check to see that it isn't null, the function doesn't end. I know this as I have sys_exit set after the function so it can't be missed.
Posted By: Uhrwerk

Re: see if a text is still valid - 05/31/14 21:56

Then you did something wrong in your code. Here is a working example for you:
Code:
#include <acknex.h>

TEXT* myText = NULL;

void waitForMyTextToBecomeNull()
{
	while (myText)
		wait(1);
		
	sys_exit("e voila...");
}


void main()
{
	myText = txt_create(1,1);
	waitForMyTextToBecomeNull();

	wait(-2);

	ptr_remove(myText);
	myText = NULL;
}

Posted By: FoxHound

Re: see if a text is still valid - 06/01/14 16:19

You're code does work, even when I pass the text pointer through the function. Prehaps my storing the text with handle has something to do with it. Here is the code that concerns this.

Click to reveal..

//////////////////////////////
// this function checks to see if the var has changed, and if it has we alter the string in the text to match it
//////////////////////////////
void monitor_for_var_change(TEXT* loc_text)
{
while(loc_text)
wait(1);

sys_exit("");
}

Yes I Have made sure that skill_y holds the text. when the function runs the panel and the text both disappear. I simply need the function itself that checks for the text to end so I do not have pointer errors (I removed code that had nothing to do with the problem and I removed that from my own code to see if it might be the issue.

void destory_foxpan(PANEL* fox_pan)
{

TEXT* mytext = ptr_for_handle(fox_pan.skill_y);


//////////////////////////////
// we remove the text
//////////////////////////////
ptr_remove(mytext);
mytext = NULL;


//////////////////////////////
// we remove the panel
//////////////////////////////
ptr_remove(fox_pan);

}
Posted By: Uhrwerk

Re: see if a text is still valid - 06/01/14 16:24

You're testing a local variable for a change. The local variable however won't change until you modify it in the corresponding function. You can either use a global variable instead or pass a pointer to the pointer to the function and check the variable this way.
Posted By: FoxHound

Re: see if a text is still valid - 06/01/14 16:57

How am I using a local variable? I am assigning the pointer to a pointer and modifying it. This can be seen as when I destroy the text, it disappears from my screen.

If you can show an example of where I can keep the pointer in the skill of the panel and make this work it would be great, otherwise it seems a struct is my best hope.
Posted By: Uhrwerk

Re: see if a text is still valid - 06/01/14 17:45

Here is a working example for you that uses a pointer to the pointer:
Code:
#include <acknex.h>

void waitForThePointerToBecomeNull(void** p)
{
	while (*p)
		wait(1);

	sys_exit("e voila...");
}

TEXT* myText = NULL;

void main()
{
	myText = txt_create(1,1);
	waitForThePointerToBecomeNull(&myText);
	
	wait(-2);
	
	ptr_remove(myText);
	myText = NULL;
}

Posted By: FoxHound

Re: see if a text is still valid - 06/01/14 19:03

Still not working. However I did get it to work a different way by having the destory function set the skill_y of the text to 0 and tell the wait function to end if the skill y is zero. Not happy with it but it works.
Posted By: Uhrwerk

Re: see if a text is still valid - 06/01/14 19:22

So you destroy the panel, set the panels skill_y and then read that skill_y in another function?
Posted By: FoxHound

Re: see if a text is still valid - 06/01/14 19:35

I set the skill_y to zero and then destroy the text.
Posted By: Uhrwerk

Re: see if a text is still valid - 06/01/14 19:53

Then make sure you wait one frame inbetween... If you want to do it the straigthforward way i've shown you I'm still willing to help. Just post the code that is giving you trouble.
Posted By: FoxHound

Re: see if a text is still valid - 06/01/14 21:49

What I am making is an editable digit. Right now you can not click on digits in our to manually edit them. So my plan is to make a panel to lay on top of another panel. The new panel is only a button. Put a text in front of that. When the button is pressed inkey is used to edit the string of the text. When we tell that panel to go bye bye it takes the panel and text with it.

Beside the pointer issue, and possible it is the same issue, is that this will only work once. If I try to make more than one of these it will crash when the button is pressed. However if there is just one it works fine. However for the editor I am working on I will probably need 1000.


Here is all the main code

Quote:

//////////////////////////////
// this creates the button panel and text to lay on top of it allowing for editable text
//////////////////////////////
PANEL* create_foxpan(var pos_x,var pos_y,PANEL* mother_panel,BMAP* click_bmap,var* loc_display_var)
{

//////////////////////////////
// we create the panel and give it a button so it can adjust the string and display var
//////////////////////////////
fox_pan = pan_create("",mother_panel.layer + 1);

//////////////////////////////
// we set our position to our mother panel
//////////////////////////////
fox_pan.pos_x = mother_panel.pos_x + pos_x;
fox_pan.pos_y = mother_panel.pos_y + pos_y;

//////////////////////////////
// our bmap is our button
//////////////////////////////
pan_setbutton(fox_pan,0,0,
0,0,click_bmap,click_bmap,click_bmap,NULL,adjust_string,NULL,NULL);


//////////////////////////////
// we create the text and set it's string to that of the var we will display
//////////////////////////////
fox_text = txt_create(1,mother_panel.layer + 2);//1 higher than the butotn panel
str_cpy((fox_text.pstring)[0], "123");
str_for_num((fox_text.pstring)[0],*loc_display_var);

//////////////////////////////
// we set our position to our mother panel
//////////////////////////////
fox_text.pos_x = mother_panel.pos_x + pos_x;
fox_text.pos_y = mother_panel.pos_y + pos_y;

//////////////////////////////
// we record pointers to the panel
//////////////////////////////
fox_pan.skill_x = handle(mother_panel);//loc_display_var;//handle(loc_display_var); //handle(mother_panel);////loc_display_var;
fox_pan.skill_y = handle(fox_text);

//////////////////////////////
// we record pointers to the text
//////////////////////////////
fox_text.skill_x = handle(mother_panel);
fox_text.skill_y = loc_display_var;

//////////////////////////////
// we make the panel and the text visible
//////////////////////////////
set(fox_pan,SHOW);
set(fox_text,SHOW);


//////////////////////////////
// we return our panel pointer
//////////////////////////////
return(fox_pan);
}




//////////////////////////////
// this funciton is called when the button for the fox pan is pushed letting us know we want to change the string and thus the display number
//////////////////////////////
void adjust_string(var button_number,PANEL* panel_pointer)
{

//////////////////////////////
// we declare a string so we can use it to hold the old string
//////////////////////////////
STRING* old_string = "#3";


//////////////////////////////
// we get the pointer for the text we were using
//////////////////////////////
TEXT* temp_text = ptr_for_handle(panel_pointer.skill_y);

//////////////////////////////
// we copy the old string in case we need to put it back
//////////////////////////////
str_cpy(old_string,(temp_text.pstring)[0]);


//////////////////////////////
// we also check to see how we ended the key
//////////////////////////////
var key = inkey((temp_text.pstring)[0]); // wait until [enter] pressed

//////////////////////////////
// we get the var for the number
//////////////////////////////
var new_number = str_to_num((temp_text.pstring)[0]); // vpi == 3.142

//////////////////////////////
// we make sure we entered a number and not a letter
//////////////////////////////
if(new_number == 0)
{


//////////////////////////////
// we copy the original string back to this one
//////////////////////////////
str_cpy((temp_text.pstring)[0],old_string);


//////////////////////////////
// get it right!
//////////////////////////////
printf("must be a number");

}
else
{

//////////////////////////////
// we changed our number so now we need to update or display number
//////////////////////////////
var* loc_temp = temp_text.skill_y;
*loc_temp = new_number;


}




}

//////////////////////////////
// this function checks to see if the var has changed, and if it has we alter the string in the text to match it
//////////////////////////////
void monitor_for_var_change(TEXT** loc_text, var* loc_display_var)
{
//////////////////////////////
// we get the old value of the var
//////////////////////////////
var old_var = *loc_display_var;
// while(ptr_for_handle(handle(loc_text)) != NULL) //while(loc_text == NULL) //while(1)
while(loc_text)
{

//////////////////////////////
// this is for when we are done with the text
//////////////////////////////
if(loc_text.skill_x == 0)
{
sys_exit("");
return;
}

//////////////////////////////
// if our old var was changed then we update the string in the editble text for the fox pan
//////////////////////////////
if(old_var != *loc_display_var)
{
old_var = *loc_display_var;
str_for_num(*(loc_text.pstring)[0],*loc_display_var);
}

wait(1);
}
sys_exit("");
}

void destory_foxpan(PANEL* fox_pan)
{
TEXT* mytext;
mytext = ptr_for_handle(fox_pan.skill_y);

mytext.skill_x = 0;
wait(1);
//////////////////////////////
// we remove the text
//////////////////////////////
ptr_remove(mytext);
mytext = NULL;


//////////////////////////////
// we remove the panel
//////////////////////////////
ptr_remove(fox_pan);

}





Here is how I start up the foxx pans

Quote:

temp_pan_0 = create_foxpan(0,0, mother_pan_show_0, button_to_push_bmap, display_number_0);
temp_text_0 = ptr_for_handle(temp_pan_0.skill_y);
monitor_for_var_change(ptr_for_handle(temp_pan_0.skill_y), display_number_0);


Here is how I destory the fox_pans

Quote:

while(1)
{
if(key_y)
{
destory_foxpan(temp_pan_0);
// return;
while(key_y)wait(1);
}
}


I was planning on making this a user contribution so posting the code is cool.
Posted By: FoxHound

Re: see if a text is still valid - 06/17/14 20:55

I redeveloped the fox pan using a struct and in the end I base if the foxpan needs to be removed by if the mother panel is removed or not. I get the same thing. Only if a handle and ptr_for_handle.

It works but when I put 100 panels with one foxpan on each and nothing else on the screen then the FPS drops to 80.

Once I get this to not be such a FPS eater I will put it in user contributions.

If it matters I use A7 latest.


The setup
Quote:
create_fox_pan(0,0,0,0,mother_pans[a],bmap_create("click_button.png"),values[a]);


The header file
Quote:

#ifndef fox_pan_h
#define fox_pan_h

typedef struct fox_panel {


PANEL* mother_pan;

var* *mother_panel_handle;

PANEL* click_panel;

TEXT* see_string_text;

var* adjustable_var;


var offset_x;
var offset_y;

var text_offset_x;
var text_offset_y;

} fox_panel;




//////////////////////////////
// this is used to create a fox panel and returns the fox panel as a string
//////////////////////////////
fox_panel* create_fox_pan(var pos_x,var pos_y, var text_offset_x,var text_offset_y, PANEL* mother_panel, BMAP* click_bmap,var* loc_display_var);

//////////////////////////////
// this funciton is called when the button for the fox pan is pushed letting us know we want to change the string and thus the display number
//////////////////////////////
void adjust_string_fox_pan(var button_number,PANEL* panel_pointer);

//////////////////////////////
// this keeps track of info on the mother panel and transfer it to the fox panel
//////////////////////////////
void fpan_monitor_function(fox_panel* fp, var* loc_display_var);

//////////////////////////////
// we get rid of the fox panel and everything about ti
//////////////////////////////
void destory_foxpan(fox_panel* fp);

#endif


the main file
Quote:

#ifndef fox_pan_c
#define fox_pan_c


//////////////////////////////
// the fox panel allows for having editable digits on standard panels
// it works by having a mother panel, the main backboard, then creating a panel that only has a button. this button should be invsible or able to look as if it belongs on the backboard
// the function from the button will allow for editing of the text. Pressing enter will set the new number typed in. If letters are typed in it will ignore it.
// If the number is changed outside of the button's function it will be updated.
// if the panel is moved, the button panel and the text will move with it.
//////////////////////////////








//////////////////////////////
// we include our header file
//////////////////////////////
#include <general_functions\fox_pan.h>






//////////////////////////////
// this is used to create a fox panel and returns the fox panel as a string
//////////////////////////////
fox_panel* create_fox_pan(var pos_x,var pos_y, var text_offset_x,var text_offset_y, PANEL* mother_panel, BMAP* click_bmap,var* loc_display_var)
{



//////////////////////////////
// we create the fox panel pointer
//////////////////////////////
fox_panel* temp_fox_panel = malloc(sizeof(fox_panel)); // creates a new SPOT struct at runtime
memset(temp_fox_panel,0,sizeof(fox_panel)); // set the struct content to zero (it's undefined after malloc)


temp_fox_panel.offset_x = pos_x;
temp_fox_panel.offset_y = pos_y;

temp_fox_panel.mother_pan = mother_panel;

// temp_fox_panel.mother_panel_handle = handle(mother_panel);


temp_fox_panel.click_panel = pan_create("",mother_panel.layer + 1);


//////////////////////////////
// we set our position to our mother panel
//////////////////////////////
temp_fox_panel.click_panel.pos_x = mother_panel.pos_x + pos_x;
temp_fox_panel.click_panel.pos_y = mother_panel.pos_y + pos_y;

//////////////////////////////
// our bmap is our button
//////////////////////////////
pan_setbutton(temp_fox_panel.click_panel,0,0,
0,0,click_bmap,click_bmap,click_bmap,NULL,adjust_string_fox_pan,NULL,NULL);


//////////////////////////////
// we create the text and set it's string to that of the var we will display
//////////////////////////////
temp_fox_panel.see_string_text = txt_create(1,mother_panel.layer + 2);//1 higher than the butotn panel

//////////////////////////////
// 10 digits
//////////////////////////////
str_cpy((temp_fox_panel.see_string_text.pstring)[0], "#10");



//////////////////////////////
// we set our position to our mother panel
//////////////////////////////
temp_fox_panel.see_string_text.pos_x = mother_panel.pos_x + text_offset_x;
temp_fox_panel.see_string_text.pos_y = mother_panel.pos_y + text_offset_y;



temp_fox_panel.see_string_text.red = 255;
temp_fox_panel.see_string_text.blue = 0;
temp_fox_panel.see_string_text.green = 0;


temp_fox_panel.adjustable_var = loc_display_var;

//////////////////////////////
// we store the pointer to the fox panel
//////////////////////////////
temp_fox_panel.click_panel.skill_x = handle(&temp_fox_panel);


//////////////////////////////
// we make the panel and the text visible
//////////////////////////////
set(temp_fox_panel.click_panel,SHOW);
set(temp_fox_panel.see_string_text,SHOW);

//////////////////////////////
// we now make the fox panel behave with the mother panel
//////////////////////////////
fpan_monitor_function(temp_fox_panel, *loc_display_var);

//////////////////////////////
// we return our panel pointer
//////////////////////////////
return(temp_fox_panel);





}






//////////////////////////////
// this funciton is called when the button for the fox pan is pushed letting us know we want to change the string and thus the display number
//////////////////////////////
void adjust_string_fox_pan(var button_number,PANEL* panel_pointer)
{

//////////////////////////////
// we declare a string so we can use it to hold the old string
//////////////////////////////
STRING* old_string = "#3";


fox_panel* temp_fp = handle(panel_pointer.skill_x);



//////////////////////////////
// we get the pointer for the text we were using
//////////////////////////////
TEXT* temp_text = temp_fp.see_string_text;//ptr_for_handle(panel_pointer.skill_y);


//////////////////////////////
// we copy the old string in case we need to put it back
//////////////////////////////
str_cpy(old_string,(temp_text.pstring)[0]);


//////////////////////////////
// we also check to see how we ended the key
//////////////////////////////
var key = inkey((temp_text.pstring)[0]); // wait until [enter] pressed

//////////////////////////////
// we get the var for the number
//////////////////////////////
var new_number = str_to_num((temp_text.pstring)[0]); // vpi == 3.142

//////////////////////////////
// we make sure we entered a number and not a letter
//////////////////////////////
if(new_number == 0)
{


//////////////////////////////
// we copy the original string back to this one
//////////////////////////////
str_cpy((temp_text.pstring)[0],old_string);


//////////////////////////////
// get it right!
//////////////////////////////
printf("must be a number");

}
else
{

//////////////////////////////
// we changed our number so now we need to update or display number
//////////////////////////////
*temp_fp.adjustable_var = new_number;

}

}



//////////////////////////////
// this keeps track of info on the mother panel and transfer it to the fox panel
//////////////////////////////
void fpan_monitor_function(fox_panel* fp, var* loc_display_var)
{

//////////////////////////////
// we get the old value of the var
//////////////////////////////
var old_var = *loc_display_var;


//////////////////////////////
// as long as the mother panel is good we are good
//////////////////////////////
while(ptr_for_handle(handle(fp.mother_pan)))
{


//////////////////////////////
// for testing purposes
//////////////////////////////
// loc_mother_pan.pos_x = random(800);
// loc_mother_pan.pos_y = random(800);


//////////////////////////////
// we keep our position the same
//////////////////////////////
fp.click_panel.pos_x = fp.mother_pan.pos_x + fp.offset_x;
fp.click_panel.pos_y = fp.mother_pan.pos_y + fp.offset_y;
//////////////////////////////
// now the text panel
//////////////////////////////
fp.see_string_text.pos_x = fp.mother_pan.pos_x + fp.text_offset_x;
fp.see_string_text.pos_y = fp.mother_pan.pos_y + fp.text_offset_y;


//////////////////////////////
// if the mother panel is invsible then so are we
//////////////////////////////
if(is(fp.mother_pan,SHOW))
{
set(fp.click_panel,SHOW);
set(fp.see_string_text,SHOW);
}
else
{
reset(fp.click_panel,SHOW);
reset(fp.see_string_text,SHOW);
}




//////////////////////////////
// if our old var was changed then we update the string in the editble text for the fox pan
//////////////////////////////
if(old_var != *loc_display_var)
{
old_var = *loc_display_var;
str_for_num( (fp.see_string_text.pstring)[0],old_var);
}


wait(1);
}

destory_foxpan(fp);



}





//////////////////////////////
// we no longer need the fox pan so we get rid of it
//////////////////////////////
void destory_foxpan(fox_panel* fp)
{


//////////////////////////////
// we remove the text
//////////////////////////////
ptr_remove(fp.see_string_text);
fp.see_string_text = NULL;


//////////////////////////////
// we remove the panel
//////////////////////////////
ptr_remove(fp.click_panel);
fp.click_panel = NULL;

//////////////////////////////
// now we get rid of the fox panel itself
//////////////////////////////
free(fp);

}

#endif
© 2024 lite-C Forums