Creating Command With Key On Keyboard?
#1

Hello I Was Wondering To Make A Command With A Key On Keyboard.

When Player Press It And It Do the command.
Like Pressing "N" it will "/car engine"

anyone can give me a suggestion?
Reply
#2

First, download this zcmd include and put it pawno folder/include then copy-paste this code in your a new .pwn and compile.
pawn Код:
#include <a_samp>
#include <zcmd>

#define colorRed        0xFF0000FF
#define colorYellow     0xFFFF00FF

forward StartEngine(playerid);
forward DamagedEngine(playerid);

new vehEngine[MAX_VEHICLES];

public OnPlayerStateChange(playerid, newstate, oldstate)
{
    new vehicleid = GetPlayerVehicleID(playerid);
   
    if(newstate == PLAYER_STATE_DRIVER)
    {
        if(vehEngine[vehicleid] == 0)
        {
            TogglePlayerControllable(playerid, 0);
            SendClientMessage(playerid, colorYellow, "Vehicle engine NOT started");
            SendClientMessage(playerid, colorYellow, "To start the vehicle's engine press \"Shift\" or type \"/engine\"");
        }
        else if(vehEngine[vehicleid] == 1)
        {
            TogglePlayerControllable(playerid, 1);
            SendClientMessage(playerid, colorYellow, "Vehicle engine running");
        }
    }
    return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    new vehicleid = GetPlayerVehicleID(playerid);
   
    if(IsPlayerInAnyVehicle(playerid))
    {
        if(vehEngine[vehicleid] == 0)
        {
            if(newkeys == KEY_NO)
            {
                vehEngine[vehicleid] = 2;
                SetTimerEx("StartEngine", 3000, 0, "i", playerid);
                SendClientMessage(playerid, colorYellow, "Vehicle engine starting");
            }
        }
        if(newkeys == KEY_SECONDARY_ATTACK)
        {
            RemovePlayerFromVehicle(playerid);
            TogglePlayerControllable(playerid, 1);
        }
    }
    return 1;
}

public StartEngine(playerid)
{
    new vehicleid = GetPlayerVehicleID(playerid);
    new Float:health;
    new rand = random(2);
   
    GetVehicleHealth(vehicleid, health);
   
    if(IsPlayerInAnyVehicle(playerid))
    {
        if(vehEngine[vehicleid] == 2)
        {
            if(health > 300)
            {
                if(rand == 0)
                {
                    vehEngine[vehicleid] = 1;
                    TogglePlayerControllable(playerid, 1);
                    SetTimerEx("DamagedEngine", 1000, 1, "i", playerid);
                    SendClientMessage(playerid, colorYellow, "Vehicle engine started");
                }
                if(rand == 1)
                {
                    vehEngine[vehicleid] = 0;
                    TogglePlayerControllable(playerid, 0);
                    SendClientMessage(playerid, colorYellow, "Vehicle engine failed to start");
                }
            }
            else
            {
                vehEngine[vehicleid] = 0;
                TogglePlayerControllable(playerid, 0);
                SendClientMessage(playerid, colorYellow, "Vehicle engine failed to start due to damage");
            }
        }
    }
    return 1;
}

public DamagedEngine(playerid)
{
    new vehicleid = GetPlayerVehicleID(playerid);
    new Float:health;
   
    GetVehicleHealth(vehicleid, health);

    if(IsPlayerInAnyVehicle(playerid))
    {
        if(vehEngine[vehicleid] == 1)
        {
            if(health < 300)
            {
                vehEngine[vehicleid] = 0;
                TogglePlayerControllable(playerid, 0);
                SendClientMessage(playerid, colorYellow, "Vehicle engine stopped due to damage");
            }
        }
    }
    return 1;
}

public OnPlayerExitVehicle(playerid, vehicleid)
{
    TogglePlayerControllable(playerid, 1);
    return 1;
}
               
CMD:engine(playerid, params[])
{
    new vehicleid = GetPlayerVehicleID(playerid);

    if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, colorRed, "You need to be in a vehicle to use this command");
   
    if(vehEngine[vehicleid] == 0)
    {
        vehEngine[vehicleid] = 2;
        SetTimerEx("StartEngine", 3000, 0, "i", playerid);
        SendClientMessage(playerid, colorYellow, "Vehicle engine starting");
    }
    else if(vehEngine[vehicleid] == 1)
    {
        vehEngine[vehicleid] = 0;
        TogglePlayerControllable(playerid, 0);
        SendClientMessage(playerid, colorYellow, "Vehicle engine stopped");
        SendClientMessage(playerid, colorYellow, "To re-start the vehicle's engine press \"Shift\" or type \"/engine\"");
    }
    return 1;
}
Reply
#3

