OnPlayerKeyStateChange - buggy object
#1

I've created a submarine script, and I'm trying to add a torpedo thing to it.
But, the problem is, that the torpedo object(etc.) is only being created(and stuff done), if the submarine is driving(forwards/backwards).

If I stand still/turn it left/right, I will receive the message, but nothing else will happen!

Here's the code;

pawn Code:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(inSub{ playerid })
    {
        new Float:X, Float:Y, Float:Z;
        GetPlayerPos(playerid, X, Y, Z);
        new Float:X2, Float:Y2, Float:Z2;
       
        new string[92];
        if(newkeys & KEY_YES)
        {
            if(Submarine[SubID[playerid]][TorpedoLoaded] && CanShoot[playerid])
            {
                if(Submarine[SubID[playerid]][Torpedos])
                {
                    Submarine[SubID[playerid]][Torpedos]--;
                    format(string, sizeof(string), "SUBMARINE: Torpedo away! Remaining torpedos: %d/4", Submarine[SubID[playerid]][Torpedos]);
                    CanShoot[playerid] = 0;
                    foreach(Player, i)
                    {
                        if(IsPlayerInRangeOfPoint(i, 20, X, Y, Z))
                        {
                            SendClientMessage(i, -1, string), string = "\0";
                        }
                    }
                    Submarine[SubID[playerid]][TorpedoObject] = CreateObject(1636, Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY]-2, Submarine[SubID[playerid]][sZ], 0, 0, 0);
                    GetXYInFrontOfObject(Submarine[SubID[playerid]][TorpedoObject], playerid, Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], 150.0);
                    new time = MoveObject(Submarine[SubID[playerid]][TorpedoObject], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ], 65.0);
                    SetTimerEx("DetonateTorpedo", time, false, "ii", playerid, Submarine[SubID[playerid]][TorpedoObject]);
                    format(string, sizeof(string), "SUBMARINE: The torpedo will explode in approx. %d seconds!", time/1000);
                    SendClientMessage(playerid, -1, string), string = "\0";
                }
                else SendClientMessage(playerid, -1, "SUBMARINE: You've ran out of torpedos!");
            }
            else
            {
                SendClientMessage(playerid, -1, "SUBMARINE: Loading torpedo..");
                SetTimerEx("TorpedoLoadedT", 2500+random(3000), false, "ii", playerid, SubID[playerid]);
            }
        }
Reply
#2

Be very careful with that code, specifically this part:
Code:
format(string, sizeof(string), "SUBMARINE: Torpedo away! Remaining torpedos: %d/4", Submarine[SubID[playerid]][Torpedos]);
CanShoot[playerid] = 0;
foreach(Player, i)
{
	if(IsPlayerInRangeOfPoint(i, 20, X, Y, Z))
	{
		SendClientMessage(i, -1, string), string = "\0";
	}
}
The way I see it, you send the first player found in range a message, and then you "NULL terminate" the string, perhaps you wanted to "end it" after the foreach loop?

Also I suggest detecting stuff like that under OnPlayerUpdate with static variables, simply because it gets called more frequently.
Reply
#3

Quote:
Originally Posted by Virtual1ty
View Post
Be very careful with that code, specifically this part:
Code:
format(string, sizeof(string), "SUBMARINE: Torpedo away! Remaining torpedos: %d/4", Submarine[SubID[playerid]][Torpedos]);
CanShoot[playerid] = 0;
foreach(Player, i)
{
	if(IsPlayerInRangeOfPoint(i, 20, X, Y, Z))
	{
		SendClientMessage(i, -1, string), string = "\0";
	}
}
The way I see it, you send the first player found in range a message, and then you "NULL terminate" the string, perhaps you wanted to "end it" after the foreach loop?

Also I suggest detecting stuff like that under OnPlayerUpdate with static variables, simply because it gets called more frequently.
Too many things, doing a loop each second ( or even less! ) would cause the server to crash imo.
It's the same as running a 1 second ( or 500 ms ) timer which repeats itself..

Still need help though.
Reply
#4

I only meant for detecting the 'Y' key, that's why there are if-checks to prevent that. Also, I suppose you only want torpedos to be fired every "X" seconds, so then you can also limit it that way by using GetTickCount() or gettime() so it only gets called once when needed. Like it's done in "Grand Larceny" with class selection (changing cities).
Reply
#5

Quote:
Originally Posted by Virtual1ty
View Post
I only meant for detecting the 'Y' key, that's why there are if-checks to prevent that. Also, I suppose you only want torpedos to be fired every "X" seconds, so then you can also limit it that way by using GetTickCount() or gettime() so it only gets called once when needed. Like it's done in "Grand Larceny" with class selection (changing cities).
No, the torpedos can be shot whenever the player wants to ( well, after loading ).
And I'm still.. new to gettime/gettickcount, so I will stick to thing I actually understand and know.


