Jump to content

Using table to save data


TheCapn

Recommended Posts

Hello guys,

I'm working on a gamemode and I'd like to know what would be the best way to save datas ? Because if I use a MySQL request each time I want to save somethings (kills, levels, money ...), I think that the dedicated server won't support that.

What are you advices to make a powerful and optimised saving system ?

Regards,

Link to comment

You can use either SQLite, MySQL or XML as storage. I suggest using MySQL if it's a big system and requires most of the features that come with SQL, and if you want to edit databases via phpMyAdmin.

Almost all game servers support MySQL, you should make sure you have that feature as you never know when you need it around eventually, it's good to have that feature. MySQL queries don't kill the server, you won't have to worry about that. If it wasn't efficient enough then no one would use it in the first place. You can also make a temporary queue for queries and do the query statements over time if you want to, this way you can balance the load. You can use XML to save queries, this way you won't lose the queries if the server crashes. You can also run XML files through a custom made executeable and then use it to finish the queue if the server crashes and you want to manually do the queries.

If you don't necessarily need all the features of MySQL or don't need phpMyAdmin or so, then go for SQLite. You can transfer to MySQL quite easily if you ever needed to.

Edited by Guest
Link to comment

Tables aren't saved permanently. They are unloaded as the resource it is in is stopped. You can use tables for quick temporary saving or simply storing semi-big data for quick access, but you always have to make the table again.

There is a Wiki page regarding XML: https://wiki.multitheftauto.com/wiki/Xml

The functions related to XML are included on the article, you can use those to get started.

Link to comment

Ok, I get it.

However, would you help me resolving one problem ?

I've read this tutorial :https://forum.multitheftauto.com/viewtopic.php?f=148&t=37395

But now, I would like to go further and add a function that would allow the player to block his PM.

I guess I need to use a table, no ? So how could I use it, please ?

Regards,

Link to comment

Well, you can do that in many ways, let's go for tables as you mentioned that.

local ignoredPlayers = { } 
  
function setMessagesEnabled( player, enabled ) 
     if ( isElement( player ) ) and ( type( enabled ) == "boolean" ) then 
          ignoredPlayers[ player ] = enabled 
          return true 
     end 
     return false 
end 
  
addCommandHandler( "togpm" 
     function( player, cmd ) 
          local newState = ignoredPlayers[ player ] and false or true 
          if ( setMessagesEnabled( player, newState ) ) then 
               outputChatBox( "You're now " .. ( newState and "receiving" or "ignoring" ) .. " incoming private messages.", player, 220, 175, 20, false ) 
          end 
     end 
) 
  
addCommandHandler( "pm", 
     function( player, cmd, name, ... ) 
          -- Whatever code you have in there 
          -- Add the following before receiving a message 
          if ( ignoredPlayers[ personToReceive ] ) then 
               outputChatBox( "That player doesn't like you right now.", player, 245, 20, 20, false ) 
               return 
          end 
     end 
) 

That PM system tutorial works, yes, but it's not advanced enough to recognize partial names. It should do the trick however. You can find the function for partial names in the "Useful Functions" page of the Wiki site.

Link to comment
  • Moderators
Hey, thanks a lot !

What about using getElementData and setElementData ? I've juste found this function and they seems just appropriate to my use.

AFAIK it has been designed to share efficient data/score between clients.

When you use it for all things it will lagg the server and I know you are going to use it for everything, it is simply a starter habbit.

I will recommend you to stay with those tables, because they are the most powerful/fasted/optimised scripting way. Simply because they are part of lua, when other things are just extended functions from mta.

Next to saving in tables, your code can be shorter(writing less code) for the same result.

Link to comment
Hello guys,

I'm working on a gamemode and I'd like to know what would be the best way to save datas ? Because if I use a MySQL request each time I want to save somethings (kills, levels, money ...), I think that the dedicated server won't support that.

What are you advices to make a powerful and optimised saving system ?

Regards,

'fileWrite' + 'toJSON'.

Link to comment

Hey, i highly suggest using SQLite instead of a xml file, if you wanna know more information on how here, its from a post a few minutes ago for the same kind of thing

I reccommend you use sqlite. I will show you some basic ways to use it to your advantage. The first thing your gonna need to do is connect to the database, and create the table if it doesnt exist for what you want to start, and include the variables in there, like so
addEventHandler ( "onResourceStart", getRootElement(), function(res) 
    db = dbConnect("sqlite", "vehicles.db") 
    dbExec(db, "CREATE TABLE IF NOT EXISTS vehicle(owner TEXT, x INT, y INT, z INT, xr INT, yr INT, zr INT)") 
end) 

Basically what we did above is create a database and then chec kif the table exists, and if not create it with those certain collums. Now lets say you want to insert a new line of data when someone buys car ( i wont say how to do this, im just showing you how to save. ). You would do that with a dbquery, heres an example.

  
 dbExec(db, "INSERT INTO `vehicles`(`owner`, 'x','y','z','xr','yr','zr') VALUES(?,?,?,?,?,?,?)",  playerAccountNameHere, carx, cary, carz, carxrot, caryrot, carzrot)  
  

