Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Userdata
Started by AkioSarkiz Jan 04 2019 04:35 PM

6 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

AkioSarkiz

[GLOBAL: userInfoPane.html]
AkioSarkiz
  • Observer

  • 26 posts
  • Corona SDK

I make a request to the server. I get a json table. I want to assign a variable, but nothing comes out. Writes that I assign user data. Why?

 

    _tbl = network.request("http://example.com/index.php", "GET", function(event)


      local table;


      if ( event.isError ) then
            print( "Network error: ", event.response);
      else


        local json = require("json");
        table = json.decode(event.response);


        for i = 1, 10, 1 do
          print(table[i][1], table[i][2]);
        end
        --[[
        output:
                  1 1
                  2 2
                  3 3
                  4 4
                  5 5
                  6 6
                  7 7
                  8 8
                  9 9
                  0 0
        ]]
      end


      return table;
    end);


    print(_tbl);  --[[ output:
                                userdata: 073F2658
                    ]]

 



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,463 posts
  • Corona SDK

You can't use print() on a table like that. 
 
You need to iterate the contents and print any content that is printable.
 
SSK includes two features for dumping the contents of a table.  These features are borrowed from (and modified) external sources:
- dump() Prints first-level of table contents.
- print_r() Iterates entire table and prints all children (won't work on self-referencing/closed-loop tables). 
 
https://roaminggamer.github.io/RGDocs/pages/SSK2/extensions/#table-debug-features
 
Example 1

local t = {}
t.age = 10
t.name = "bob"
t.pocket = {}
t.pocket.coins = 10
t.pocket.wallet = true

-- Dump t
-- Prints age, name, and pocket, but does not iterate into pocket.
-- i.e. It just says pocket exists and is a table.
table.dump( t )

output:

Table Dump:
-----
age (string)                   == 10 (number)                   
name (string)                  == bob (string)                  
pocket (string)                == table: 0AE99808 (table)       
-----

 
 
Example 2

local t = {}
t.age = 10
t.name = "bob"
t.pocket = {}
t.pocket.coins = 10
t.pocket.wallet = true

-- Recursively dump t
-- Prints age, name, and pocket children too.

table.print_r( t )

output:

table: 0AE99600 {
   [pocket] => table: 0AE99600
   {
      [wallet] => true
      [coins] => 10
   }
   [name] => "bob"
   [age] => 10
}



[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,463 posts
  • Corona SDK

Here is the code for my table extensions:

https://raw.githubusercontent.com/roaminggamer/SSK2/master/ssk2/core/extensions/table.lua

 

 

Be warned, it uses some string extensions for dump/print_r so you'll need the string extension code too.



[TOPIC: post.html]
#4

AkioSarkiz

[GLOBAL: userInfoPane.html]
AkioSarkiz
  • Observer

  • 26 posts
  • Corona SDK

I do not understand anything. Why I return the table to the function, and the user data is returned? It should not be. When I return a number, the number should return. When I return a table, the table should return. How can user data be returned? It is not right. I did not understand your examples. My function is not involved there. network.request()



[TOPIC: post.html]
#5

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,463 posts
  • Corona SDK

you wrote that print statement right?

 

That won't work.

 

 

Sorry you don't understand, but there isn't much I can to to be more clear.

 

PS - Michael points this out below, but you're also not using network.request() correctly.

 

It does not block and then return a value you can use.  It is asynchronous and you need a listener to handle the returned data.



[TOPIC: post.html]
#6

Michael Flad

[GLOBAL: userInfoPane.html]
Michael Flad
  • Contributor

  • 206 posts
  • Corona SDK

Network.request does not return the table from within your listener, it returns a handle you may use to cancel the request. It seems to be implemented on the C++ side of Corona and so it returns a userdata.

 

See the documentation at https://docs.coronalabs.com/api/library/network/request.html#listener-required

 

And the examples at the bottom of the page.



[TOPIC: post.html]
#7

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,147 posts
  • Corona SDK

userdata is basically allocated memory from the C/C++ side of the C-Lua bridge. In practical terms, it is most likely a C struct. C structs, in C are a lot like tables in that you access structure members with a structname.membername like syntax:

struct User {
     char name[32];
     int age;
     double salary;
}; 

User user;
strcpy( user.name, "Fred" );
user.age = 57;
user.salary = 15.00;

When that comes to Lua, it's not a table but user data, so you can't just dump it. You have to know what the values are. Lets say you have event.user comes back from a C based API, user is user data, but it's also a known struct. You can't see it as a table, but you should be able to do:

print( event.user.name )
print( event.user.age )
print( event.user.salary )

You just can't iterate over event.user like a table since it's not a table.

And as others have posted out, that return value will not have the results of the request. You can only get that in the listener in the event.response member.

 

Rob




[topic_controls]
[/topic_controls]