Random Messages
#1

I have decided to create a random message system, But this isn't one of these regular random message where in they are placed in an array. Instead, they will be created dynamically, Loaded/Saved/Inserted in the database. So far I am not having any problems on loading, saving or inserting the data on the database; However I am having a hard time on coding the part where the HintMessage() generates the next random message. Is it possible to randomized the next message just like in the regular random message system? Like for example; I have 3 random messages in the database;

1. This is my first test.
2. This is the second one.
3. This is the last one.

HintMessage() will then randomized these (just like random()), The current code I had returns null (I know, I have coded it wrong so my bad) - Help will be appreciated on this one ~ And sorry about my grammar, Just like always xD

PHP код:
public HintMessage()
{
    new 
id random(sizeof(randomMessages));
    
SendFormatMessage(INVALID_PLAYER_IDCOLOR_RED"* "cyan"%s"randomMessages[id][randomMessage]);
    return 
1;
}
stock LoadRandomMessage(id// This one is looped through i < MAX_RANDOM_MESSAGES
{
    new 
query[128], field[130];
    new 
DBResultresultdb;
    
format(querysizeof(query), "SELECT * FROM `randommsg` WHERE `id` = %d"id);
    
resultdb db_query(databasequery);
    if(
db_num_rows(resultdb))
    {
        
db_get_field_assoc(resultdb"message"fieldsizeof(field)); format(randomMessages[id][randomMessage], 128field);
        
randomMessages[id][randomMessageExist] = true;
    }
    
db_free_result(resultdb);
    return 
1;

Reply
#2

In my opinion having both in database and in arrays stored destroys the whole point of it. I'd rather have it in the database and when it is about sending a random message:
pawn Код:
"SELECT * FROM randommsg ORDER BY RANDOM() LIMIT 1"
A random row will be selected and then you can retrieve the "message" and directly use it to send the client message to all.

You can still be able to select all the messages, show them to a dialog and edit the message of the said item.
Reply
#3

What's the point of having a dynamic message system if you have to reload stuff?
Just select a random row and send the message there.

pawn Код:
public HintMessage()
{
    new
        DBResult: result = db_query(database, "SELECT * FROM `randommsg` ORDER BY RANDOM() LIMIT 1;");

    if (db_num_rows(result))
    {
        new string[145];
        db_get_field_assoc(result, "message", string, sizeof (string));
        format(string, sizeof (string), "* "cyan"%s", string);
        SendClientMessageToAll(COLOR_RED, string);
    }
    else print("ERROR: Could not find any rows [HintMessage()]");
    return 1;
}
EDIT: Konstantinos, forgot to reload again, haha.
Reply
#4

I have made my own function, executing a query using the syntax you have mentioned above;
Is this correct ??

PHP код:
stock InitializeRandomMessage()
{
    new 
DBResult:resultfield[130];
    
result db_query(result"SELECT * FROM `randommsg` ORDER BY RANDOM() LIMIT 1");
    
    if(
db_num_rows(result))
    {
        
db_get_field_assoc(result"message"fieldsizeof(field));
    }
    
db_free_result(result);
    return 
field;

Edit; Guess am right HAHAHAHA
Edit2; Thank you so much both of ya much luv from yours truly Jake, the code works properly.
Reply
#5

I myself prefer the code I posted, because it wastes less memory.

You're creating a string with 130 cells and then you're creating another one with SendFormatMessage.
What I'm doing is re-using the string I used to store the message.

It barely makes any difference, but I like optimizing it as much as I can.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)