• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Android Return array with asynchronous event from extenstion

salyossy

Member
Hi.
I am using a JAVA file as an extention, starting with line code: import com.yoyogames.runner.RunnerJNILib;

So far used RunnerJNILib.DsMapAddDouble( dsMapIndex, "****", ****); to return single values.

But now I have a need to return an array.

Is is possible? If not, is there a workaround for this with buffers? How?

Thanks!
 

salyossy

Member
Oh no!

That sounds not easy to me. Not familiar with JSON.

I am trying to pass an array of type double.
In order to do that, if I understand correctly, I must convert all the numbers into a string, and pass it. But isn't there a limitation to string size? My array is quite big.

I am googling this of course. But if someone could give me a simple example of code to start with, ON JAVA SIDE, it would be more then nice.

If there is another way... I would love to know.

And thank you very much FrostyCat !
 
Last edited:

chamaeleon

Member
You could have two functions. One to ask the Java side for the length of the array, and one to get the content in a buffer. Call the first function to get the length, allocate a buffer large enough to hold that number of double values in GML, and pass the buffer pointer to Java in the second function and populate it in Java (using whatever methods are appropriate for putting binary data in such a memory structure, I couldn't say what they are specifically). Then simply iterate over the buffer reading double values from it in GML, and finally free the buffer in when done with it. In principle this should work (but I have not personally created Java extensions...)
 

salyossy

Member
Ok. So the method suggested by FrostyCat was successful.
I'm still worried about performance, so I would like to try buffers, as chamaeleon suggested.
Still don't know how to send buffers.... hopefully someone explain that.

If someone would ever need this, here's what I did, and and worked:

All I had to do is add these:
import java.io.*;
import java.util.*;


Then pass the array converted to string:
RunnerJNILib.DsMapAddString( dsMapIndex, "data",Arrays.toString(myArray));

That passed the array's content as a string to the async event. (PERFORMANCE???)

Now, at the async event:
json=ds_map_find_value(async_load, "data");
myArray = json_parse(json);


Thats it. myArray is passed!
 

Binsk

Member
Glad you got this figured out.

Gonna comment on the buffer situation as @chamaeleon hit the nail on the head. Now, I have not done this in Java as it, as far as I understood, does not have proper 'pointer' support. I have done this in C++ so I will explain the concepts as to how I did it here and maybe you can translate it to Java as it has been a long time since I needed to use Java.

You can create a buffer in GameMaker which is effectively allocating one solid strip of memory to storing data. You can get the location this strip starts, in memory, by calling buffer_get_address(). This is a pointer, and also where I am not sure how you would handle this type of data in Java. You can pass this value to your extension by specifying it as a 'string' on GameMaker's side. This works because a string is technically just a pointer to a strip of data where the data is a series of 'char' values. GameMaker can't tell the difference, it just knows you are passing a location in memory.

On the extension side, in C++ I would 'receive' the data as a double pointer (aka., an array of doubles). I could then modify the data inside this array however I wished and it would automatically 'update' on GameMaker's side because it was modifying the exact same location in memory. I couldn't resize the array, as that would create a new strip of data in a new location, but I could modify to my heart's content.

That's how it works, pretty simple. If there is a way to receive this pointer and manually tell Java 'this is an array of doubles' then you should be good. If there is not then I am not sure how you would go about this. As said, it has been probably close to a decade since I touched Java.
 
Top