Need help converting Java to Blueprint

The hardest part about learning blueprint is that the vast majority of source and reference material is in Java or similar. I am not a programmer…

Can anyone help finish converting the below Java to BP? I think I understand most of it, but some parts I don’t yet know how to work out in BP. Any help would be appreciated! :slight_smile:

Below is my current progress. The problems I think I am left with are;
What is “nearest” and how do I handle it?
How do I “set the field”?
How do I “print the field”?

So the first part I think matches the Java pretty well, minus the fact that I don’t know what to do with “char nearest”.

The second part seems pretty good minus the “nearest” and field setting/printing sections that I am not sure how to handle. Also, this is only showing 1 biome, so the rest would slide alongside the desert biome as needed.

Here is the source Java:

    // a simple 'struct' to hold some vars
    class BiomeInfo {
        char type;
        int x;
        int y;

//next we set up some points. You can add random to this task and also add any type multiple times
     char[][] field = new field[32][32];
        biomeInfo[0].type = '#';
        biomeInfo[0].x = 5;
        biomeInfo[0].y = 5;

    //next: walk over the field
        for (int i = 0; i < field.length; i++) {
                for (int j = 0; j < field[0].length; j++) {
                    char nearest = '.'; // value here doesn't matter
                    int dist = Integer.MAX_VALUE; // select a big number
                    // walk over each biomeInfo
                    for (int z = 0; z < biomeInfo.length; z++) {
                        // calculate the difference in x and y direction
                        int xdiff = biomeInfo[z].x - i;
                        int ydiff = biomeInfo[z].y - j;
                        // calculate euclidean distance, sqrt is not needed
                        // because we only compare and do not need the real value
                        int cdist = xdiff*xdiff + ydiff*ydiff;
                        // is the current distance smaller than the old distance?
                        // if yes, take this biome
                        if (cdist < dist) {
                            nearest = biomeInfo[z].type;
                            dist = cdist;
                    // set the field to the nearest biome       
                    field[i][j] = nearest;
                    // you can mark the biome point with this code
                    // if (dist == 0) {
                    //    field[i][j] = 'X';
                    // }
    // print field:
        for (int i = 0; i < field.length; i++) {
            for (int j = 0; j < field[0].length; j++) {
                System.out.printf("%c ", field[i][j]);
            System.out.printf("%n"); // %n means newline

An example print of what I am trying to achieve:

Blueprint screenshot is too small, barely can see any text. Regarding the code, it does not look like C++ at all. Googling System.out.printf says it’s Java.

Doh! Updated the post, thanks!

Sorry about the picture. Not sure why it is so small.

But all I really need is a general layout based on that code snippet. Starting at the third ForLoop is where I start to lose grasp.

I think I need a ForLoop for each biome type but I don’t have a clue how to lay the rest out.

That’s how i was able to reproduce it. Given that i you didn’t provide any information about types used there. I’ve declared biomeInfo as vector array and used vector.z instead of biomeInfo.type, and didn’t do anything with field[i][j] = nearest.

Thanks. Seeing the breakdown in the lower right helps me see how this is supposed to work out. I think maybe I didn’t provide enough information though, so I am sorry for that.

This is supposed to be generating a 2D map which uses the vector for each biome variable, counts the distance, and then fills the space inbetween the variables at the end. The “Types” will be my instanced static mesh actor, say Desert, Grassland, Jungle, Ocean, etc.

For simplicity, lets say we just use 4 terrain types. I have a preset vector for each of those terrain types, and the system uses that to determine those vector’s distances from a reference which is then used to fill the map with those types based on their proximity to each other.

Feels like I am pretty close now though, so thanks for your help and any additional help you can provide! :slight_smile:

Updated the original question to reflect the progress I have made with your help, thanks.

“nearest” is just biomeInfo you choose to put in the array after biome-loop. BiomeInfo::type probably will represent a mesh for you, not sure what you’re trying to achieve, if so, nearest should be mesh/actor too.

Regarding setting and printing the field, totally not sure as there is two-dimensional array used in the code, not sure if blueprints support that. In this case you would be able to print field in the log but not on the screen.

I guess it would be better to “rethink” algorithm taking to account you’re using blueprints.

So “nearest” is would just be an actor variable set with one of my meshes? That makes sense.

So the third ForLoop assigns “Nearest” a mesh actor based on it’s location, and the last bit of the second ForLoop sets the current i & j indexes to be that mesh actor.

In this case I should be able to ignore the print field since I can set and print in the same action right?

I think 2D arrays are possible from prior discussions with the devs, but I don’t think it is necessary in this instance. My 1D array has vectors loaded which are each split into X & Y(i & j).

So, when it says “field[i][j] = nearest”, I can just use the indexes from the loops, convert to vector, and then place a static mesh there using my “nearest” variable.

I need to do some work on the conversions between indexes and vectors, but hopefully I can try this out soon, thanks! :slight_smile:

Yeah, exactly like this :slight_smile: Actually it’s looks a bit complicated for me to implement this algorithm in blueprints as it requires some custom data structures.

I guess field in this case should be array of actors, then when you’re going to “spawn field” just iterate over them and get their locations.

It liiiiiivvvvvveeeeeeesssssssss!!! lol

I think I have some problem with my handling of the Y axis since the terrain seems to like generating between the poles, but other than that it seems to work as planned! :slight_smile:


Glad it works, looks nice :slight_smile:
Keep up the good work :slight_smile:

Oh comment unmarked question as solved…