3 registered members (dr_panther, Ayumi, 1 invisible),
708
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
beginner problem with loops
#449971
04/04/15 09:49
04/04/15 09:49
|
Joined: Apr 2015
Posts: 29 Germany
Saschaw04
OP
Newbie
|
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: 3run]
#449973
04/04/15 10:19
04/04/15 10:19
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
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
OP
Newbie
|
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
Expert
|
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:
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
OP
Newbie
|
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
OP
Newbie
|
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
Serious User
|
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:
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 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.
|
|
|
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
OP
Newbie
|
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
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
This is how I would approach it:
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: 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
|
|
|
|