Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by dr_panther. 05/18/24 11:01
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 (dr_panther, Ayumi, 1 invisible), 708 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
beginner problem with loops #449971
04/04/15 09:49
04/04/15 09:49
Joined: Apr 2015
Posts: 29
Germany
Saschaw04 Offline OP
Newbie
Saschaw04  Offline OP
Newbie

Joined: Apr 2015
Posts: 29
Germany
Hello,

I want to make a flashlight for my horror-survival game. The battery of the flashlight should be empty in 300 seconds and flashlight should be automatically go off.
I used this code:


var x = 1;
var battery = 300;

function flashlight(ENTITY* ent){
if(battery>0){
my.ambient = 100;
my.lightrange = 150;
}
while((battery>0)){
wait(-1);
battery = battery - x;
}
my.ambient = 0;
my.lightrange = 0;

}

Action (){
if(key_f) flashlight(me);
}

The Problem is that the flashlight does go off in a few seconds Long before 300 seconds.
I watched the var battery and it fells rapid and not with the value of 1 in 1 second like in the code.

(Sorry my english is not so good - i am german^^)

Can anyone tell me why the flashlight goes off before 300 seconds are gone??


-- started with programming on march 2015 --
-- living in Germany near Dortmund --
Re: beginner problem with loops [Re: Saschaw04] #449972
04/04/15 10:08
04/04/15 10:08
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Hello, Saschaw04! laugh

I'll show how to make a count down timer, and you'll learn from it and adjust your code, so you'll be able to learn tongue
Code:
// 300 seconds
var counter_timer = 300;

void count_down(){

    // while we still have to count down:
    while(counter_timer > 0){

        DEBUG_VAR(counter_timer, 10);

        // count down each second:
        counter_timer -= time_step / 16;

        wait(1);
    }

    beep();
}



Best regards


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: beginner problem with loops [Re: 3run] #449973
04/04/15 10:19
04/04/15 10:19
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
do you execute the flashlight function multiple times?


POTATO-MAN saves the day! - Random
Re: beginner problem with loops [Re: Kartoffel] #449977
04/04/15 11:10
04/04/15 11:10
Joined: Apr 2015
Posts: 29
Germany
Saschaw04 Offline OP
Newbie
Saschaw04  Offline OP
Newbie

Joined: Apr 2015
Posts: 29
Germany
First of all thank you for the answers.

@Kartoffel: Yes flashlight function is in a while Loop and will execute when I press f key.

@3run: I checked it out in a separate scipt and it runs.
But then I checked it out in my own scipt, but there it Counts less then 300 seconds. There it Counts just a few seconds. I could see like the 300 fells to 0 in a few seconds.

Have you a idea, why it does not work in the other script?


-- started with programming on march 2015 --
-- living in Germany near Dortmund --
Re: beginner problem with loops [Re: Saschaw04] #449979
04/04/15 11:21
04/04/15 11:21
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Sorry I should've put it more clearly:

Do you accidently execute your flashlight function multiple times?

It happens if you use something like this for example:
Code:
while(1)
{
   if(key_a)
      do_something();
   
   wait(1);
}


If you hold [A] for 10 frames the function gets called 10 times.
So, if that's the same as your case, you have to make sure that the function gets called only once.

You could assign a fuction to a key and use a toggle var.
Let me know if you need an example.

Last edited by Kartoffel; 04/04/15 11:28.

POTATO-MAN saves the day! - Random
Re: beginner problem with loops [Re: Kartoffel] #449981
04/04/15 11:31
04/04/15 11:31
Joined: Apr 2015
Posts: 29
Germany
Saschaw04 Offline OP
Newbie
Saschaw04  Offline OP
Newbie

Joined: Apr 2015
Posts: 29
Germany
(Jetzt auf deutsch um weitere Missverständnisse zu vermeiden)
Ja, ich habe die Taschenlampen-Funktion innerhalb einer while(1) Schleifen aufgerufen, sodass ständig überprüft wird, ob die f-Taste gedrückt wird.
Also genauso wie in deinem Beispiel, nur dass die Schleife bei mir in einer Entity-Aktion steht