Tried to set it on "OnPlayerUpdate", still the same problem. ( torpedo only gets created when moving forwards/backwards )
Reply
#6

Well what do you mean, it doesn't work sideways, you can only fire torpedos forwards/backwards?
I don't know what to tell you, somewhere is a mistake at your end, one of the variables you are using isn't correctly set, it's either "inSub{playerid}" or "CanShoot[playerid]".
Also show your code for moving the submarine, somekind of problem must be there if the torpedo only get's created when moving, as you said.
Reply
#7

Quote:
Originally Posted by Virtual1ty
View Post
Well what do you mean, it doesn't work sideways, you can only fire torpedos forwards/backwards?
I don't know what to tell you, somewhere is a mistake at your end, one of the variables you are using isn't correctly set, it's either "inSub{playerid}" or "CanShoot[playerid]".
Also show your code for moving the submarine, somekind of problem must be there if the torpedo only get's created when moving, as you said.
If I move the submarine forwards and reverse, the torpedos get created etc.
If I steer it sideways(to turn), they dont get created.
If I stand still, they dont get created.

Full code;
http://pastebin.com/SKXchiqi

movement of the subs
pawn Code:
public OnPlayerUpdate(playerid)
{
    new Float:pos[4], key[3];
    GetPlayerKeys(playerid, key[0], key[1], key[2]);
    if(inSub{ playerid })
    {
        if(key[1] == KEY_UP)
        {
            GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
            GetXYInFrontOfPlayer(playerid, pos[0], pos[1], 1.5);
            SetPlayerPos(playerid, pos[0], pos[1], pos[2]);
           
            GetObjectPos(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]);
            SetObjectPos(Submarine[SubID[playerid]][Object], pos[0], pos[1], Submarine[SubID[playerid]][sZ]);
        }
        else if(key[1] == KEY_DOWN)
        {
            GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
            GetXYInFrontOfPlayer(playerid, pos[0], pos[1], -0.5);
            SetPlayerPos(playerid, pos[0], pos[1], pos[2]);
           
            GetObjectPos(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]);
            SetObjectPos(Submarine[SubID[playerid]][Object], pos[0], pos[1], Submarine[SubID[playerid]][sZ]);
        }
        if(key[2] == KEY_LEFT)
        {
            GetPlayerFacingAngle(playerid, pos[3]);
            SetPlayerFacingAngle(playerid, pos[3] + 0.5);
            SetCameraBehindPlayer(playerid);

            GetObjectRot(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]);
            SetObjectRot(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]+0.5);
        }
        else if(key[2] == KEY_RIGHT)
        {
            GetPlayerFacingAngle(playerid, pos[3]);
            SetPlayerFacingAngle(playerid, pos[3] - 0.5);
            SetCameraBehindPlayer(playerid);
           
            GetObjectRot(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]);
            SetObjectRot(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]-0.5);
        }
        if(key[0] == KEY_JUMP)
        {
            if(pos[2] < 2.0)
            {
                GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
                SetPlayerPos(playerid, pos[0], pos[1], pos[2] + 0.5);
               
                GetObjectPos(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]);
                SetObjectPos(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]+0.6);
               
                if(pos[2] > 2.0)
                {
                    GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
                    SetPlayerPos(playerid, pos[0], pos[1], pos[2] - 1.0);
                   
                    GetObjectPos(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]);
                    SetObjectPos(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]-1.1);
                }
            }
            else GameTextForPlayer(playerid, "~n~~n~~n~~n~~n~~n~~n~~r~MAX HEIGHT REACHED", 2500, 3);
        }
        else if(key[0] == KEY_SPRINT)
        {
            if(pos[2] > -23.0)
            {
                GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
                SetPlayerPos(playerid, pos[0], pos[1], pos[2] - 0.5);
               
                GetObjectPos(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]);
                SetObjectPos(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]-0.4);
               
                if(pos[2] < -23.0)
                {
                    GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
                    SetPlayerPos(playerid, pos[0], pos[1], pos[2] + 1.0);
                   
                    GetObjectPos(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]);
                    SetObjectPos(Submarine[SubID[playerid]][Object], Submarine[SubID[playerid]][sX], Submarine[SubID[playerid]][sY], Submarine[SubID[playerid]][sZ]+0.9);
                }
            }
            else GameTextForPlayer(playerid, "~n~~n~~n~~n~~n~~n~~n~~r~MAX DEPTH REACHED", 1500, 3);
        }
    }
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)