Help and comments of Bolt Action Rifle system
#1

Hello!

I'm currently writing this system which replicates a bolt action rifle.
I'm having some trouble making it work correctly, thought. Sometimes it works, sometimes it doesnt, please give me your best advice and help if you can!


OnPlayerKeyStateChange callback
Quote:

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
if (PRESSED(KEY_FIRE))
{
if(GetPlayerWeapon(playerid) == 33)
{
if(cerrojo == 0)
{
cerrojoTimer = SetTimer("cerrojoTime", 500, true);
}
else
{
SetPlayerArmedWeapon(playerid,0);
SendClientMessage(playerid, COLOR_RED, "You must pull your rifle bolt.");
ClearAnimations(playerid);
}
}
}
if (HOLDING(KEY_FIRE))
{
if(GetPlayerWeapon(playerid) == 33)
{
if(cerrojo == 0)
{
cerrojoTimer = SetTimer("cerrojoTime", 500, true);
}
else
{
SetPlayerArmedWeapon(playerid,0);
ClearAnimations(playerid);
}
}
}
return 1;
}

cerrojoTime
Quote:

public cerrojoTime()
{
cerrojo = 1;
KillTimer(cerrojoTimer);
}

Bolt command
Quote:

CMD:bolt(playerid, params[])
{
if(GetPlayerWeapon(playerid) == 33 && cerrojo == 1)
{
SendClientMessage(playerid, COLOR_GREEN, "You pull the bolt of your rifle.");
cerrojo = 0;
}
else
{
SendClientMessage(playerid, COLOR_RED, "You're not holding your rifle or the bolt is already loaded.");
}
return 1;
}

When player spawns, cerrojo value is set to 0.

Why do you think its working so erratically?

Do you think it is adviced to use a timer for this system? Or should I set the variable value directly in the OnPlayerKeyStateChange callback? My reasoning for using a timer is that the variable sets its value long before the shot is made, and it doesn't let the player shoot his rifle, but I'm not sure if I'm correct.

EDIT: After I use /bolt, the cerrojo value is still 1.

EDIT 2: If you have a more esthetic or optimized way to make this system, please let me know!
Reply
#2

which part is exactly dont works?
Reply
#3

Quote:
Originally Posted by Mugala
Посмотреть сообщение
which part is exactly dont works?
Well, when I use timers the value of the variable cerrojo never changes to zero, I'm thinking because there's a loop somewhere in the timer but I'm not sure.

And if I don't use a timer, like this:

Quote:

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
if (PRESSED(KEY_FIRE))
{
if(GetPlayerWeapon(playerid) == 33)
{
if(cerrojo == 0)
{
//cerrojoTimer = SetTimer("cerrojoTime", 500, true);
cerrojo = 1;
}
else
{
SetPlayerArmedWeapon(playerid,0);
SendClientMessage(playerid, COLOR_RED, "You must pull your rifle bolt.");
ClearAnimations(playerid);
}
}
}
if (HOLDING(KEY_FIRE))
{
if(GetPlayerWeapon(playerid) == 33)
{
if(cerrojo == 0)
{
//cerrojoTimer = SetTimer("cerrojoTime", 500, true);
cerrojo = 1;
}
else
{
SetPlayerArmedWeapon(playerid,0);
ClearAnimations(playerid);
}
}
}
return 1;
}

It won't let me shoot, I think because the variable sets its value to 1 long before I'm able to shoot.
Reply
#4

Yup, you set the timer to be repeating:
pawn Код:
SetTimer("cerrojoTime", 500, true);
// set it to
SetTimer("cerrojoTime", 500, false);
Reply
#5

Quote:
Originally Posted by Misiur
Посмотреть сообщение
Yup, you set the timer to be repeating:
pawn Код:
SetTimer("cerrojoTime", 500, true);
// set it to
SetTimer("cerrojoTime", 500, false);
Thanks! I'm kind of learning how to pawno.

Do you guys think using a timer its the best when it comes to memory saving or optimization? Do you guys have other way to write this code?
Reply
#6

Quote:
Originally Posted by Eibison
Посмотреть сообщение
Thanks! I'm kind of learning how to pawno.

Do you guys think using a timer its the best when it comes to memory saving or optimization? Do you guys have other way to write this code?
I dont know what u mean but you're using Global variables, instead of per-player variable.
edit this global variables to player variables, otherwise this script will not work for 2 or more players.
Reply
#7

Quote:
Originally Posted by Mugala
Посмотреть сообщение
I dont know what u mean but you're using Global variables, instead of per-player variable.
edit this global variables to player variables, otherwise this script will not work for 2 or more players.
Will do, thanks for the tip!

What I meant is that will using a timer eventually lag the server if, for example, a lot of players execute it simultaniely
Reply
#8

Quote:
Originally Posted by Eibison
Посмотреть сообщение
Will do, thanks for the tip!

What I meant is that will using a timer eventually lag the server if, for example, a lot of players execute it simultaniely
yeah it will cause some lags, but you can't notice in game
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)