Код:
CMD:car(playerid, params[])
{
	new string[128];
	if(isnull(params))
	{
		SendClientMessageEx(playerid, COLOR_GREY, "SUDUNG: /car [name]");
		SendClientMessageEx(playerid, COLOR_GREY, "Available names: Status, Engine, Light, Cop, Hood, Fuel, Windows");
		return 1;
	}
	if(strcmp(params, "engine", true) == 0 && IsPlayerInAnyVehicle(playerid) && GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
	{
		new engine,lights,alarm,doors,bonnet,boot,objective,vehicleid;
		vehicleid = GetPlayerVehicleID(playerid);
		if(GetVehicleModel(vehicleid) == 481 || GetVehicleModel(vehicleid) == 509 || GetVehicleModel(vehicleid) == 510 || DynVeh[vehicleid] != -1 && DynVehicleInfo[DynVeh[vehicleid]][gv_iType] == 1 && GetVehicleModel(vehicleid) == 592) return SendClientMessageEx(playerid,COLOR_WHITE,"This command can't use in this vehicle.");
		GetVehicleParamsEx(vehicleid,engine,lights,alarm,doors,bonnet,boot,objective);
		if(engine == VEHICLE_PARAMS_ON)
		{
			SetVehicleEngine(vehicleid, playerid);
			format(string, sizeof(string), "{FF8000}** {C2A2DA}%s turns the key in the ignition and the engine stops.", GetPlayerNameEx(playerid));
			ProxDetector(30.0, playerid, string, COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE);
		}
		else if((engine == VEHICLE_PARAMS_OFF || engine == VEHICLE_PARAMS_UNSET))
		{
			if (GetPVarInt(playerid, "Refueling")) return SendClientMessageEx(playerid, COLOR_WHITE, "You can't do this while refueling.");
			format(string, sizeof(string), "{FF8000}** {C2A2DA}%s turns the key in the ignition and the engine starts.", GetPlayerNameEx(playerid));
			ProxDetector(30.0, playerid, string, COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE);
			SendClientMessageEx(playerid, COLOR_WHITE, "Vehicle engine starting, please wait...");
			SetTimerEx("SetVehicleEngine", 1000, 0, "dd",  vehicleid, playerid);
		}
This is my engine code. can u add
Reply
#4

add cmd code in OnPlayerStateChange Callback.
try this

pawn Код:
#define PRESSED(%0) \
    (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))

public OnPlayerStateChange(playerid, newstate, oldstate)
{
  if(PRESSED(KEY_NO))
  {
    if(IsPlayerInAnyVehicle(playerid) && GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
    {
        new engine,lights,alarm,doors,bonnet,boot,objective,vehicleid;
        vehicleid = GetPlayerVehicleID(playerid);
        if(GetVehicleModel(vehicleid) == 481 || GetVehicleModel(vehicleid) == 509 || GetVehicleModel(vehicleid) == 510 || DynVeh[vehicleid] != -1 && DynVehicleInfo[DynVeh[vehicleid]][gv_iType] == 1 && GetVehicleModel(vehicleid) == 592) return SendClientMessageEx(playerid,COLOR_WHITE,"This command can't use in this vehicle.");
        GetVehicleParamsEx(vehicleid,engine,lights,alarm,doors,bonnet,boot,objective);
        if(engine == VEHICLE_PARAMS_ON)
        {
            SetVehicleEngine(vehicleid, playerid);
            format(string, sizeof(string), "{FF8000}** {C2A2DA}%s turns the key in the ignition and the engine stops.", GetPlayerNameEx(playerid));
            ProxDetector(30.0, playerid, string, COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE);
        }
    }
 }
return 1;
}
Reply
#5

Here is problem.
Код:
./includes/callbacks.pwn(77) : error 017: undefined symbol "newkeys"
./includes/callbacks.pwn(88) : error 017: undefined symbol "string"
./includes/callbacks.pwn(88) : error 017: undefined symbol "string"
./includes/callbacks.pwn(88) : error 029: invalid expression, assumed zero
./includes/callbacks.pwn(88) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


5 Errors.
Reply
#6

Read up on it here https://sampwiki.blast.hk/wiki/Keys and here https://sampwiki.blast.hk/wiki/OnPlayerKeyStateChange plus make sure you have this defined
Код:
#define PRESSED(%0) \
	(((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
Reply
#7

Код:
#define PRESSED(%0) \
    (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
  if(PRESSED(KEY_NO))
  {
    if(IsPlayerInAnyVehicle(playerid) && GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
    {
        new engine,lights,alarm,doors,bonnet,boot,objective,vehicleid;
        vehicleid = GetPlayerVehicleID(playerid);
        if(GetVehicleModel(vehicleid) == 481 || GetVehicleModel(vehicleid) == 509 || GetVehicleModel(vehicleid) == 510 || DynVeh[vehicleid] != -1 && DynVehicleInfo[DynVeh[vehicleid]][gv_iType] == 1 && GetVehicleModel(vehicleid) == 592) return SendClientMessageEx(playerid,COLOR_WHITE,"This command can't use in this vehicle.");
        GetVehicleParamsEx(vehicleid,engine,lights,alarm,doors,bonnet,boot,objective);
        if(engine == VEHICLE_PARAMS_ON)
        {
            SetVehicleEngine(vehicleid, playerid);
            format(string, sizeof(string), "{FF8000}** {C2A2DA}%s turns the key in the ignition and the engine stops.", GetPlayerNameEx(playerid));
            ProxDetector(30.0, playerid, string, COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE);
        }
    }
 }
return 1;
}
Reply
#8

it still error
Quote:

./includes/callbacks.pwn(8 : error 017: undefined symbol "string"
./includes/callbacks.pwn(8 : error 017: undefined symbol "string"
./includes/callbacks.pwn(8 : error 029: invalid expression, assumed zero
./includes/callbacks.pwn(8 : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase


4 Errors.

Reply
#9

Everyone makes it harder than it is.
When the key is pressed, just execute the command with a parameter like this:
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
  if(PRESSED(KEY_NO))
  {
cmd_car(playerid, "engine");
 }
return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)