Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (NnamueN, Akow, 1 invisible), 1,421 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
LucasJoshua, Baklazhan, Hanky27, firatv, wandaluciaia
19054 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
keyboard... while (key_a == 1) #280991
07/24/09 20:47
07/24/09 20:47
Joined: Jul 2009
Posts: 36
S
SomebodyNew Offline OP
Newbie
SomebodyNew  Offline OP
Newbie
S

Joined: Jul 2009
Posts: 36
Hello there,

i've been playing around with the tutorial on keyboards and here is a code that i wrote and don't really understand how it works.
If you have a free minute i'd be happy if you could help me out here.

Here is the code:
Code:
////////////////////////////////////////////////////////////////////////////
#include <acknex.h>
#include <default.c>
////////////////////////////////////////////////////////////////////////////
function main();
function enemy();
function spawn_object();
function spam_objects();
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
ENTITY*spaceship;
ENTITY*enemy;
////////////////////////////////////////////////////////////////////////////
function main()
{
   level_load("Test1.wmb");
	camera.x = -250;
	spam_objects();
}

function spam_objects()
{
	while (key_w)
		{
			spawn_object();
			wait(10);
		}
}

function spawn_object()
{
	ent_create("enemy.mdl", vector(0,0,0), enemy);
	wait(1);
}

function enemy()
{
	while(1)
	{
		c_move(my, vector(5,0,0), nullvector, GLIDE);
		wait(1);
	}
}



Now the thing I have a problem with is that pressing w does not create an entity.
Pressing a button should return the value 1. And as long as while receives the value 1 it executes its function over and over again. (which it doesnt)

I've made it work using "on_w" but I really want to understand whats missing here.

Also replacing "while (key_w)" with "while (key_w == 1)" doesn't do anything.


Also writing the following code directly into the main function won't allow me to create an entity using q:
Code:
if (key_q == 1)
{
      spawn_object();
}


Again, it works just fine if you use on_key...

Any suggestions?

Thanks in advance for your help and have a great weekend.

Re: keyboard... while (key_a == 1) [Re: SomebodyNew] #280993
07/24/09 20:54
07/24/09 20:54
Joined: Aug 2008
Posts: 2,838
take me down to the paradise c...
Cowabanga Offline
Expert
Cowabanga  Offline
Expert

Joined: Aug 2008
Posts: 2,838
take me down to the paradise c...
Quote:
here is a code that i wrote and don't really understand how it works.
Hilarious!! grin
How can you write a code and you don't know what it does?? laugh

Re: keyboard... while (key_a == 1) [Re: SomebodyNew] #280994
07/24/09 20:56
07/24/09 20:56
Joined: May 2004
Posts: 1,510
Denmark
Claus_N Offline
Serious User
Claus_N  Offline
Serious User

Joined: May 2004
Posts: 1,510
Denmark
It's because you haven't pressed key_w in the first frame, and therefore the loop in function spam_objects just stops without running even once smile

Edit: You should be using something like this:
Code:
function spam_objects()
{
	while (1)
	{
		if(key_w)
		{
			spawn_object();
		}
		wait(10);
	}
}



Last edited by Claus_N; 07/24/09 20:57.
Re: keyboard... while (key_a == 1) [Re: Claus_N] #281004
07/24/09 21:24
07/24/09 21:24
Joined: Jul 2009
Posts: 36
S
SomebodyNew Offline OP
Newbie
SomebodyNew  Offline OP
Newbie
S

Joined: Jul 2009
Posts: 36
Thanks for clearing that up. It now does exactly what it should do. Thank you.

In case I don't use while(1) to make the engine go through the loops functions all the time I would have to call the loop everytime I want the engine to check if the loop's conditions are true or false, right?


@Cowabanga
Well thats the language barrier right there. To really learn lite-c I write small pieces of code and try until they work (and more important I understand what exactly they do). Of course I ment to say something like "the code does something and I don't understand why it does (or doesn't) what it does".
Sorry if my English isn't that good. Fell free to correct me - I actually appreciate it very much (since its the only way for me to improve).

Re: keyboard... while (key_a == 1) [Re: SomebodyNew] #281008
07/24/09 21:40
07/24/09 21:40
Joined: May 2004
Posts: 1,510
Denmark
Claus_N Offline
Serious User
Claus_N  Offline
Serious User

Joined: May 2004
Posts: 1,510
Denmark
Yes, whenever the condition for the while loop (in your case "while(key_w)") is false, the loop stops, and the next code in your function is being executed.

To see how it works, you could try this instead:

Code:
function spam_objects()
{
	// Wait till key_w is pressed
	while(key_w == 0) {wait(1);}
	
	// Spawn the object
	spawn_object();
	
	// Wait till key_w is not pressed anymore
	while(key_w) {wait(1);}
	
	// Wait till key_w is pressed the second time
	while(key_w == 0) {wait(1);}
	
	// Spawn the object again
	spawn_object();
	
	// And then nothing more happens
}



Quote:
Also replacing "while (key_w)" with "while (key_w == 1)" doesn't do anything.

The condition (key_w) is the same as (key_w != 0) smile

Last edited by Claus_N; 07/24/09 21:41. Reason: typo

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