Little coding questions - For general minor queries 5

Constants are generally preferred over definitions: `const something = 5;`. They provide an actual name for the value so any warnings or errors will provide more information at compile time. `#define` is effectively search-and-replace at compile-time which can lead to some non-obvious issues.
Reply

Quote:
Originally Posted by Nathan94
View Post
Hello sampers, I was wondering what's the difference between "#define something 5" and "#define something (5)"?
If it's a single constant there is no difference.But if it was an expression for example
PHP Code:
#define EIGHT_PLUS_TWO 8+2 
and
PHP Code:
#define EIGHT_PLUS_TWO (8+2) 
are different.

lets see the first case
PHP Code:
#define EIGHT_PLUS_TWO 8+2
main(){
    new 
result EIGHT_PLUS_TWO;
    
ASSERT(20==result);//will fail as result is 18

the above one is expanded to
Code:
new result = 2 * EIGHT_PLUS_TWO = 2 * 8+2 = 16+2 = 18 as per operator precedence
But in the second case
PHP Code:
#define EIGHT_PLUS_TWO (8+2)
main(){
    new 
result EIGHT_PLUS_TWO;
    
ASSERT(20==result);//will pass as one in parentheses is evaluated first

is expanded to
Code:
result = 2 * EIGHT_PLUS_TWO = 2 * (8+2) = 2*(10) = 20 as per operator precedence
Reply

Hi,

I've a few questions related to MySQL.
  1. What's the BEST way to store player records (IP, admins, bans, etc)
  2. And, what's the best way to store configuration data in the database? Such as:
PHP Code:
CREATE TABLE IF NOT EXISTS "#TABLE_SERVER_CFG" (\
    `
weatherINTEGER, \
    `
timeINTEGER, \
    `
double_scoreINTEGER, \
    `
motdVARCHAR(100), \
    `
amotdVARCHAR(100)\

I don't think it's a good use of MySQL to store such information and I should use INI or CFG files. But, I wish to keep everything all-together in one database. And the data is supposed to be updated on server shutdown or during the runtime.
Reply

You're right, SQL is not good for that. But if you are to, do one item per row, not column. So just a few generic `key` and `value` columns, then each config option separately. Similar for player records. An `admins` table with one per row; a `bans` table etc.
Reply

Hello,

I've been adding vehicles to my samp server by using the samp debug however i've discovered there was an easier way to add cars and objects to my server and that is from fusez's map editor.

Now, if i add objects and cars to my map using fusez's map editor and when i save it how would i add the objects and cars used from that filterscript into my main server? Also would it completely erase the vehicles i've added by using the samp debug?
Reply

Just load the FS.

No.
Reply

Is there any json_decode function? Need to store a json array to an Array in pawn.
Reply

Quote:
Originally Posted by justinnater
View Post
Is there any json_decode function? Need to store a json array to an Array in pawn.
https://sampforum.blast.hk/showthread.php?tid=543919

Quite straightforward to use for something as simple as your request to be fair.
Reply

Quote:
Originally Posted by ComDuck
View Post
https://sampforum.blast.hk/showthread.php?tid=543919

Quite straightforward to use for something as simple as your request to be fair.
Thanks, I am using it now and it's been a big help.
I am using it to retrieve JSON data from the HTTP function.

Although there is one thing that is bothering me about this HTTP function... The response data is being send to a callback.

Would it be possible to create a function that will make the HTTP function return the response data to itself instead of a callback?


So you could use it like this for example:
HTML Code:
new response = HTTP(playerid, HTTP_GET, "random.com", "");
Reply

Quote:
Originally Posted by justinnater
View Post
Thanks, I am using it now and it's been a big help.
I am using it to retrieve JSON data from the HTTP function.

Although there is one thing that is bothering me about this HTTP function... The response data is being send to a callback.

Would it be possible to create a function that will make the HTTP function return the response data to itself instead of a callback?


So you could use it like this for example:
HTML Code:
new response = HTTP(playerid, HTTP_GET, "random.com", "");
I don't think it is possible, you or someone else are welcome to surprise me. Calling HTTP won't just return the response code, it will also show the output data based on the URL requested through the parameters if if the response code is a 200 (OK). I guess in the bigger picture, it is much more efficient to design it that way.

EDIT: A different JSON plugin follows the same design: https://sampforum.blast.hk/showthread.php?tid=653362
Reply

I've just found another problem with the HTTP function.

I'm sending two HTTP requests (Those two functions have a different callback where the response has to go to) in one function, the problem is that it will only react to the first callback that gets a response first, the other one wont be called after. Is this behavior normal or could it be something else?
Reply

Just don't use the built-in HTTP function at all. There are better plugins, such as "requests".
Reply

What is the best way to store items attached to a player in the mysql database? What should be the columns?

I was thinking over:
PHP Code:
 _______________________________________
|   ...........items....................|
 ---------------------------------------
id       int(8), PRIMARY KEYA_I   |
pid      int(8FOREIGN KEY (pid)   | // player register id from `players`
modelid  int(8)                     |
slot     int(8)                     |
boneid   int(8)                     |
fOffsetX Float                      |
fOffsetY Float                      |
fOffsetZ Float                      |
fRotX    Float                      |
fRotY    Float                      |
fRotZ    Float                      |
fScaleX  Float                      |
fScaleY  Float                      |
fScaleZ  Float                      |
 --------------------------------------- 
This will be working, I think, but if player is using all slots (0-9), will be 10 rows per player, what do you think about this? maybe you have better ways?
Reply

Quote:
Originally Posted by CherryMond
View Post
What is the best way to store items attached to a player in the mysql database? What should be the columns?

This will be working, I think, but if player is using all slots (0-9), will be 10 rows per player, what do you think about this? maybe you have better ways?
I don't think there's any other way.

I don't see any redundant rows, and all your data dependencies make sense (based on https://sampwiki.blast.hk/wiki/SetPlayerAttachedObject). Although, I was thinking of an idea where you can compress each XYZ coordinates into a single line and separate the X, Y and Z float values with a separator character (-, /, etc), but why do all that extra work for your case? I don't even think it's a good idea anyways. What if you only want to modify a single coordinate value only?
Reply

The design is OK. If you set pid+slot as UNIQUE key, you can use INSERT INTO .. ON DUPLICATE.

Quote:
Originally Posted by ComDuck
View Post
Although, I was thinking of an idea where you can compress each XYZ coordinates into a single line and separate the X, Y and Z float values with a separator character (-, /, etc), but why do all that extra work for your case? I don't even think it's a good idea anyways. What if you only want to modify a single coordinate value only?
Exactly, it is a bad idea.
Reply

Thank you for your answer, in which case it will be as it is.
Reply

Quote:
Originally Posted by ComDuck
View Post
Although, I was thinking of an idea where you can compress each XYZ coordinates into a single line and separate the X, Y and Z float values with a separator character (-, /, etc), but why do all that extra work for your case? I don't even think it's a good idea anyways. What if you only want to modify a single coordinate value only?
That isn't a compression. That isn't good in any way. SQL can store floats as numbers internally. Your method would convert it to a string first, losing all the optimizations available. What's more, storing bits of data together is exactly what SQL is already designed to do very very well. So you are badly replicating part of the features of a database in the database, instead of just using the native functionality.
Reply

in which way do we make commands with multiple functions?

for example: /giveplayer [id] [money/health/armor] [value]

thanks
Reply

Quote:
Originally Posted by v4yne1
View Post
in which way do we make commands with multiple functions?

for example: /giveplayer [id] [money/health/armor] [value]

thanks
HERE is a well-written tutorial about the whole thing. Good luck.
Reply

Quote:
Originally Posted by polygxn
View Post
HERE is a well-written tutorial about the whole thing. Good luck.
thanks man
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)