Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Trading Journey
by howardR. 04/24/24 20:04
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (AndrewAMD, EternallyCurious, Petra, 1 invisible), 764 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
key_press only works every other iteration. #433167
11/25/13 17:56
11/25/13 17:56
Joined: Nov 2006
Posts: 497
Ohio
xbox Offline OP
Senior Member
xbox  Offline OP
Senior Member

Joined: Nov 2006
Posts: 497
Ohio
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);
	}
}


Re: key_press only works every other iteration. [Re: xbox] #433241
11/27/13 01:09
11/27/13 01:09
Joined: Nov 2006
Posts: 497
Ohio
xbox Offline OP
Senior Member
xbox  Offline OP
Senior Member

Joined: Nov 2006
Posts: 497
Ohio
any ideas??

Re: key_press only works every other iteration. [Re: xbox] #433247
11/27/13 07:26
11/27/13 07:26
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
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.


Always learn from history, to be sure you make the same mistakes again...
Re: key_press only works every other iteration. [Re: Uhrwerk] #433306
11/27/13 16:05
11/27/13 16:05
Joined: Nov 2006
Posts: 497
Ohio
xbox Offline OP
Senior Member
xbox  Offline OP
Senior Member

Joined: Nov 2006
Posts: 497
Ohio
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.

Re: key_press only works every other iteration. [Re: xbox] #433309
11/27/13 17:25
11/27/13 17:25
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
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).


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: key_press only works every other iteration. [Re: xbox] #433315
11/27/13 19:25
11/27/13 19:25
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
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.


Always learn from history, to be sure you make the same mistakes again...
Re: key_press only works every other iteration. [Re: Uhrwerk] #433323
11/27/13 23:16
11/27/13 23:16
Joined: Nov 2006
Posts: 497
Ohio
xbox Offline OP
Senior Member
xbox  Offline OP
Senior Member

Joined: Nov 2006
Posts: 497
Ohio
Thanks guys. That did it! laugh


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1