Get Data from MySQL in pawn for #define
#1

Hi,
I think it's immposible but i need to ask
I want to know is possible to get any data before load gamemode.For example I want to give admins possibility to Edit some messages/etc(I have many idea about it,but i don't know can I do it,so is possible to make something like this:

Код:
#define TEST_MESSAGE "Test message"
But I need to load TEST_MESSAGE from MySQL database for example,is it possible on any way?

And in my mode

Код:
SendClientMessage(playerid,-1,TEST_MESSAGE);
Because I don't want to load from my MySQL database every message in game..I think it's unnecessarily.
Reply
#2

Yes it's very possible, everything is possible in coding!

You just need arrays instead of macros which are constants and you cannot change constants.

Here is a relative example: (note: i haven't used mysql in a while, syntax can be different in latest version)
PHP код:
#define MAX_MESSAGES 10
#define MAX_MESSAGE_SIZE 144
new myMessages[MAX_MESSAGES][MAX_MESSAGE_SIZE];
public 
OnGameModeInit() {
    new 
mySQL mysql_connect(...);
    
mysql_tquery(mySQL
        
"SELECT * FROM your_table LIMIT "#MAX_MESSAGS"", 
        
"OnMessagesLoad""\1"
    
);
}
forward OnMessageLoad();
public 
OnMessageLoad() {
    for (new 
0cache_get_row_count(); i++) {
        
cache_get_field_content(i"field_name"myMessages[i], MAX_MESSAGE_SIZE);
    }
    
// now you have all messages saved in your array "myMessages"
    // to access them, say print a message at index 5
    
printf("message 5: %s"myMessage[5]);

Reply
#3

It's ok for 5 messages,but what if I want to store all possibility messages...I don't think that it's good idea..I know for it,it's reason why I'm ask is possible to get and store it in #define..(That pawno before compile get data from mysql database,for example..I don't know is it possible with other programs or...)
Reply
#4

Wait, something wrong here. So you have plenty of messages(i reckon they are static messages(you change them from time to time)). If is right what im saying, why you want to load from a database in first place? You can dedicate a place in your script to put all of them as #define
Reply
#5

You can't load the messages to put in a define.
#define is a precompiler instruction and is used to replace text in the script upon compiling.
This happens before your script is even executed, and at that point, there is no connection to any database yet.

After compiling, the #define isn't in the amx file.

But you can do it as Gammix told you: use arrays.

Why would you even use defines to hold messages?
A define is static and cannot be changed during runtime.

Using arrays, you can allow the messages to be added/edited/removed from the database and script during runtime.
They're just more flexible and can be expanded easily.
In Gammix's code, you simply need to modify the MAX_MESSAGES define to allow more messages to be held in memory by your script/server.
Reply
#6

Just use simple variables to define the message you want. I dont see any bad in it. You can set only you or other allowed person can change the message.
Reply
#7

Maybe you are don't understand me,i'm think that Admins can change,edit ALL messages on server.All possibilty SendClientMessage i want to make editable,etc...I have Roleplay mode,so there have many messages,i think it's not good idea to have something like this:

Quote:

new myMessages[2000][144];

Reply
#8

Quote:
Originally Posted by GospodinX
Посмотреть сообщение
It's ok for 5 messages,but what if I want to store all possibility messages...I don't think that it's good idea..I know for it,it's reason why I'm ask is possible to get and store it in #define..(That pawno before compile get data from mysql database,for example..I don't know is it possible with other programs or...)
Since dynamics arrays doesn't exist in pawn, you have to define a limit by yourself. After that, it depends on how you store your messages on your database. I recommend you to use data normalization, there is a great tutorial which use that "technique" for weapons : https://sampforum.blast.hk/showthread.php?tid=505081
Reply
#9

Just saying, with what Gammix showed, you could probably make that paginate and allow for you to receive multiple groups of 5, or expand and get groups of 10.

If the results were put into a dialogue, you could make that show a LOT of info.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)