Any limits on AJAX post-data?

Get help using Construct 2

Post » Sun Jul 06, 2014 6:56 pm

My game is turn-based, so PHP should be adequate for processing the logic of the game. One huge advantage also is that I can auto-run my PHP logic file as a Cron-Job which will be one less thing for me to do. If I made a sort of control panel, similar to how my game runs right now, I have to log in at the right time and set it running.

The advantages of C2 are that I know the language very well and find it easier to manipulate objects. But I'm sure I can get the hang of it in PHP. Luckily, I'm in no rush, so what I think I'll do is, keep plodding away and see where it leads me.

I have worked out that if I fetch my data with FETCH_CLASS, it puts it directly into a class, and then I keep an array of those objects and work my way through those. It's not quite as easy as C2 and reminds me of the 'good old days', looping through arrays etc, but it's fairly simple. I am a bit confused that my player object doesn't have certain values but it just adds them from the database anyway. E.G My players in the database have about 10 ratings, but in my class, I only added about 3 of them for testing. When I fetch the data with FETCH_CLASS, it creates the object from my class, fills the properties I've created and then just creates the others too. Making me think that I could actually just skip the properties and allow it to fill automatically anyway.

Anyway, learning and enjoying myself as I go, it's fun :)
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Sat Jul 12, 2014 8:42 pm

@troublesum ... Sorry to bother you again but you seem knowledgeable on this stuff, so any ideas why this is happening? Pasting this from my other thread...

I know this should be fairly basic so I must be doing something wrong.

I'm sending a json_encoded string from my php file to C2. The string shows [4,3,1,3,1,2,1,1,0,3] when C2 receives it (when set in a text object). It's just 10 numbers in PHP.

I then try to load it into my C2 array with Array -> Load from JSON string (String).

The array is still empty, so I'm doing something wrong. Any ideas?

Can you identify which stage this is going wrong at? Does that string [4,3,1,3,1,2,1,1,0,3] look like a json string to you? That's what it's showing when I echo it from php to a browser, then when I show it in a text object in C2, but when I convert it into my C2 array it's just empty.
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Sat Jul 12, 2014 8:46 pm

farflamex wrote:@troublesum ... Sorry to bother you again but you seem knowledgeable on this stuff, so any ideas why this is happening? Pasting this from my other thread...

I know this should be fairly basic so I must be doing something wrong.

I'm sending a json_encoded string from my php file to C2. The string shows [4,3,1,3,1,2,1,1,0,3] when C2 receives it (when set in a text object). It's just 10 numbers in PHP.

I then try to load it into my C2 array with Array -> Load from JSON string (String).

The array is still empty, so I'm doing something wrong. Any ideas?

Can you identify which stage this is going wrong at? Does that string [4,3,1,3,1,2,1,1,0,3] look like a json string to you? That's what it's showing when I echo it from php to a browser, then when I show it in a text object in C2, but when I convert it into my C2 array it's just empty.


This [4,3,1,3,1,2,1,1,0,3] cannot be loaded inside the array via the load from JSON since it is not correctly formatted, I think using a tokenat and removing the [ and ], you could load the values inside an array.
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Sat Jul 12, 2014 8:49 pm

I posted this on your other thead but wanted to reply to this one as well.

Make sure your encapsulating the php array in the format that the C2 array is expecting.

Code: Select all
 $myArray = array();
    $myArray[] = 4;
    $myArray[] = 3;
    $myArray[] = 1;
    $myArray[] = 3;
    $myArray[] = 1;

    $output = array(
   'c2array' => true,
   'size' => array(
       0 => count($myArray),
       1 => 1,
       2 => 1
   ),
   'data' => array()
    );

    $x = 0;
    foreach ($myArray as $value) {
   $output['data'][$x] = array();
   $output['data'][$x][0] = array();
   $output['data'][$x][0][0] = $value;
   $x++;
    }

    return json_encode($output);


The resulting JSON string would look like this :
{"c2array":true,"size":[5,1,1],"data":[[[4]],[[3]],[[1]],[[3]],[[1]]]}

You can now take this string as is and load to the Array in C2
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Mon Aug 25, 2014 3:30 am

Sorry totally unrelated. I cant send you a private message, so I'll try here. I just loved the graphics on your latest game Jovian War. I was wondering if you could share the source of those graphics? Thank you.
B
8
S
3
Posts: 16
Reputation: 702

Post » Sun Aug 31, 2014 2:54 pm

k, I'll reply on the JW thread.
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Sun Aug 31, 2014 5:45 pm

I have found that there seems to be an upper limit on the size of the json string, I think it was 32K characters, or perhaps larger. I think it's a string constraint, not specifically on ajax. A quick test would tell you. I discovered it as the xml file I was taking in got truncated.
B
7
S
3
G
1
Posts: 184
Reputation: 1,352

Post » Wed Oct 15, 2014 1:51 am

troublesum wrote:I posted this on your other thead but wanted to reply to this one as well.

Make sure your encapsulating the php array in the format that the C2 array is expecting.

Code: Select all
 $myArray = array();
    $myArray[] = 4;
    $myArray[] = 3;
    $myArray[] = 1;
    $myArray[] = 3;
    $myArray[] = 1;

    $output = array(
   'c2array' => true,
   'size' => array(
       0 => count($myArray),
       1 => 1,
       2 => 1
   ),
   'data' => array()
    );

    $x = 0;
    foreach ($myArray as $value) {
   $output['data'][$x] = array();
   $output['data'][$x][0] = array();
   $output['data'][$x][0][0] = $value;
   $x++;
    }

    return json_encode($output);


The resulting JSON string would look like this :
{"c2array":true,"size":[5,1,1],"data":[[[4]],[[3]],[[1]],[[3]],[[1]]]}

You can now take this string as is and load to the Array in C2


How would you format your json_encode($output) if all you want to do is return, say, five name/value pairs?
B
9
S
1
Posts: 33
Reputation: 612

Post » Wed Nov 12, 2014 10:42 pm

From C2 to PHP? If it's 5, I'd put them in an array and send them as above. You could do it as I did it at first, by making a string, separated by a symbol (e.g the pipe '|') and then 'explode' them at the other end.

Have to say, having done quite a bit of PHP and C2 now, I'd go with json_encode all the time if it's already an array at either side. For a small amount of info, I'd probably just send it as a string.
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Previous

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 8 guests