SA-MP Forums Archive
Having a few scripting questions. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Having a few scripting questions. (/showthread.php?tid=477137)



Having a few scripting questions. - dragonslife - 22.11.2013

Hello guys,

I'm very new to scripting and still trying to learn. I'm currently following a tutorial for it on ******* which I lateron want to edit more into something of myself. Currently (and probably in the future I will too) I am running in to some problems that I don't know by myself how to solve. I appreciate answers I get to help me further, but please keep in mind that I'm trying to learn. So when something is wrong or should be different, please also explain me why that is so rather than just giving me the solution to the problem.

Okay, here's my problem now:

I have the /me and /act command scripted and when I use them ingame it shows the proper message for when /me is done without params. However if I type for example "/me test" is returns the message "unknown command". Can anyone explain to why /me works, but /me with params doesn't? Here's the code:
Код:
CMD:me(playerid, params[])
{
	new string[126];
	
	if(isnull(params))
		return SendClientMessage(playerid, -1, "Usage: /me [action] - For example: /me kneels down and picks up the helmet from the ground.");

	format(string, sizeof(string), "* %s %s", GetName(playerid), params);
	NearbyMessage(playerid, -1, string);
	
	return 1;
}



Re: Having a few scripting questions. - Konstantinos - 22.11.2013

Can you post the NearbyMessage too? Everything else seems to be fine.


Re: Having a few scripting questions. - dragonslife - 22.11.2013

Here's the nearby message:
Код:
stock NearbyMessage(playerid, color, string[])
{
	new Float: PlayerPosition[3];
	
	GetPlayerPos(playerid, PlayerPosition[0], PlayerPosition[1], PlayerPosition[2]);
	
	for(new i; i < MAX_PLAYERS; i++)
	{
		if(IsPlayerInRangeOfPoint(i, PlayerPosition[0], PlayerPosition[1], PlayerPosition[2]))
		{
			SendClientMessage(i, color, string);
		}
	}
}



Re: Having a few scripting questions. - RajatPawar - 22.11.2013

pawn Код:
if(IsPlayerInRangeOfPoint(i, PlayerPosition[0], PlayerPosition[1], PlayerPosition[2]))
You have forgotten the "range" parameter!
https://sampwiki.blast.hk/wiki/IsPlayerInRangeOfPoint
It should be:
pawn Код:
#define NEARBY_RANGE 5.0

if(IsPlayerInRangeOfPoint(i, NEARBY_RANGE,  PlayerPosition[0], PlayerPosition[1], PlayerPosition[2]))



Re: Having a few scripting questions. - Konstantinos - 22.11.2013

pawn Код:
if(IsPlayerInRangeOfPoint(i, PlayerPosition[0], PlayerPosition[1], PlayerPosition[2]))
You forgot the range as parameter. I'm sure it gave you a warning while compiling about number of arguments does not match definition. It's always recommended to use scripts with 0 errors and warnings.

Let's say you want everyone who is in range of 15 meters see the message:
pawn Код:
if(IsPlayerInRangeOfPoint(i, 15.0, PlayerPosition[0], PlayerPosition[1], PlayerPosition[2]))
Or you can change it to your needs.


Re: Having a few scripting questions. - dragonslife - 22.11.2013

Okay thank you very much, that solved my problem of the /me and /act commands.


Now I'm running into the next problem which is with the /pay command:
First I had this:
Код:
CMD:pay(playerid, params[])
{
	new id, amount, pName[MAX_PLAYER_NAME], pName2[MAX_PLAYER_NAME], string[126];
	if(sscanf(params, "ud", id, amount))
	return SendClientMessage(playerid, -1, string);
	
	if(Player[playerid][Money] >= amount)
	{
		Player[playerid][Money] -= amount;
		Player[id][Money] -= amount;
		
		GetPlayerName(playerid, pName, sizeof(pName));
		GetPlayerName(playerid, pName2, sizeof(pName));
		
		
		format(string, sizeof(string), "%s has given %s $%s", pName, pName2, amount);
		NearbyMessage(playerid, -1, string);
	}
	return 1;
}
When that resulted in an empty message (new line in chat, no characters), so I added the following code. The message it should put out when no params are entered is from my /act command. I still need to edit this, I just wanted to see if it worked or not. However it remains with the same result.

