SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 05/12/2016, 12:17 PM   #3551
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,915
Reputation: 256
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by Hansrutger View Post
Check the script defines: "#define Custom::" and you should see an explanation hopefully. If that is actually thing, it's the first time I am seeing that and am sorry for misleading.
There is no script, I just wrote it. x)
__________________
actor_plus - v5.0.2
I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath
Dayrion is offline   Reply With Quote
Old 05/12/2016, 11:19 PM   #3552
SickAttack
Godfather
 
Join Date: Dec 2013
Posts: 5,113
Reputation: 754
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by RaZVaN ^ xD View Post
I respected the function header he provided in his post, where number was an integer, not a string. Also, this code is just a snippet I wrote in 2 minutes, based on a simple algorithm, sorry for not having enough time to optimize the hell out of it, even if these micro optimizations have little to no impact in reality.

However, if we're at this subject, then your function is not that bright, too:
- you don't check anywhere if array used to store the counter is large enough
- function would return invalid results for negative numbers
- instead of creating a 2 cell string, copying every character to that string, and then converting it to a number, you could have done something like this instead: "array[(string[i]-'0')] ++;"
- should I bring into discussion the differencies between pre- and post- increments? nah


So, why is my function ridiculous?
Just by pointing out the while, it's to be noted that you ruined it starting there.

A lot of what you said is invalid, so there's no need to quote it (array large enough?...).

-----------

Mine can obviously be improved, it's just something to get him started on. But processing an integer is just ridiculous.
SickAttack is offline   Reply With Quote
Old 06/12/2016, 07:08 PM   #3553
RaZVaN ^ xD
Huge Clucker
 
Join Date: Jul 2012
Posts: 298
Reputation: 14
Default Re: Little coding questions - For general minor queries 5

Quote:
Originally Posted by SickAttack View Post
Just by pointing out the while, it's to be noted that you ruined it starting there.

A lot of what you said is invalid, so there's no need to quote it (array large enough?...).

-----------

Mine can obviously be improved, it's just something to get him started on. But processing an integer is just ridiculous.
It seems I can't have an argumented discussion, you just keep saying that my code is "ridiculous" still you don't give any reasons why it is so, while I did mention why I chose to post an alternative to your code. (-- respecting his function header --)

I admit that number/digit processing is much easier using strings, but if you would have ******'d that modulo 10 algorithm I used, you'd have seen that it is very, very, very basic.

-- end of discussion
RaZVaN ^ xD is offline   Reply With Quote
Old 06/12/2016, 08:45 PM   #3554
jbankss
Little Clucker
 
Join Date: Jun 2016
Posts: 46
Reputation: 0
Default Re: Little coding questions - For general minor queries 5

SQLite:

I thought the following would work but the count always ends up as 0 regardless of how many vehicles I have in the DB, any clues as to why its not working?

PHP Code:
    format(Query,sizeof(Query), "SELECT * FROM `VEHICLEDATA` WHERE `OwnerID` = %i"PlayerInfo[playerid][UserID]);
    
Result db_query(DatabaseQuery);
    
count db_num_rows(Result);
    
db_free_result(Result); 
??
jbankss is offline   Reply With Quote
Old 06/12/2016, 09:04 PM   #3555
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,989
Reputation: 2649
Default Re: Little coding questions - For general minor queries 5

Don't use num_rows if you're only interested in the count. That's inefficient. Use the count() aggregate function instead and retrieve the result as an int.
PHP Code:
SELECT COUNT(*) AS numVehicles FROM ... 
__________________
Vince is offline   Reply With Quote
Old 06/12/2016, 09:34 PM   #3556
jbankss
Little Clucker
 
Join Date: Jun 2016
Posts: 46
Reputation: 0
Default Re: Little coding questions - For general minor queries 5

Fixed - thanks.

Last edited by jbankss; 07/12/2016 at 01:06 PM.
jbankss is offline   Reply With Quote
Old 07/12/2016, 01:16 AM   #3557
Hansrutger
Gangsta
 
Hansrutger's Avatar
 
Join Date: May 2011
Location: Sweden
Posts: 928
Reputation: 158
Default Re: Little coding questions - For general minor queries 5