-- started with programming on march 2015 --
-- living in Germany near Dortmund --
Re: beginner problem with loops [Re: Saschaw04] #449984
04/04/15 11:43
04/04/15 11:43
Joined: Apr 2015
Posts: 29
Germany
Saschaw04 Offline OP
Newbie
Saschaw04  Offline OP
Newbie

Joined: Apr 2015
Posts: 29
Germany
@Kartoffel
Could make a example please. How it works?


-- started with programming on march 2015 --
-- living in Germany near Dortmund --
Re: beginner problem with loops [Re: Saschaw04] #449986
04/04/15 12:19
04/04/15 12:19
Joined: Apr 2005
Posts: 1,988
Canadian, Eh
DLively Offline
Serious User
DLively  Offline
Serious User

Joined: Apr 2005
Posts: 1,988
Canadian, Eh
Hey Saschaw04

First, I believe the manual said I read somewhere not to use while loops in functions (voids) but rather, nest the function into a while loop, in an action.

so in your flashlights action do this:
Code:
var light_on = 0;
var pressed = 0;
function press_f(){
     // -- Turn the Light On
     if(key_f && pressed == 0 && light_on == 0){light_on = 1;pressed = 1;}

     // -- Turn the Light Off
     if(key_f && pressed == 0 && light_on == 1){light_on = 0;pressed = 1;}

     // -- Wait until release of key_f to allow pressed again
     if(key_f==0 && pressed == 1) {pressed = 0;}

     if(light_on == 1){
           ..//Do this
     }
     else{
           ..//Reset / stop countdown
     }
}
action flashlight(){
      while(1){
            press_f();
            wait(1);
      }
}



EDIT: I Just edited the heck out of this - so now you can know its final tongue


I looked in the manual to see if I could back up my info... but I couldn't find it. I'm not sure where I read that, but feel it to be true. Someone correct me if I'm wrong here.

Last edited by DLively; 04/04/15 12:50.

A8 Pro 8.45.4
YouTube: Create Games For Free
Free Resources: www.CGForFree.com
Re: beginner problem with loops [Re: DLively] #449988
04/04/15 12:46
04/04/15 12:46
Joined: Apr 2015
Posts: 29
Germany
Saschaw04 Offline OP
Newbie
Saschaw04  Offline OP
Newbie

Joined: Apr 2015
Posts: 29
Germany
OK, thank you.
I will think about it and check it out.
I write when I did it


-- started with programming on march 2015 --
-- living in Germany near Dortmund --
Re: beginner problem with loops [Re: Saschaw04] #449992
04/04/15 13:19
04/04/15 13:19
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
This is how I would approach it:

Code:
var flashlight_on = 0;

void toggle_flashlight()
{
	flashlight_on = 1 - flashlight_on; // toggle between 1 and 0
	
	if(flashlight_on)
	{
		// *** turn flashlight on
		
		while(flashlight_on)
		{
			// *** battery drain
		}
	}
	else
	{
		// *** turn flashlight off
	}
}


main()
{
	// blabla
	
	on_f = toggle_flashlight; // assign 'toggle_flashlight' to the [F]-key
	
	// blabla	
}


Note that the 'on_f'-event gets executed only a single time when you press the button, no matter how long you keep it pressed (which is exactly what you want in this case).

Edit: Obviously, you have to put your own stuff where the "// ***" are.

Edit2:
Quote:
Ja, ich habe die Taschenlampen-Funktion innerhalb einer while(1) Schleifen aufgerufen, sodass ständig überprüft wird, ob die f-Taste gedrückt wird.
Also genauso wie in deinem Beispiel, nur dass die Schleife bei mir in einer Entity-Aktion steht

Das ist dann auch das Problem. Damit wird nämlich die Funktion jeden frame gestartet so lange du die F-Taste gedrückt hältst. Deswegen läuft die countdown-funktion mehrmals parallel, was dann dazu führt dass der counter viel schneller und unregelmäßig runterzählt.

Last edited by Kartoffel; 04/04/15 13:24.

POTATO-MAN saves the day! - Random
Page 1 of 3 1 2 3

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