Jump to content

Converting table-stiring and backwards


aka Blue

Recommended Posts

So I have a Lua script which works in both sides (client-server). I use the server-side to store positions in a database. The problem I have is that it does not convert the string into a table in client-side so I can't use the positions I have stored.

Exemple:

CLIENT-SIDE

local sx, sy = guiGetScreenSize()
local x, y = ( sx / 1024 ), ( sy / 768 )

local table = {

	{x*200, y*2,x*50,y*200},
	{x*200, y*2,x*50,y*200},
	{x*200, y*2,x*50,y*200},
	{x*200, y*2,x*50,y*200},

}

triggerServerEvent( "storeData", localPlayer, toJSON( table ) )

SERVER-SIDE

local data = fromJSON( storedData )
triggerClientEvent( x, "sadas", x, data ) -- It returns nil

Is there any way to transform a table into a string and backwards?

Thanks.

Link to comment

I use toJSON to encode the table I got client-side. The values are dx positions, like sx*222, sy*22, etc. When I want to load the data again with fromJSON I get nil value. I don't know why. I use JSON for a couple of things and I never get this error.

Link to comment
  • Moderators
2 hours ago, aka Blue said:

I use toJSON to encode the table I got client-side. The values are dx positions, like sx*222, sy*22, etc. When I want to load the data again with fromJSON I get nil value. I don't know why. I use JSON for a couple of things and I never get this error.

 

A Is the whole table a nil value

B or is it a value after indexing the table?

 

In case of A, it is very much possible to get those issues. JavaScript (Object Notation) does not support the data type tables after all.

Use iprint to figure out which type of JavaScript objects MTA has assigned for your table format.

[ "test"] = array

{0:"test" } = object

 

In case of B.

  1. First of all, it is better to send over tables instead of JSON. This makes the packet size much smaller and it might also solve your problem for some unknown reason. (Strings much bigger data type than numbers.)
     
  2. Did you set up your database correct that the column supports enough characters? (this can break JSON of course)
     
  3. Debug JSON, show me your JSON!

 

 

 

 

 

 

 

 

 

 

  • Like 1
Link to comment

@IIYAMA I trigger the client-side table formatted with JSON:

PSkEc9z.png

The table has this default values

SyfHKln.png

When I change something, I charge the data from the database (I know it's not the best option but it's for testing)

36bU4L3.png

If I don't use fromJSON, I get this in iprint

pczrAg9.png

But if I use fromJSON, I get this

vqO5jIx.png

I don't know where is the problem. I need to trigger the positions to client-side and change the value of posHud table with the database values.

If I trigger the result without using fromJSON, I get this

fWFewLR.png

I think its not formatting good or something.

Thanks for answering!

  • Like 1
Link to comment
  • Moderators
3 minutes ago, aka Blue said:

If I don't use fromJSON, I get this in iprint

pczrAg9.png

Can you show me the complete JSON string? Because this one looks incomplete.

 

This looks fine:

[ <-- MTA wrapper

[ [ <-- first array (table)

[ [ [ <-- sub array (table)

 

This looks like it is cut off, after getting it from the database (on the end of the string)

], ["

 

Not sure if there is a character limit for the console.

 

 

A quick validation:

debugJSON_A = stringA -- JSON from clientside


-- get the string from the database:
debugJSON_B = stringB


if debugJSON_A != debugJSON_B then
  outputChatBox("database column `hud` is not correct set-up")
end

 

 

 

 

 

  • Thanks 1
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...