Returning arrays from functions... So with that being said, is it possible? I am not talking about strings/char arrays but rather arrays that include for instance normal integers or even floats. I can't seem to get it working, but this is kinda what I am trying to accomplish:
Code:
main() //I'm not actually calling it from here... But just for the example written in this post
{
    new array[MAX_PLAYERS];
    array = MyFunction();
}

MyFunction()
{
    //fill an array with ints
    new array[MAX_PLAYERS];
    for (new i = 0; i < MAX_PLAYERS; i++)
    {
	array[i] = 5;
    }
    return array;
}
Now this will work fine if we were talking about a char array because you have "format" to assist you when you are calling the function, but when it comes to integers I am not really sure what to do.

I have also tried to reference it but no luck there...
Code:
main()
{
    new array[MAX_PLAYERS];
    MyFunction(array);
}

MyFunction(&array[])
{
    for (new i = 0; i < MAX_PLAYERS; i++)
    {
	array[i] = 5;
    }
}
I, of course, can just simply skip this and not use an array in my actual scenario by simply itterate "MyFunction()" as many times I want and get the integers one by one, however I was curious about how this works and if it is possible to return an integer/float array? Maybe it's something simple, that I am missing. Anyhow thanks in advance for help!
__________________


Hansrutger is offline   Reply With Quote
Old 07/12/2016, 07:33 AM   #3558
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,915
Reputation: 256
Default Re: Little coding questions - For general minor queries 5

Hi'.
I probably (99%) misunderstood what is the 'problem' there. If I don't, this is a great problem.
In function, array are passed by reference so you don't need &.
If you want to set a value for a whole array like you said (I think), your example were correct:
You can test it:
PHP Code:
main()
{
    new array[
10];

    
MyFunction(array, _99); //Passing second param'

    
for(new i10i++)
    {
        
printf("\n %i:\n%i",i, array[i]);
    }
}

MyFunction(string[], len sizeof(string), value 5)
{
    for (new 
0leni++)
    {
        
string[i] = value;
    }

Apologies if I misundestood. /:
__________________
actor_plus - v5.0.2
I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath
Dayrion is offline   Reply With Quote
Old 08/12/2016, 11:23 PM   #3559
Hansrutger
Gangsta
 
Hansrutger's Avatar
 
Join Date: May 2011
Location: Sweden
Posts: 928
Reputation: 158
Default Re: Little coding questions - For general minor queries 5

Code:
MyFunc(arr[])
{
	for (new i = 0; i < 10; i++)
	{
		arr[i] = 5;
	}
}

CMD:test(playerid, params[])
{
	new array[10];
	MyFunc(array);
	for (new i = 0; i < 10; i++)
	{
		printf("Iterating on position %i | Value: %i", i, array[i]);
	}
	
	return 1;
}
Perfecto, thank you very much! The problem I had with my own scenario was solved by iterating the function multiple times which is the same thing, in terms of result. Just tested your way to confirm, thank you very much kind sir! :P
__________________


Hansrutger is offline   Reply With Quote
Old 10/12/2016, 06:43 AM   #3560
Logic_
High-roller
 
Logic_'s Avatar
 
Join Date: Jun 2015
Location: 666
Posts: 1,493
Reputation: 267
Default Re: Little coding questions - For general minor queries 5

I'm receiving error 'Argument type mismatch (argument 3)' when I try to compile my gamemode with the function called 'GetItemName'.

PHP Code:
// My array
enum E_ITEM
{
    
ItemName[20]
};
new 
gItem[][E_ITEM] =
{
    {
"something something"}
};

// My function
GetItemName(item)
{
    return 
gItem[item][ItemName];
}

// Error line
PlayerTextDrawSetString(playeridtext[playerid], GetItemName(PlayerStat[playerid][INV_SLOT1])); 
I can also use gItem[PlayerStat[playerid][INV_SLOT1]][ItemName] but I don't want to use it.
__________________
Premium quality and reasonable pricing game Server hosting

Last edited by Logic_; 10/12/2016 at 07:25 AM.
Logic_ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make a soldier aotumaticly be general when the general dies ? AlbanianGuy Scripting Help 35 13/05/2012 03:35 PM
Minor problem Desert Help Archive 4 17/03/2010 06:24 PM
Counting with mysql queries actiwe Help Archive 2 26/02/2010 02:36 PM
SQL queries using GROUP BY Dabombber Help Archive 2 05/10/2009 07:47 PM


All times are GMT. The time now is 06:06 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.