Код:
CMD:pay(playerid, params[])
{
	new id, amount, pName[MAX_PLAYER_NAME], pName2[MAX_PLAYER_NAME], string[126];
	if(sscanf(params, "ud", id, amount))
	return SendClientMessage(playerid, -1, string);
	
	if(isnull(params))
	{
		return SendClientMessage(playerid, -1, "Usage: /act [action] - For example: /act You would notice a silver chain with a gold ring hangs arround my neck.");
	}
	else(Player[playerid][Money] >= amount);
	{
		Player[playerid][Money] -= amount;
		Player[id][Money] -= amount;
		
		GetPlayerName(playerid, pName, sizeof(pName));
		GetPlayerName(playerid, pName2, sizeof(pName));
		
		
		format(string, sizeof(string), "%s has given %s $%s", pName, pName2, amount);
		NearbyMessage(playerid, -1, string);
	}
	return 1;
}



Re: Having a few scripting questions. - Konstantinos - 22.11.2013

It returned an empty message because you send as message an empty string (NULL). Take a look closer to the code, you declare string and if params not used, it'll send the empty message.

pawn Код:
CMD:pay(playerid, params[])
{
    new id, amount;
    if(sscanf(params, "ud", id, amount)) return SendClientMessage(playerid, -1, "Usage: /pay <ID/Part Of Name> <amount>");
    if(id == INVALID_PLAYER_ID) return SendClientMessage(playerid, -1, "Invalid player");
       
    if(Player[playerid][Money] >= amount)
    {
        Player[playerid][Money] -= amount; // take the money from the player
        Player[id][Money] += amount; // give the money to the specific player
       
        new pName[MAX_PLAYER_NAME], pName2[MAX_PLAYER_NAME], string[100];
       
        GetPlayerName(playerid, pName, sizeof(pName));
        GetPlayerName(playerid, pName2, sizeof(pName));
       
       
        format(string, sizeof(string), "%s has given %s $%s", pName, pName2, amount);
        NearbyMessage(playerid, -1, string);
    }
    return 1;
}



Re: Having a few scripting questions. - dragonslife - 22.11.2013

Nvm this, noticed the change. Thank you for explaining me!


Re: Having a few scripting questions. - Konstantinos - 22.11.2013

Quote:
Originally Posted by dragonslife
Посмотреть сообщение
One question, you leave this part completly out:
Код:
pName[MAX_PLAYER_NAME], pName2[MAX_PLAYER_NAME], string[126];
Isn't that needed to identify some parts in the code?
No, I didn't!

I used them before using GetPlayerName function.

I prefer declaring things when they're about to be used. In some case such as wrong parameters, it'd return the usage so you declared them without any reason.


Re: Having a few scripting questions. - dragonslife - 22.11.2013

Yeah I noticed, but only after I posted that.. therefor the edit :P

Anyways, I have one more question (note: here comes the question that proves I'm a total starter in this scripting/programming).

I have a function that creates the login screen:
Код:
public OnPlayerConnect(playerid)
{
	new query[126], pName[MAX_PLAYER_NAME];
	
	GetPlayerName(playerid, pName, sizeof(pName));
	
	format(query, sizeof(query), "SELECT * FROM accounts WHERE Name = '%s'", pName);
	mysql_query(query);
	mysql_store_result();
	new rows = mysql_num_rows();
	
	printf("There are %s rows in table1",rows);
	
	if(mysql_num_rows() ==1)
	{
		SendClientMessage(playerid, -1, "That username is registered!");
		ShowPlayerDialog(playerid, 0, DIALOG_STYLE_PASSWORD, "Login", "Please login with the password you made", "Login", "Cancel");
	}
	else
	{
		SendClientMessage(playerid, -1, "That username is not registered. You may register it");
		ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "Register", "Please enter a password for your account", "Register", "Cancel");
	}
	return 1;
}
Now I would like to create the logout command, except I have no idea how I make it return to the public OnPlayerConnect(playerid) function.
I tried return OnPlayerConnect, but that doesn't work... My simplistic brain
Код:
CMD:logout(playerid)
{
	return 1;
}