Little coding questions - For general minor queries 5

When should function be public?
When should function be a stock?
Reply

Hi!

You can look here.
https://sampforum.blast.hk/showthread.php?tid=335885
Reply

Quote:
Originally Posted by Amads
View Post
When should function be public?
When should function be a stock?
Public is when the function will be called through a timer, or through CallRemoteFunction.
Reply

With bitwise operation you would basically do the same
Also nearly all colors in sa-mp are stored in RGBA

color = 0xFFFF00FF; // Yellow with full alpha

First you would extract each color

red = color >>> 24; // shifting everything by 24 bits to the right
green = color >> 16 & 0xFF; // shifting and extracting the rightmost 8 bit
blue = color >> 8 & 0xFF;

After that you simply multiply it and check if it went over 255
Additionally be sure that VALUE is a positive number

red = min(0xFF, red * VALUE); // same for green and blue

At the end you put it together

color = red << 24 | green << 16 | blue << 8 | color & 0xFF;
Reply

Quote:
Originally Posted by Nero_3D
View Post
With bitwise operation you would basically do the same
Also nearly all colors in sa-mp are stored in RGBA

color = 0xFFFF00FF; // Yellow with full alpha

First you would extract each color

red = color >>> 24; // shifting everything by 24 bits to the right
green = color >> 16 & 0xFF; // shifting and extracting the rightmost 8 bit
blue = color >> 8 & 0xFF;

After that you simply multiply it and check if it went over 255
Additionally be sure that VALUE is a positive number

red = min(0xFF, red * VALUE); // same for green and blue

At the end you put it together

color = red << 24 | green << 16 | blue << 8 | color & 0xFF;
Oh right it's RGBA not ARGB (facepalm)... I'm feeling so dumb now...
Reply

Quote:
Originally Posted by Jeroen52
View Post
I'm looking to add a function to darken or lighten a piece of color with a function.
The color is in the format of GetPlayerColor, and I'd like to make it darker or lighter depending on a float value.

Example to make a color darker.
Code:
ChangeColorIntensity(GetPlayerColor(playerid),0.5)
Example to make a color lighter.
Code:
ChangeColorIntensity(GetPlayerColor(playerid),1.5)
https://sampwiki.blast.hk/wiki/Colors_List#Doing_math

Code:
SetPlayerMarkerVisibility(playerid, alpha = 0xFF)
{
    new oldcolor, newcolor;
    alpha = clamp(alpha, 0x00, 0xFF);
    oldcolor = GetPlayerColor(playerid);
    newcolor = (oldcolor & ~0xFF) | alpha;
    return SetPlayerColor(playerid, newcolor);
}
Reply

if i use foreach(Player, i), not need to do

