key_press only works every other iteration.

Posted By: xbox

key_press only works every other iteration. - 11/25/13 17:56

I have my main function that sets up the selected level, starts playing a background song if it's not already playing, and allows the user to press keys to mute the song, and toggle panels.

For some reason, When I start it up, the main function runs once and everything runs fine, but when I get the second level, the main function is run again, but the keys do not work but when I get to the third level, everything works fine again. It only works every other iteration.

Here is the main function. [yes it's sloppy and not optimized]
Code:
void main(){
	var timerShow = 0;
	// preload in buffer all created models:
	preload_mode = 3;
	// show all errors:
	warn_level = 6;
	// set maximal framerate:
	fps_max = 60;

	// set video parameters:
	//video_mode =12;
	//video_screen = 1;

	video_set(sys_metrics(0), sys_metrics(1), 0, 1);
	//video_set(1366, 768, 0, 1); 
	video_aspect = 1.777;
	video_window(0, 0, 0, "Untitled");
	mouse_map = arrow;
	mouse_mode = 4;
//	level_load(NULL);
	text_outline = 100;
	
	choose->pos_x = (sys_metrics(0) - choose.size_x)/2;
	choose->pos_y = (sys_metrics(1) - choose.size_y)/2;
	blackPan->size_x = sys_metrics(0);
	blackPan->size_y = sys_metrics(1);
	timer_pan->pos_y = sys_metrics(1) - 68;
	//collectPan->size_x = bmap_width("way.pcx") * 2;
	//collectPan->size_y = bmap_height("way.pcx");
	collectPan->pos_x = sys_metrics(0) - 276;
	collectPan->pos_y = sys_metrics(1) -68;
	
	
	
	if(started == 0)
		initalLaunch();
		
	wait_for(initalLaunch);
	
	//set(timer_pan, SHOW);
	mouse_mode = 0;
	// run physX:
	physX_open();
	// freeze the game:
	freeze_mode = 1;
	// load the level:
	level_load(levelStr);
	// wait three frames:
	wait(3);
	// unfreeze the game:
	freeze_mode = 0;
	// wait till player exist:
	heroEnt = ent_create(ball_string, vector(0,0,100), actBall);
	while(!heroEnt){ wait(1); }
	
	while(1){
		
		if(media_playing(background) == 0)
		{
			
			background = media_play("Prince Yeti - coco.mp3", NULL, volume);
		}
		
		if(key_pressed(key_for_str("m")) == 1)
		{
			while(key_pressed(key_for_str("m")) == 1){wait(1);}
			if(volume == 50)
				volume = 1;
			else
				volume = 50;
			media_tune(background, volume,0, 0);
		}
		
		if(key_pressed(key_for_str("t")) == 1)
		{
			while(key_pressed(key_for_str("t")) == 1){wait(1);}
			toggle(timer_pan, SHOW);
		}
		
		if(initial == 0)
		{
			minutes = 0;
			seconds = 0;
			milliseconds = 0;
			initial = 1;
		}
		if(collectChange == 1)
		{
			str_cpy(collectString, "");
			str_cat_num(collectString, "%.0f", collected);
			str_cat(collectString, "/");
			str_cat_num(collectString, "%.0f", needToCollect);
			
			FONT* newFont = font_create("Calibri#40");
			pan_setstring(collectPan, 0, 55, collectPos , newFont, collectString);
			collectChange = 0;
		}
		
		
		// cycle throw all entities:
		for(you = ent_next(NULL); you; you = ent_next(you)){
			// if transparent object, was found:
			if(you.objType == objTrans){
				// check whether we are between camera and player:
				if((you.x + you.min_x < maxv(camera.x, heroEnt.x) + 5 && you.x + you.max_x > minv(camera.x, heroEnt.x) - 5)
				&& (you.y + you.min_y < maxv(camera.y, heroEnt.y) + 5 && you.y + you.max_y > minv(camera.y, heroEnt.y) - 5)){
					// decrease alpha:
					you.alpha = clamp(you.alpha - 50 * time_step, 20, 100);
				}
				else{
					// increase alpha:
					you.alpha = clamp(you.alpha + 50 * time_step, 20, 100);
				}			
			}
		}
		// wait one frame:
		wait(1);
	}
}

Posted By: xbox

Re: key_press only works every other iteration. - 11/27/13 01:09

any ideas??
Posted By: Uhrwerk

Re: key_press only works every other iteration. - 11/27/13 07:26

You know that the main function is called exactly once and that is at the program's start? If you want to reexecute it you have to manually call it, right?
Code:
heroEnt = ent_create(ball_string, vector(0,0,100), actBall);
while(!heroEnt){ wait(1); }

You've got dead code there by the way. The while loop will never be executed. You assigned a value other than NULL to it, so hero_ent will always ne non-NULL. in the next line. Until end_create returns NULL, then you have an endless loop.
Posted By: xbox

Re: key_press only works every other iteration. - 11/27/13 16:05

Yeah I know the main function is only called once, so after every level change I manually call he main function again to set everything up. I don't think that explains why key presses are recognized every other time I call the main function.
Posted By: Superku

Re: key_press only works every other iteration. - 11/27/13 17:25

I suggest you define a new function which you call on level load that only resets the relevant parts.
You can try to use proc_kill to stop all other instances of the main function (i.e. stop all its other loops).
Posted By: Uhrwerk

Re: key_press only works every other iteration. - 11/27/13 19:25

Originally Posted By: xbox
Yeah I know the main function is only called once, so after every level change I manually call he main function again to set everything up.
And that's why you have n running main functions after n level changes all interfering with each other.
Posted By: xbox

Re: key_press only works every other iteration. - 11/27/13 23:16

Thanks guys. That did it! laugh
© 2024 lite-C Forums