Little coding questions - For general minor queries 5

RajatPawar, the indexes are wrong in the loop and it could cause run time error 4 if the rows were more than 3.

pawn Код:
#include <a_samp>

new dimarray[][] =
{
    {10, 69, 27},
    {22, 33, 44},
    {15, 16, 9}
};

main() {}

public OnGameModeInit()
{
    PrintArray(dimarray, 0, sizeof (dimarray));
    return 1;
}

stock PrintArray(array[][], index, size)
{
    for (new i; i != size; ++i)  printf("%i", array[i][index]);
    return 1;
}
You can access any index you want of the 2nd dimension for all the rows. But it will work only for 2D arrays.
Reply

A little coding question...
Just wanted to ask which format type a player's IP is. I mean something like an integer. Cause I tried to make /banip cmd, which failed. I checked it and thought everything was correct, and defined the IP as a Float, which I think is atual mistake.
Reply

Quote:
Originally Posted by NaClchemistryK
View Post
A little coding question...
Just wanted to ask which format type a player's IP is. I mean something like an integer. Cause I tried to make /banip cmd, which failed. I checked it and thought everything was correct, and defined the IP as a Float, which I think is atual mistake.
IPs are strings.
Reply

The vehicle I added as a static vehicle does not appear when a player requests a class.

Facts -

1) The static vehicle has been created under OnGameModeInit.
2) The interior and virtual worlds of both the player and vehicle are the SAME.
3) There are NO other filterscripts running that might be interfering.
4) When I connect and try to login, the car won't show up. But when I force class selection AFTER spawning once, the vehicle appears.

The 4th fact can be demonstrated by -

First time after connect (immediately) -



After dying, forcing class selection -



My code -

pawn Code:
new vehS = 0;

public OnGameModeInit()
{
     ...
     vehS = AddStaticVehicle(429,-1538.4653,596.7906,6.8617,38.9806,13,13);
     ...
     return 1;
}

public OnPlayerRequestClass(playerid,classid)
{
    SetPlayerPos(playerid,-1538.8669,594.3477,7.1813);
        SetPlayerFacingAngle(playerid,130.4796);
        SetPlayerVirtualWorld(playerid, 0);

    SetPlayerCameraPos(playerid, -1543.0037,590.2316,7.1875);
    SetPlayerCameraLookAt(playerid, -1538.5853,596.4193,9.4897);
   
    SetVehicleVirtualWorld(vehS, GetPlayerVirtualWorld(playerid));
    LinkVehicleToInterior(vehS, GetPlayerInterior(playerid));
    return 1;
}
I've added debugs, etc, nothing. I've tested this with [uL] Pottus - he considers this to be a SA-MP streaming issue (and thanks, by the way!). I'd be glad to receive any help!

Thanks!
Reply

