Page 1 of 1

Charging?

PostPosted: Wed Jul 19, 2006 9:51 pm
by beefwelington
Say I've got an actor, just a red dot, and when I hold down the spacebar I want it to "charge" the dot. Depending on how long I hold the spacebar, I want the dot to have a different initial velocity when I let go of the spacebar.

Like, I hold space for one second and let go and the dot moves forward at xvelocity = 2, but if I hold it for 4 seconds and let go, the dot moves forward at xvelocity = 8.

Also, in a somewhat related question, if I've got a variable in the scripting for one action, how do I transfer the value of that variable over to another script for a different action? I'm not very familiar with C, only Java.

PostPosted: Wed Jul 19, 2006 10:03 pm
by makslane
You can try this:

1) Create the variable charge_ini_time (as Integer, Actor variable)
2) On Key Down event, put this:

Code: Select all
charge_ini_time = getTime().sec_utc;


3) On Key Up event, put this:

Code: Select all
//total_time will hold the number of seconds elapsed
int total_time = getTime().sec_utc - charge_ini_time;


4) Now, you can use the total_time value to change some other value, like velocity:

Code: Select all
xvelocity = total_time;


Read more about getTime() here:
http://game-editor.com/docs/script_refe ... tm#gettime

PostPosted: Wed Jul 19, 2006 10:21 pm
by beefwelington
Thanks for the fast and helpful response!

edit: I've gotten it to work, but for some reason my red dot won't budge. I've double checked, it doesn't seem to work.

PostPosted: Wed Jul 19, 2006 10:30 pm
by makslane
Create the variable in this way, will create a local variable, only valid in the script.

To access the variable in other scripts you can:

1) Create in the Global Code editor or
2) Create an Actor variable, using the 'Variables' button on Script Editor or
3) Create an Global variable, using the 'Variables' button on Script Editor

If you need an variable with different values between actors, like 'lives' you will want use the 'Actor variable' type

PostPosted: Wed Jul 19, 2006 10:51 pm
by beefwelington
Okay, now I've gotten it to work. The script for key down was set on repeat, so it kept getting new times for the charge value. Thanks for all the help.

PostPosted: Wed Jul 19, 2006 11:14 pm
by makslane
If you use repeat in the Key Down event, you will always reinit the charge_ini_time variable and don't will get the correct time between key down and key up.

I think the right way is don't use the repeat.
So, if you keep the key pressed for 10s, you will get 10s in key up.

PostPosted: Wed Jul 19, 2006 11:44 pm
by DilloDude
Another way would be to create a real variable 'charge' and on key down, repeat,
Code: Select all
charge += .5;
(or any other value) and on key up, put
Code: Select all
xvelocity = charge;
charge = 0;

PostPosted: Wed Jul 19, 2006 11:48 pm
by makslane
Nice solution 8)