Multibrain Data Structures by Mescad

Problems Koding something? Get help from other users here!
User avatar
Level 3
Level 3
Posts: 178

Multibrain Data Structures by Mescad

Post#1 » Fri Aug 19, 2016 7:07 am

moved over from forums

It sounds like you'd enjoy brain arrays! An object can hold 100 different brains at once, so in the simplest form, we can use each brain as a node and store 100 different elements per object. With this design, for an array of 1000 elements, we only need 10 objects.

But first, let's step back and look at the simple example. Let's create an array of 25 random numbers and display them on the screen. So you can probably imagine a way to do this using 25 different objects. You number them 1-25 and store one number on each object. This is the basic design we are using, but instead of 25 different objects, we will use 25 different brains on ONE object.

Step 1 - Create an object that will use the array.
This could be your default character, a logic cube, or anything really. This is where we'll put the Kode for reading from the array and writing to the array. For example, I have a world called "Scrolling Inventory" where I put this Kode into a table. When the player interacts with the table, the table creates an array and copies the player's inventory into it. For this example, I'm creating mine in an empty world as a logic cube, calling this the "Main" object.

Step 2 - Create an object that will contain the array.
For simple arrays of a few dozen elements or fewer, you can actually just host the array directly on the object you added in Step 1 (I did this in "Scrolling Inventory"). But in general, I prefer to create a logic cube to hold the array. For larger arrays, you will require a new object for every 100 elements, but in our simple 25 element example a single logic cube will do. I've named mine "storage cube" and will refer to it below by that name. You don't need any Kode in this object's brain.

Step 3 - Create a "blank" brain
For every new element we add to the array, we will need an additional brain. This brain doesn't need any Kode in it, because we are just using the variables it stores. However, as an optimizing step, when Project Spark detects that a brain has no Kode in it, that brain and the variables it contains get automatically deleted. Because of this, we need a placeholder Kode tile to indicate that we don't want this brain to be erased.

Create a new logic cube and add this Kode to it:

WHEN [once] DO [ ]

Now you can either save this brain to the brain gallery (push the hamburger button, then choose Save Brain), or if you prefer you can just leave this in the world, as I did in "Scrolling Inventory". I'm calling mine "blank_brain" in the examples below.

Step 4 - Create the array
Now open the brain of your Main object (created in step 1) and add the following Kode. Note that we are indenting under the [once] tile, since you only need to create the array one time.

WHEN [once] DO [ ]
WHEN [for each of][25] DO [storage cube][add brain][blank_brain][to channel]["array"][to slot][current index]

Let's step through this second line of Kode.

First the WHEN side says [for each of][25]. The number 25 comes from the fact we are creating an array of 25 elements. This number can be anything from 1 to 100. If you want an array with more than 100 elements, you'll need to break it into chunks of 100.

Next we have the [storage cube] object from Step 2, added to this line with the in-world picker. If you have tiny arrays, like in my "Scrolling Inventory" example, you can leave this tile out and your array will be stored on your Main object. Later when we have huge arrays, we'll use an object variable here to select different storage locations. For now, we're using the storage cube.

Next is the [add brain] tile. This puts an additional brain onto the storage cube object. This new brain will run at the same time as the brains already on the object. Each brain can hold its own unique copy of a variable, and this is the key to our array structure.

The next tile is the blank_brain that we created in Step 3. You can either choose your blank_brain object with the in-world picker, or if you saved the brain to the gallery, you can choose it with the gallery picker.

Next we have the tile [to channel] followed by some text "array". This is optional, but good practice to include. Each brain is located in a channel in a particular slot. This is where you tell the brain which channel to live in. I'm calling my channel "array" in this example.

After the channel, we have the tile [to slot], and this is NOT optional. Each slot can only hold one brain, so the slot field acts as the array's index number. The slot number I'm choosing is the [current index] of the array. This means the first brain is added to slot 1, the second brain is added to slot 2, and so on up to slot 25. Programmers who prefer arrays to start with 0 instead of 1 should use [current index][minus][1] here.

So that's the array created. How do we add stuff to this array?

Step 5 - Use the array.

To write something to the array or read something from the array, we need to first tell Project Spark which element we want. Remember that the slot numbers are used as the array index. There are other ways to do this, but the easiest to understand is by using a brain variable. The basic format looks like this:

WHEN [once] DO [ ]
WHEN [ ] DO [my brain variable][equals][storage cube][get brain][in channel]["array"][4]
WHEN [ ] DO [my brain variable][some variable][equals]["some data"]
WHEN [ ] DO [display][my brain variable][some variable][screen center]

In Line 2 we are finding the desired element in the array, in this case, the 4th one, and are pointing a brain variable at that location. (Can't find them? Brain variables are on page 2 of the Values section of the Kode wheel). The channel part is probably obvious, but the "4" here is the slot number. There's not a [in slot] tile like there is for channels, so just know that you stick the slot number at end of the line. So now we have a brain variable pointed at the array location. Time to store something there.

Line 3 is very generic looking, because really you can store ANY type of variable here. We're using the brain variable defined in Line 2 as an address for the array.

Line 4 shows how a brain variable can be used to pull things out of the array. Here we're displaying the data, but you can do anything with those variables you can do with any normal variable of the same type.

So back to our example with 25 random numbers. In the Main object, add this Kode:

WHEN [LT][pressed] DO []
WHEN [for each of][25] DO [my_brain_variable][equals][storage cube][get brain][in channel]["array"][current index]
WHEN [ ] DO [my_brain_variable][my_data][equals][random number][1][to][100][as integer]
WHEN [RT] DO [ ]
WHEN [for each of][25] DO [my_brain_variable][equals][storage cube][get brain][in channel]["array"][current index]
WHEN [] DO [display][my_brain_variable][my_data][screen top left][small font]

In Line 1 we are waiting for LT to be pressed. This triggers a new set of 25 random numbers.
Line 2 loops through our array, retrieving each element. Line 3 stores a new random number into each element selected in Line 2.

In Line 4 we are waiting for RT to be held down. This triggers the displaying of our 25 numbers.
Line 5 looks just like Line 2, retrieving each element of the array. But this time instead of changing those values, we follow it with Line 6, displaying the values on the screen.

The Next Step...
So there's very basic array. With this setup, for an array of size N you need floor(N / 100) + 1 objects, plus the blank_brain template and your Main object. Check out my shared level "Large Array (no gameplay)" for a system that stores 2500 elements in 25 objects.
End of line.

Return to “Koding Konversations”

Who is online

Users browsing this forum: No registered users and 1 guest