Quote:
Originally Posted by Konstantinos
View Post
IPs are strings.
ok, what the fuckі...
I believe you, but
I just need an explenation, how can ip be a string when there are numbers in it? strings are actually literal made by humans, which are represented by numbers when the script reads it(that's how I know it). But Numbers represent themselves, and a IP has numbers. why isn't it an integer or float?
Reply

An IP has 3 dots in it and that is neither an integer nor a floating-point number so it must be a string for obvious reasons.

Also IPs can be converted to an integer but it reaches ~4.2 billions and the limit in PAWN is 32-bit so it will
go negative if exceeds.
Reply

Alright... so it can't be float, cause a float is a number that has a single " . "
But just a lil' question, looking at your arguments made me think that this sentence I said was incorrect:
Quote:

strings are actually literal words made by humans, which are represented by numbers when the script reads it

And if it is incorrect, please tell me the correct definition..
And thanks for all the help you gave me, Luffy-sensei
Reply

Strings are arrays that store a text and yes, each character is actually a number.
Reply

Ah yes, that makes sense. Thank you. (:
Reply

i've got a weird problem in Floatcos, floatsin.
the code i use is:
pawn Code:
CMD:test1(playerid, params[])
{
    new Float:X, Float:Y, Float:Z, Float:ang, obj, Float:Xx, Float:Yy, Float:Zz, Float:fX, Float:fY, str[128]
    GetPlayerFacingAngle(playerid, ang);
    GetPlayerPos(playerid, X, Y, Z);
    GetXYInFrontOfPlayer(playerid, 45, fX, fY);
    obj = CreateObject(345, fX, fY, Z, 0, 0, 0);
    GetObjectPos(obj, Xx, Yy, Zz);
    format(str, sizeof str, "X = %f, Y = %f, Z=%f Your pos : %f ,%f, %f", Xx, Yy, Zz, X, Y, Z);
    SendClientMessage(playerid, -1, str);
    return 1;
}
and Rajat suggested my to try this code:
pawn Code:
CMD:spawntest(playerid)
{
    new Float: fX, Float: fY, Float: fZ, ob;
    GetXYInFrontOfPlayer(playerid, 45.1, fX, fY);
    MapAndreas_FindZ_For2DCoord(fX, fY, fZ);
 
    ob = CreateObject(345, fX, fY, fZ, 0, 0, 0);
    return 1;
}
Both codes gave the same result.
The objects spawned in different direction, the first spawn in my right side and after i moved some place and looked at a different place, it spawned the object in my left side and so on. it spawned the object infront of my when i was looking in the North-East.

GetXYInFrontOfPlayer:
pawn Code:
GetXYInFrontOfPlayer(playerid, Float: distance, &Float: rX, &Float: rY)
{
     new Float: x, Float: y, Float: z, Float: angle;
     
     GetPlayerPos(playerid, x, y, z);
     GetPlayerFacingAngle(playerid, angle);

     rX = (x + (distance * floatcos(-angle, degrees)));
     rY = (y + (distance * floatsin(-angle, degrees)));
}
EDIT: Fixed. rX should be floatsin and vice versa.

EDIT2:
Is there anyway i can check whether the player has pressed the Jump Key other than in OnPlayerKeyChangeState (im planning the check it in a timer)
Reply

GetPlayerKeys just gets the up, down, left and right anyway i fixed it.
Is there anyway i can lock the players camera i mean limit it so the player cant see 360degree but only 90 degree
Reply

Hello guys, i need your help here... How can i set a house interior with this script code? And to change the level if possible... Thank you

Code:
CMD:hedit(playerid, params[])
{
	new bizid, string[128], input;
	if(!IsPlayerLoggedIn(playerid)) return SendClientMessage(playerid, COLOR_GREY, "You need to login first before using any command.");
	if(PlayerInfo[playerid][pAdmin] < 4 && !PlayerInfo[playerid][pHouseMod]) return SendClientMessage(playerid, COLOR_GREY, "You are not an authorized to use this command.");
//    if(!aDuty[playerid]) return SendClientMessage(playerid, COLOR_GREY, "You are not on admin duty.");
	if(sscanf(params, "s[32]", params))
	{
		SendClientMessage(playerid, COLOR_WHITE, "USAGE: /hedit [option] [houseid]");
		SendClientMessage(playerid, COLOR_GREY, "OPTIONS: location | price | level");
		return 1;
	}
	if(!strcmp(params, "location", true, 8))
	{
	    if(sscanf(params, "s[32]i", params, bizid)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /hedit location [house]");
        new idx = bizid;
        if(!HouseInfo[bizid][hLevel]) return SendClientMessage(playerid, COLOR_GREY, "Invalid house id.");
		GetPlayerPos(playerid, HouseInfo[idx][hX], HouseInfo[idx][hY], HouseInfo[idx][hZ]);
		DestroyDynamicPickup(HouseInfo[idx][hPickup]);
    	HouseInfo[idx][hPickup] = CreateDynamicPickup(1273, 1, HouseInfo[idx][hX], HouseInfo[idx][hY], HouseInfo[idx][hZ], 0);
		DestroyDynamic3DTextLabel(HouseInfo[idx][hText]);
        if(!strcmp("The State", HouseInfo[idx][hOwner])) format(string, sizeof(string), ""COE"["CWE"%d, House (Lvl: %d)"COE"]
"COE"Owner"CWE" %s
"COE"Status"CWE" For Sale
"CWE"$%d", idx, HouseInfo[idx][hLevel], HouseInfo[idx][hOwner], HouseInfo[idx][hPrice]);
		else format(string, sizeof(string), ""COE"["CWE"%d, House (Lvl: %d)"COE"]
"COE"Owner"CWE" %s
"COE"Status"CWE" %s", idx, HouseInfo[idx][hLevel], HouseInfo[idx][hOwner], RHS(idx));
		HouseInfo[idx][hText] = CreateDynamic3DTextLabel(string, COLOR_WHITE, HouseInfo[idx][hX], HouseInfo[idx][hY], HouseInfo[idx][hZ]+0.3, 15);
	    format(string, sizeof(string), "{FF0000}[Admin Warn]{FF6347} %s has changed house ID %d\'s location.", RPN(playerid), bizid);
		SendAdminMessage(COLOR_DARKRED, 1, string);
		Log("logs/house.log", string);
	}
	else if(!strcmp(params, "price", true, 5))
	{
    	if(PlayerInfo[playerid][pAdmin] < 4 && !PlayerInfo[playerid][pHouseMod]) return SendClientMessage(playerid, COLOR_GREY, "You are not an authorized to use this command.");
	    if(sscanf(params, "s[32]ii", params, bizid, input)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /house price [house] [price]");
        new idx = bizid;
		if(!HouseInfo[idx][hLevel]) return SendClientMessage(playerid, COLOR_GREY, "Invalid house id.");
		if(strcmp("The State", HouseInfo[bizid][hOwner])) return SendClientMessage(playerid, COLOR_GREY, "You can\'t edit the price of owned businesses.");
	    HouseInfo[bizid][hPrice] = input;
	    format(string, sizeof(string), ""COE"["CWE"%d, House (Lvl: %d)"COE"]
"COE"Owner"CWE" %s
"COE"Status"CWE" For Sale
"CWE"$%d", idx, HouseInfo[idx][hLevel], HouseInfo[idx][hOwner], HouseInfo[idx][hPrice]);
		UpdateDynamic3DTextLabelText(HouseInfo[bizid][hText], COLOR_WHITE, string);
	    format(string, sizeof(string), "{FF0000}[Admin Warn]{FF6347} %s has set house ID %d\'s price to $%d.", RPN(playerid), bizid, input);
		SendAdminMessage(COLOR_DARKRED, 1, string);
		Log("logs/house.log", string);
	}
	else if(!strcmp(params, "level", true, 5))
	{
		if(PlayerInfo[playerid][pAdmin] < 4 && !PlayerInfo[playerid][pHouseMod]) return SendClientMessage(playerid, COLOR_GREY, "You are not an authorized to use this command.");
	    if(sscanf(params, "s[32]ii", params, bizid, input)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /house level [houseid] [level]");
        new idx = bizid;
    	//if(!HouseInfo[bizid][hLevel]) return SendClientMessage(playerid, COLOR_GREY, "Invalid house id.");
    	if(input < 1 || input > 11) return SendClientMessage(playerid, COLOR_GREY, "Levels are between 1 and 11.");
	    HouseInfo[bizid][hLevel] = input;
        if(!strcmp("The State", HouseInfo[idx][hOwner])) format(string, sizeof(string), ""COE"["CWE"%d, House (Lvl: %d)"COE"]
"COE"Owner"CWE" %s
"COE"Status"CWE" For Sale
"CWE"$%d", idx, HouseInfo[idx][hLevel], HouseInfo[idx][hOwner], HouseInfo[idx][hPrice]);
		else format(string, sizeof(string), ""COE"["CWE"%d, House (Lvl: %d)"COE"]
"COE"Owner"CWE" %s
"COE"Status"CWE" %s", idx, HouseInfo[idx][hLevel], HouseInfo[idx][hOwner], RHS(idx));
		UpdateDynamic3DTextLabelText(HouseInfo[bizid][hText], COLOR_WHITE, string);
	    format(string, sizeof(string), "{FF0000}[Admin Warn]{FF6347} %s has set house ID %d\'s level to %d.", RPN(playerid), bizid, input);
		SendAdminMessage(COLOR_DARKRED, 1, string);
		Log("logs/house.log", string);
	}
	return 1;
}
[/QUOTE]
Reply

Thanks! I did know about that feature of the Streamer plugin. It'll help me a lot.
Reply

Hi.

Which one of these three ways is the faster to load vehicles ? I can't run a benchmark on my computer because it's too slow and I think results will be corrupted.

First method :

PHP Code:

stock vehicles_Load_1
()
{
    new 
query[128];
    
mysql_query(MySQL"SELECT `id` FROM `Vehicules` ORDER BY DESC LIMIT 1");
    new 
id cache_get_row_int(01);
    for(new 
1idi++)
    {
        
format(querysizeof(query), "SELECT model, x, y, z, angle, col1, col2, respawn FROM Vehicles WHERE id = %d LIMIT 1"i);
        
mysql_tquery(MySQLquery"OnQueryVehicleDone""i"id);
    }
    return 
1;
}

forward OnQueryVehicleDone(id);
public 
OnQueryVehicleDone(id)
{
    
model cache_get_row_int(01);
    
cache_get_row_float(02);
    
cache_get_row_float(03);
    
cache_get_row_float(04);
    
angle cache_get_row_float(05);
    
col1 cache_get_row_int(06);
    
col2 cache_get_row_int(07);
    
respawn cache_get_row_int(08);
    if(
respawn == '\0'respawn = -1;
    
CreateVehicle(modelxyzanglecol1col2respawn);
    return 
1;

Second method :

PHP Code:

stock vehicles_Load_2
()
{
    
mysql_tquery(MySQL"SELECT * FROM Vehicules""LoadVehicles""");
    return 
1;
}

forward LoadVehicles();
public 
LoadVehicles()
{
    new 
nbrVeh cache_get_row_count();
    for(new 
1nbrVeh;v++)
    {
        
model cache_get_row_int(01);
        
cache_get_row_float(02);
        
cache_get_row_float(03);
        
cache_get_row_float(04);
        
angle cache_get_row_float(05);
        
col1 cache_get_row_int(06);
        
col2 cache_get_row_int(07);
        
respawn cache_get_row_int(08);
        if(
respawn == '\0'respawn = -1;
        
CreateVehicle(modelxyzanglecol1col2respawn);
    }
    return 
1;

Third method :

PHP Code:

stock vehicles_Load_3
()
{
    
mysql_tquery(MySQL"SELECT * FROM Vehicules""Loading_Third");
    return 
1;
}

forward Loading_Third();
public 
Loading_Third()
{
    new 
rowsfieldsdata[50];
    
cache_get_data(rowsfieldsMySQL);
    for(new 
0rowsi++)
    {
        
// Variables
        
cache_get_field_content(i"model"data);          model strval(data);
        
cache_get_field_content(i"x"data);              floatstr(data);
        
cache_get_field_content(i"y"data);              floatstr(data);
        
cache_get_field_content(i"z"data);              floatstr(data);
        
        
cache_get_field_content(i"angle"data);        angle strval(data);
        
cache_get_field_content(i"col1"data);        col1 strval(data);
        
cache_get_field_content(i"col2"data);            col2 strval(data);
        
cache_get_field_content(i"respawn"data);         respawn strval(data);
        if(
respawn == '\0'respawn = -1;
        
        
// On ajoute le vйhicule
        
AddStaticVehicleEx(modelxyzanglecol1col2respawn);
    }

Thanks in advance.
Reply

The first one is out of the question. Running multiple queries is never a good idea when you can just run one query to get the same data.

Now, from what I can see the only real difference between the last two is the "cache_get_field_" functions. I would imagine it's faster to just address each row by ID (one cell) rather than a string (a bunch of cells) but I'm not sure how MySQL works internally for that. The second looks way easier to write though and you don't need to mess around with a cache variable to store the value in then assign that to the target (also I noticed that requires using strval/floatstr).

The second option looks like the best method.
Reply

Quote:
Originally Posted by [HLF]Southclaw
View Post
The first one is out of the question. Running multiple queries is never a good idea when you can just run one query to get the same data.

Now, from what I can see the only real difference between the last two is the "cache_get_field_" functions. I would imagine it's faster to just address each row by ID (one cell) rather than a string (a bunch of cells) but I'm not sure how MySQL works internally for that. The second looks way easier to write though and you don't need to mess around with a cache variable to store the value in then assign that to the target (also I noticed that requires using strval/floatstr).

The second option looks like the best method.
I agree with Southclaw, 2nd option would be the best method, if you want it more better you could try avoiding *.

Your query would look like

pawn Code:
SELECT model, x, y, z, angle, col1, col2, respawn FROM Vehicules
Here's an explanation why you should avoid *

Quote:
Avoid SELECT *

The more data is read from the tables, the slower the query will become. It increases the time it takes for the disk operations. Also when the database server is separate from the web server, you will have longer network delays due to the data having to be transferred between the servers.

It is a good habit to always specify which columns you need when you are doing your SELECT's.
Reply

Okay then, thanks you for the help.
Reply

Is there a way to get numargs/getarg to work with multidimensional arrays? This is my current code
Code:
stock funcname(playerid, ...)
{
	new args = numargs(), index, str[1024];
	for(new i = 1; i < args; i++)
	{
		index = 0, strdel(str, 0, sizeof(str));
		do str[index] = getarg(i, index);
		while (str[index++] != 0);
        }
}
Works like a charm if i call it like funcname(playerid, "hello", "world"); but I don't always know the amount of arguments i want to insert. Instead I wanted to use a multidimensional array
Code:
new str[][] = {"hello", "world"};
funcname(playerid, str);
But I don't know how to get the values since getargs only seems to support simple arrays as arguments.
Reply

If you want to use an multidimensional you wont need getarg anymore, do you ?
pawn Code:
stock funcname(playerid, array[][], size = sizeof array) {}
If you want to access a multidimensional arrray you should know how it gets passed to the function
Normal arrays just look like that hello\0 or world\0 and a two dimensional would look like \8\28hello\0world\0
The first two numbers represent the offset from theirs position in bytes
Reply

Actually i wanted to use an unknown number of multidimensional arrays, unfortunately my example was not very clear. The second part of your answer is exactly what i needed, thank you very much. Is there a way to determine the length of "\8\28hello\0world\0"? I want to count \0s to get \8\28 data and then explode the rest of the string. Or how would you fetch the multidimensional array?
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)