+10

How to use Arrays in Bolt with a simple script.

Reaper 3 years ago updated by Shaun White 3 years ago 3

And for those of you asking whats an Array, why would I use them, an array is a single variable with multiple storage locations, think of it like a graph with X and Y dimension.

ie a 10x 10 graph has 100 locations , and a 10x10x5 has 500 locations, very useful for storing

simple map data ie what tile is at what location and maybe terrain types / what is on floor etc.



First youll need to make a c# script for any Array you want to make, Make sure you rename 

both it and the classname accordingly.

ArrayData.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Bolt;
using Ludiq; public class ArrayData : MonoBehaviour {
// Define your array here ie
// public int[,] MyArray = new int[10,10];
// No need for it to have 3 dimensions but if it is only
// 2 dimensionns then remove the int ArrayZ from the functions. public int[,,] Map = new int[20,20,4];
public GameObject GameManager; public int GetArrayData(int ArrayX, int ArrayY,int ArrayZ) //,out int Value2,out int Value3 etc
{
return Map [ArrayX, ArrayY, ArrayZ];
} public int SetArrayData(int ArrayX, int ArrayY,int ArrayZ, int Value, out int Current) //,out int Value2,out int Value3 etc
{
// Current outputs the variable value before it is altered ie the same value that youd get from getarraydata before setting it.
Current = Map [ArrayX, ArrayY, ArrayZ];
Map [ArrayX, ArrayY, ArrayZ] = Value;
return Value;
}
}


Now click tools>Bolt>Unit Options Wizard, then click next and at the bottom of types

click the + and do a fuzzy search for what youve called your script (in my case ArrayData)

 and generate. You may or may not have to click tools>bolt> build etc before it appears in the fuzzy finder.


Now attach the script to the gameobject you wish to be handling the array (in my case the gamemanager / mapeditor) Then drag and drop the object its attached to into the Gameobject variable on the script.



The Array obviously needs to be declared within the c# script but it can be easily manipulated and accessed from within bolt. ie these routines Ive used 1st to make outer edges of a map then to fill the map with pills, and finaly to randomly generate a maze (95% done).


To use within bolt just do a fuzzy search on GetArrayData / SetArrayData and plug things in.

Just make sure you dont go out of the range of the array size you declared as there are no checks.


Below is just example code of how I used it.


Here the graph steps through 2 loops to set all the locations within the array to 21 (the value

that will represent a pill to collect in a pacman game.) The random bnit is just if a flag is set in the editor and can be ignored.



The next part then sets the left and right and top and bottom rows to be all 1s, the number that

will represent a wall on my map.



So that bit basicly makes a 20x20 map with an outer wall and the rest is filled with pills.


This last section is the routine I use to draw the map up after its finished generating.



It either instansiates a Pill or a Wall depending on the value in the array.


And below is a zoomed out shot of the tangleed mess that generates the whole map.

(It branches into that after setting the pills/walls in the array that after its generated it goes to the draw routine.




An example of one of random maps generated so far, as said still a Wip but its coming

along :)



Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):

Anyone know how to do the equivalent of [code] here ?

Edit: Thanks Reality - its the format button then select code for anyone else looking for it :)

+2

Select the paragraph symbol (first button) and select "code".

I am code.  Fear me.

This is an excellent tutorial for using your own code components with Bolt in general. Thank you very much for taking the time to write it. It's really opened Bolt up for me!