PDA

View Full Version : Just going to mess around with some stuff


Knightmare1
01-21-2008, 03:27 AM
hey, im Nightmare, and yes, im new to scripting, so now im going to try to learn to script.

im trying to make a weapon that will overheat, this is what i have so far:
//#CLIENTSIDE
function onCreated()
{
this.overheat_max = 100;
this.overheat = 0;
}

function onWeaponFired()
{
this.active = !this.active;
if (this.active)
{
setTimer( 1 );
}
if (!this.active)
{
onCreated()
}
}
function onKeyPressed()
{
if (this.key = "s")
{
if (this.active)
{
if (this.overheat = !100)
{
//shoot functions.. havent gotten that far..
this.overheat +=1;
}
}
}
}

function onTimeOut()
{
if (this.overheat = this.overheat_max)
{
//makes it so it cant shoot anymore..
}
}

cbk1994
01-21-2008, 05:57 AM
Needs some work ;)

One thing I noticed right away was you were doing

if ( var = ! var2 )

use

if ( var != var2 )

Knightmare1
01-21-2008, 02:15 PM
Needs some work ;)

One thing I noticed right away was you were doing

if ( var = ! var2 )

use

if ( var != var2 )

ok ty.

Toxen
01-21-2008, 04:17 PM
Well try to change the key pressed part to this,

function onKeyPressed()
{
if (keydown2(getkeycode("s"),true))
{
if (this.active)
{
if (this.overheat = !100)
{
//shoot functions.. havent gotten that far..
this.overheat +=1;
}
}
}
}

Angel_Light
01-21-2008, 04:36 PM
//#CLIENTSIDE
function onCreated()
{
this.overheat_max = 100;
this.overheat = 0;
}

function onWeaponFired()
{
this.active = !this.active;
if (this.active)
{
setTimer( 1 );
}
if (!this.active)
{
onCreated()
}
}
function onKeyPressed( code, key, ind)
{
if (temp.key == "s")
{
if (this.active)
{
if (this.overheat != 100)
{
this.angle = getangle( vecx( player.dir), vecy( player.dir));
shoot( this.x, this.y, this.z, this.angle, 0, 0, "arrow", NULL);
this.overheat +=1;
sleep( 0.1);
}
}
}
}

function onTimeOut()
{
if (this.overheat >= this.overheat_max)
{
this.active = 2;

sleep( 5);

this.active = 1;
}

setTimer( 0.05);
}

cbk1994
01-21-2008, 05:45 PM
Well try to change the key pressed part to this,

function onKeyPressed()
{
if (keydown2(getkeycode("s"),true))
{
if (this.active)
{
if (this.overheat = !100)
{
//shoot functions.. havent gotten that far..
this.overheat +=1;
}
}
}
}

Why would you do this? Simply


function onKeyPressed( code, key )
{
if ( key == "s" )
{
if ( this.active )
{
// etc
}
}
}


No need for keydown2 except to tell if something is held down; the onKeyPressed event tells you the key.

Toxen
01-23-2008, 04:31 AM
Well hes saying it over heats, so im guessing you needed to charge it ;O?

cbk1994
01-23-2008, 05:03 AM
For guns you would want something like this:


function onTimeOut()
{
temp.k = keydown2( getKeyCode( "s" ), true );
if ( temp.k )
{
if ( ! this.isKeyPressed )
{
// the key is not being held down; has been pressed once.
}
else
{
// the key is being held down; not just pressed once.
}
}
this.isKeyPressed = temp.k;
setTimer( 0.05 );
}


Just an example.

PrinceOfKenshin
01-23-2008, 05:06 AM
For guns you would want something like this:


function onTimeOut()
{
temp.k = keydown2( getKeyCode( "s" ), true );
if ( temp.k )
{
if ( ! this.isKeyPressed )
{
// the key is not being held down; has been pressed once.
}
else
{
// the key is being held down; not just pressed once.
}
}
this.isKeyPressed = temp.k;
setTimer( 0.05 );
}


Just an example.

I'm kinda new to gs2, why would you use a temp variable for that?

cbk1994
01-23-2008, 05:26 AM
I'm kinda new to gs2, why would you use a temp variable for that?

Because there's no point in taking up memory when this doesn't need to be stored past this function.

If you knew GS1, it's basically equivelant to a variable with no prefix, which is lost after the function is over.

temp.k just told me true/false the key is pressed. I could have eliminated this and made it into an if statement; this was just easier and simpler.

this.isKeyPressed tells me if LAST time around it was pressed, so I can tell if it is being held down, or touched once. This is so you can have modes such as single, automatic, etc.

Angel_Light
01-23-2008, 05:38 AM
in essence, it causes a fraction of a fraction of less lag, (Which adds up quick) It basically saves Memory, (RAM Memory Space).

this.var is saved until it is updated again.

temp.var is saved until the function, that it was called in, ends.

=]

bscharff
01-24-2008, 03:04 AM
*knocks on wood*

Chompy
01-24-2008, 04:05 PM
*knocks on wood*

err, what?

Tolnaftate2004
01-24-2008, 07:50 PM
Because there's no point in taking up memory when this doesn't need to be stored past this function.

If you knew GS1, it's basically equivelant to a variable with no prefix, which is lost after the function is over

temp variables still take up memory, it's just stored on the stack versus the heap. Variables defined with no prefix are global, they act now as they did in GS1 for backwards compatibility.

Inverness
01-25-2008, 01:07 AM
Function parameters are also automatically temp. variables and can be referenced as such.

Once you declare a temp. variable you can reference it without the prefix and it will not be a global. Because of this for most functions I prefer to declare all my temp.variable = 0; at the beginning of the function.

Ex:

public function save(filename) {
temp.output = 0;
temp.i = 0;
temp.e = 0;
temp.keys = this.getdynamicvarnames();
temp.vars = 0;

for (i = 0; i < keys.size(); i ++) {
if (this.(@ keys[i]).type() != -1) {
continue;
}
output.add("[" @ keys[i] @ "]");
vars = this.(@ keys[i]).getdynamicvarnames();
for (e = 0; e < vars.size(); e ++) {
if (this.(@ keys[i]).(@ vars[e]) != null) {
output.add(vars[e] @ "=" @ this.(@ keys[i]).(@ vars[e]));
}
}
}
output.savelines(filename, 0);
}