foreach(Player, i){
if(IsPlayerConnected(i)){

I don't need to use isplayerconnected ?
Reply

Quote:
Originally Posted by sam29
View Post
if i use foreach(Player, i), not need to do

foreach(Player, i){
if(IsPlayerConnected(i)){

I don't need to use isplayerconnected ?
When using foreach, checking with IsPlayerConnected is not needed at all because foreach loops through connected players only.

The syntax though you use is the old one and is deprecated. Use:
Code:
foreach(new i : Player)
Reply

Thanks Konstantinos

I got an old YSF plugin did by Y_Less. Is there in YSI, an equivalent and so i may remove this plugin ?

thanks
Reply

In my opinion, keep functions as functions. You're not really saving anything by using a macro. The advantage of a function is sanity checks and a more useful stack trace when you're using it multiple places. Example:

pawn Код:
stock HasInsurance(vehicleid)
{
    if(!(0 < vehicleid < MAX_VEHICLE)) // checks if the ID is out of bounds
    {
        print("ERROR: ..."); // catch bad vehicle IDs
        // PrintStackTrace(); // find out where HasInsurance was called from so you can fix the bug
        return 1;
    }

    if(!IsValidVehicle(vehicleid)) // checks if the vehicle actually exists
    {
        print("ERROR: ...");
        // PrintStackTrace();
        return 1;
    }

    // perform your actual function
}
You'll catch errors safely and much earlier on in development. Macros act weird sometimes and you might find yourself in a position where you can't find the reason for a bug and debugging is made difficult with the use of a macro instead of a standard function call.

Q: When are macro functions useful?

They are more useful for building libraries and creating new syntax (look at y_iterate, y_timers, modio for examples).

Sure, you can save time typing when writing regular code but it might cost you in the long run. And with modern text editors (like Sublime), writing code is faster than ever anyway.
Reply

Quote:
Originally Posted by Dayrion
Посмотреть сообщение
I'm looking too for weapons realoading sounds. I'm looking for it since a loooooong time.
I've only one, ID 36401 but it's a very bad sound.
.....
Reply

So a few days ago i was looking to make a script that would make weapons feel a bit more fleshed out, in the base game weapons don't really have any noticeable recoil besides the spray-pattern, but it still doesn't take that much skill to control your weapon, in games like Ghost Recon Phantoms the recoil is very noticeable and requires you to not only burst-fire but also keep track of how high your aim is getting with each shot.

I wanted to replicate this in SAMP with the camera controls, so i did this:

Код:
public OnPlayerWeaponShot(playerid, weaponid, hittype, hitid, Float:fX, Float:fY, Float:fZ)
{

	new 
		Float:x,
		Float:y,
		Float:z;
	
	if(recoil == 1)
	{
		GetPlayerCameraFrontVector(playerid, x, y, z);
		SetPlayerCameraLookAt(playerid, x, y, z + 1.0, CAMERA_MOVE);
	}
		
	return 1;
}
So after i finished the code i started up my server and tested it to see what sort of fuckery would happen and upon shooting, my camera did go up but then it started spazzing out and started going up to the sky until the screen turned black.
So is there any way you could accomplish what i'm trying to do in SAMP or is it not possible?
Reply

Quote:
Originally Posted by Greggu
Посмотреть сообщение
So a few days ago i was looking to make a script that would make weapons feel a bit more fleshed out, in the base game weapons don't really have any noticeable recoil besides the spray-pattern, but it still doesn't take that much skill to control your weapon, in games like Ghost Recon Phantoms the recoil is very noticeable and requires you to not only burst-fire but also keep track of how high your aim is getting with each shot.

I wanted to replicate this in SAMP with the camera controls, so i did this:

Код:
public OnPlayerWeaponShot(playerid, weaponid, hittype, hitid, Float:fX, Float:fY, Float:fZ)
{

	new 
		Float:x,
		Float:y,
		Float:z;
	
	if(recoil == 1)
	{
		GetPlayerCameraFrontVector(playerid, x, y, z);
		SetPlayerCameraLookAt(playerid, x, y, z + 1.0, CAMERA_MOVE);
	}
		
	return 1;
}
So after i finished the code i started up my server and tested it to see what sort of fuckery would happen and upon shooting, my camera did go up but then it started spazzing out and started going up to the sky until the screen turned black.
So is there any way you could accomplish what i'm trying to do in SAMP or is it not possible?
GetPlayerCameraFrontVector returns a value between -1.0 and 1.0

Use:
PHP код:
GetPointInFrontOfCamera3D(playerid,&Float:tx,&Float:ty,&Float:tz,Float:radius,&Float:rx=0.0,&Float:rz=0.0); 
3DTryg
Reply

Quote:
Originally Posted by AbyssMorgan
Посмотреть сообщение
GetPlayerCameraFrontVector returns a value between -1.0 and 1.0

Use:
PHP код:
GetPointInFrontOfCamera3D(playerid,&Float:tx,&Float:ty,&Float:tz,Float:radius,&Float:rx=0.0,&Float:rz=0.0); 
3DTryg
So it's possible?
Reply

No, not really. When you use SetPlayerCameraLookAt it actually disconnects the camera from the player. One thing you could do is increase the player's "drunk level" for a short time after shooting for a long time which would shift the camera angle without disconnecting it. You have less control over where it's moving this way but it's better than nothing!

Example:



Quick code:

pawn Код:
#include <a_samp>


static PlayerAimPunch[MAX_PLAYERS] = {0, ...};


public OnPlayerWeaponShot(playerid, weaponid, hittype, hitid, Float:fX, Float:fY, Float:fZ)
{
    if(PlayerAimPunch[playerid] == 0)
        PlayerAimPunch[playerid] = 1000;

    SetPlayerDrunkLevel(playerid, PlayerAimPunch[playerid]);
    PlayerAimPunch[playerid] += 100;

    return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(!(oldkeys & KEY_FIRE))
    {
        SetPlayerDrunkLevel(playerid, 0);
        PlayerAimPunch[playerid] = 0;
    }

    return 1;
}
I'm not entirely sure this affects the spread, it's purely a camera effect. If you were shooting a stationary target, your accuracy wouldn't change. Generally, shooting full auto is a terrible idea anyway though.
Reply

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
No, not really. When you use SetPlayerCameraLookAt it actually disconnects the camera from the player. One thing you could do is increase the player's "drunk level" for a short time after shooting for a long time which would shift the camera angle without disconnecting it. You have less control over where it's moving this way but it's better than nothing!
Huh, alright, i'll try that, thanks
Reply

Quote:
Originally Posted by Greggu
Посмотреть сообщение
Huh, alright, i'll try that, thanks
I made an edit to my post with an example
Reply

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
I made an edit to my post with an example
Yeah i've already done it myself with a few defines and gave each gun its own level of recoil and less recoil when crouching, but yours still looks interesting

PHP код:
public OnPlayerWeaponShot(playeridweaponidhittypehitidFloat:fXFloat:fYFloat:fZ)
{
    new 
string[30];
    
    if(
Aiming[playerid] == 1)
    {
        if(
GetPlayerSpecialAction(playerid) == SPECIAL_ACTION_DUCK)
        {
            switch(
weaponid)
            {
                case 
22:
                {
                    
SetPlayerDrunkLevel(playerid2001);
                }
                case 
23:
                {
                    
SetPlayerDrunkLevel(playerid2001);    
                }
                case 
24:
                {
                    
SetPlayerDrunkLevel(playerid2001);
                }
                case 
25:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
26:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
27:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
28:
                {
                    
SetPlayerDrunkLevel(playerid2001);
                }
                case 
29:
                {
                    
SetPlayerDrunkLevel(playerid2001);
                }
                case 
30:
                {
                    
SetPlayerDrunkLevel(playerid2001);
                }
                case 
31:
                {
                    
SetPlayerDrunkLevel(playerid2001);
                }
                case 
32:
                {
                    
SetPlayerDrunkLevel(playerid2001);
                }
                case 
33:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
34:
                {
                    
SetPlayerDrunkLevel(playerid2800);
                }
            }    
        }
        else
        {    
            switch(
weaponid)
            {
                case 
22:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
23:
                {
                    
SetPlayerDrunkLevel(playerid2005);    
                }
                case 
24:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
25:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
26:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
27:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
28:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
29:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
30:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
31:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
32:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
33:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
34:
                {
                    
SetPlayerDrunkLevel(playerid2040);
                }
                case 
35:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
36:
                {
                    
SetPlayerDrunkLevel(playerid2005);
                }
                case 
38:
                {
                    
SetPlayerDrunkLevel(playerid2001);
                }
            }
        }    
        
format(stringsizeof(string), "Drunk Level: %d"GetPlayerDrunkLevel(playerid));
        
SendClientMessage(playerid0xFFFFFFstring);
    }
        
    return 
1;
}
public 
OnPlayerKeyStateChange(playeridnewkeysoldkeys)
{
    if(
PRESSED(KEY_AIM))
    {
        
Aiming[playerid] = 1;
        if(
GetPlayerWeapon(playerid) == 34)
        {
            
SetPlayerDrunkLevel(playerid2070);
        }
    }
    if(
RELEASED(KEY_AIM))
    {
        
Aiming[playerid] = 0;
        
SetPlayerDrunkLevel(playerid0);
    }
    if(
RELEASED(KEY_FIRE))
    {
        
SetPlayerDrunkLevel(playerid0);
    }
    
    return 
1;

Reply

Oy, it's me again, so today i've fiddled around with animations a bit and i've made a script that makes the player roll when landing after a fall.
I wanted to make the script a bit more fleshed out but nor zcmd nor OnPlayerCommandText were working.
The compiler gives me no errors, yet when i'm in the game and i try to perform a command i've written in the script, it will give me UNKNOWN COMMAND for some reason, even this won't work

Код:
#define FILTERSCRIPT

#include <a_samp>
#include <zcmd>

#if defined FILTERSCRIPT 	

#define PRESSED(%0) \
	(((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))

#define RELEASED(%0) \
	(((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0)))	

#define HOLDING(%0) \
	((newkeys & (%0)) == (%0))	

#define KEY_AIM KEY_HANDBRAKE	

new Crouching[MAX_PLAYERS];	
new StealthKillSetup[MAX_PLAYERS];
new TargetID;
new 
	Float: health,
	Float: ang,
	Float: x,
	Float: y,
	Float: z;

public OnFilterScriptInit()
{
	return 1;
}

public OnFilterScriptExit()
{
	return 1;
}

#else

main()
{
	
}

#endif

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
	if(PRESSED(KEY_FIRE))
	{
		if(StealthKillSetup[playerid] == 1)
		{
			ApplyAnimation(playerid, "PED", "GUN_BUTT_crouch", 4.1, 0, 0, 0, 0, 0, 0);
			GetPlayerTargetPlayer(TargetID);
			SetPlayerHealth(TargetID, 0);
		}
	}
	if(HOLDING(KEY_JUMP))
	{
		GetPlayerVelocity(playerid, x, y, z);
		SetPlayerVelocity(playerid, x, y, z + 0.1);		
	}
	return 1;
}

CMD:COSOBUFFO(playerid, params[])
{
	SendClientMessage(playerid, 0xFFFFFF, "X^D");   <-------- Doesn't work for some reason
	return 1;
}

public OnPlayerUpdate(playerid)
{
	if(GetPlayerSpecialAction(playerid) == SPECIAL_ACTION_DUCK)
	{
		Crouching[playerid] = 1;
	}
	if(Crouching[playerid] == 1 && GetPlayerWeapon(playerid == 0 ))
	{
		if(GetPlayerTargetPlayer(playerid) != INVALID_PLAYER_ID)
		{
			StealthKillSetup[playerid] = 1;
		}
	}
	return 1;
}

public OnPlayerTakeDamage(playerid, issuerid, Float:amount, weaponid, bodypart)
{
	if(weaponid == 54)
	{
		ClearAnimations(playerid);
		GetPlayerFacingAngle(playerid, ang);
		SetPlayerFacingAngle(playerid, ang + 90);
		ApplyAnimation(playerid, "ped", "CROUCH_Roll_R", 100, 0, 1, 1, 0, 0, 0);
		GetPlayerHealth(playerid, health);
		SetPlayerHealth(playerid, health + amount);
	}
}
I don't understand why, it doesn't really look like i've done anything wrong so i'm not sure what's happening here
Reply

Quote:
Originally Posted by oMa37
View Post
Just a fast question, not worth making thread for it;
How can i decrease a specific amount from a MySQL table?
Example; I have saved weapon ID 26 with 7 ammo, And i want to take only 3 ammo. so it's gonna be 4 ammo left.
How can i decrease it instead of deleting?
Code:
UPDATE `Table` SET `Column` = `Column` - 3 WHERE `Player` = %d
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)