I am in the process of writing some code to be used in a puzzle game. Basically, when the pieces are shuffled to the correct position, the play wins. What is the best way to go about this logically? I was thinking storing either the GameObjects in an array, or their position in an array, and using it to reference current positions and if those positions match the victory conditions. Any thoughts on this? I am just looking for some advice on the logic to go about this. It will be on Android devices, so performance is an issue. Thanks for any help and suggestions!
If you’re using C#, might I suggest storing the game objects in a List rather then an array?
A list is useful because it preserves the entire game object and can be called much like an array, but does not require an array size to be specified… Basically, a List is equal to a multi dimensional array (if you compare it with PHP that is.)
Otherwise, if you’re using JS, you should be able to use the below code;
GameObject : goArray[ ] = new array();
function addNewToArray(GameObject itemInsert){
goArray[goArray.length] = itemInsert; //Array index starts at 0, but the length will always be array index +1 (unless nothing is in the array, in which, it equals 0, and will ultimately write the first itemInsert as index 0)
}
This code is untested, but it should work, unless Unity has an issue with storing GameObjects in a Javascript array. In which, it may be useful using C# and storing in a list.
Personally, I would recommend storing plain strings / ints / floats in the array, as this will write less junk to the memory during runtime, ultimately speeding up your gameplay and making the code more efficient.
You don’t need to use C# in order to use a List.
Hey thanks for the reply sarmth! Ok so if i understand you correctly i would:
reference each puzzle piece using an int or string
on touch, i raycast from touch position and whichever piece i hit, i pick up and move, i would then have to write a function to reference the string/int in the array
on ended touch event, send out a raycast from touched puzzle piece position, and whichever piece is hit, reference that to its respected string/int in the array
switch piece positions (either using variables that capture the position of each piece position on raycast hit/or if it is possible use the array to do that somehow)
switch the pieces index position in the array
am i on the right track with this?
again thanks for any and all help and suggestions!
Why would you store a List of strings or ints instead of just storing the List of GameObjects?
If that would be less of a hit on performance (which it seems like it would) and i would be able to use it to set up a victory condition (all pieces in the right order/position) then i would absolutely use that
That’s correct. Otherwise, you can use the index within the array as the unique identifier of each piece, and store the coords within the array, and then change the coords accordingly during an event.
excellent! Thanks a bunch for the direction and advice. If i had any other questions or run into any problems with this, do you mind if i post back to this thread for some help? I’m going to set up a list of gameobjects and see if i can just shuffle the list according to positions on the screen, and when the list is in order, the player wins.
I don’t see a problem with it. Most people here will be able to help you out, they’re fantastic ![]()
Thats great! Having a bit of trouble with importing System.Collections.Generic.
I have this
#pragma strict
import System.Collections.Generic;
function Start ()
{
var tiles : List.<GameObject>;
tiles = new List.<GameObject>();
}
and i get “List is not a generic definition error”
any suggestions?
thanks for the help!
My guess is the code should look like that:
using UnityEngine;
using System.Collections.Generic;
public class NewBehaviourScript : MonoBehaviour {
private List<GameObject> tiles;
// Use this for initialization
void Start () {
tiles = new List<GameObject>();
}
// Update is called once per frame
void Update () {
}
}
in order to output no errors.
That’s because in the code you have written, it’s UnityScript, not C#.
In UnityScript, just create a normal array.
Basically, if it’s UnityScript, you don’t need to import anything. If it’s C# that’s where you do your imports.
Ah ok that makes sense. So Lists cant be used in UnityScript then? An array of 16 elements (either a single dimensional one just to keep track of what is one what tile, or a 4x4 to map out the grid, although not sure how to really make that work to store and swap positions) would not have much of an effect on performance for mobile phones?
Thanks Tomasz Kielski! Guess i should have mentioned i was using JS
Looks like I have misinterpreted. In JavaScript or UnityScript it should look like that
#pragma strict
import System.Collections.Generic;
var tiles : List.<GameObject>;
function Start ()
{
tiles = new List.<GameObject>();
}
You could even use;
var tiles : GameObject[];
![]()
Which is what i will most likely end up doing. I was looking through some of the different types of arrays. As long as i am able to move around their location in the array, and the array has a way to either sort (which a couple do) or code a victory condition dependent on gameobject’s locations in the array, i will just use them. The arrays will be small enough that a phone will be able to handle it. Thanks for all the help guys!