This is pretty self explanitory ^

Now i will show you how to update and remove.

Update

  
 dbExec(db, "UPDATE `vehicles` SET x =   ? WHERE owner=?", ownerDataHere) 
  
  

DELETe

  
dbExec(db, "DELETE FROM 'vehicles' WHERE owner= ?", carOwner ) 
  

These are the bare basics but this should be enough to get you started. Just google "Multi theft auto dbexec" and such to learn it better.

Just thought i'd share, i found it much easier to learn than the XML stuff.

Link to comment
Hey, thanks a lot !

What about using getElementData and setElementData ? I've juste found this function and they seems just appropriate to my use.

AFAIK it has been designed to share efficient data/score between clients.

When you use it for all things it will lagg the server and I know you are going to use it for everything, it is simply a starter habbit.

I will recommend you to stay with those tables, because they are the most powerful/fasted/optimised scripting way. Simply because they are part of lua, when other things are just extended functions from mta.

Next to saving in tables, your code can be shorter(writing less code) for the same result.

It does not lag anyone to use element data, the only problem with useless data is that it is a waste of bandwidth as explained in the Wiki article, other than that, if you want to share important data of a player between the server and a client, then you can use element data, there is nothing wrong in using it. There are tens of ways, if not hundreds of ways to store and share data between the server and a client, and element data is just one of it, just like tables are.

Obviously tables can be better than element data as table is a native feature and element data is just another MTA feature, do not get me wrong, but tables are not necessarily better than element data. If you are not familiar with the algorithms that you should use when handling tables, then you should read in-depth information of tables and sorting. There are techniques that will sort a table "faster", as in more results quicker, and techniques that are way "slower", as in less results the same way as another algorithm. One good example is the Big O notation, which is fairly familiar to programmers and mathematicians, the whole data sorting category in general, and yes, this includes your daily table stuff in Lua. I found an about page about it on Reed Copsey's site, which explains the big O notation quite well. Quicksorting is a related subject and you should look into that as well.

So if you do not use tables properly, you will be stuck with it your whole life and never know what you could have done way more efficiently. Element data does not have that issue as far as I am concerned and is a pure way of sharing simple data across.

Time_Complexity.png

Image source: http://www.daveperrett.com/articles/201 ... -notation/

Important information that you might find useful transferring over the server and a client are usernames and player IDs and such, these are something you can store in many ways. At the end of the day, no one really sees a big difference in the game. They all have a main objective, which is to store data and share or update it if it is called by something.

If you want to use tables, go ahead, if you want to use element data, go ahead, if you want to use JSON, go ahead, if you want to use XML, go ahead, if you want to use YML, go ahead, if you want to use SQL, go ahead, if you want to use a flat file database, go ahead, does not matter what you use, the same result will still appear.

Here is the element data version of the "ignore messages" code;

function setMessagesEnabled( player, enabled ) 
    if ( isElement( player ) ) and ( type( enabled ) == "boolean" ) then 
        if ( getElementData( player, "pm:ignore" ) ) and ( not enabled ) then 
            removeElementData( player, "pm:ignore" ) 
        else 
            setElementData( player, "pm:ignore", enabled, false ) 
        end 
        return true 
    end 
    return false 
end 
  
addCommandHandler( "togpm" 
    function( player, cmd ) 
        local newState = getElementData( player, "pm:ignore" ) and false or true 
        if ( setMessagesEnabled( player, newState ) ) then 
            outputChatBox( "You're now " .. ( newState and "receiving" or "ignoring" ) .. " incoming private messages.", player, 220, 175, 20, false ) 
        end 
    end 
) 
  
addCommandHandler( "pm", 
    function( player, cmd, name, ... ) 
        -- Whatever code you have in there 
        -- Add the following before receiving a message 
        if ( getElementData( player, "pm:ignore" ) ) then 
            outputChatBox( "That player doesn't like you right now.", player, 245, 20, 20, false ) 
            return 
        end 
    end 
) 

Edited by Guest
Link to comment
  • Moderators

@ myonlake

Of course it may takes longer when there are more items in it.

I have tested the differences a year ago and still it elementdata takes 1000 t/m 10000 times longer.

Also I didn't said that elementdata was wrong, I only said: "When you use it for all things", it will lagg the connection.

If you are familiar with the server mini-mission server, it's syncs are partly destroyed by it. So it isn't true it can't lagg.

Link to comment
CrystalMV said:
@myonlake

Not sure what "sorting" you are talking about. Apart from element data not having "sorting" functionality at all, "sorting" here does not make any sense in the first place...

I went a little off from the subject as IIYAMA stated that tables are the best thing you can have, so I just wanted to correct him there that it's the best thing you can have when tables are used properly. Of course, in this situation it's not related since we're just getting data and not sorting it, that's why I also mentioned that using element data is okay and it's not wrong to use them.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...