I’m working on a hobby project with a few friends in the form of a pretty simple Gacha game, but we’ve hit a snag in that most of us are artists and not coders. That said, I threw together a basic outline for the Gacha system in hopes of making sense of the logic for myself before I try to translate it into C#.
The problem I’ve run into now is the sheer number of variables That I’d need to define, one for each card (of which we have 35, so far) and then the ‘affection’ points for each of those cards, the Game Objects of each card image, etc.
Would these variables and objects be better handled by a list, or array? Or would I be best off having a very long script, considering my inexperience with C#?
Arrays, or lists are almost always better for dealing with a situation like that. I don’t know what card affection is, but you can make a class or scriptable object for each card so that it can hold other information rather than just an image, and these would go in the list or array. If it’s a known number that won’t change, generally you use an array. The other nice thing about an array is you can make it public and it will show up in the editor. You can make a list show up in the editor also but it’s a little more difficult. Probably the simplest is make a class for the card, then make a public array of cards so you can add to them in the editor.
Thank you for such a quick response!
The use of arrays makes sense to me in theory, but as soon as I try to put it into practice I hit a wall.
Would typing out the entire thing without arrays cause a hit to performance, or is there not much downside besides my own use of time?
Thank you both for the help so far! I’m sorry to follow up with what are probably simple questions.
After restructuring what little I had to hopefully fit an array, this is what I have.
using System.Collections;
using System.Text;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GachaTestPart2: MonoBehaviour
{
//common cards
public GameObject cardImage;
public bool cardOwned;
public int cardID;
public int cardAffection;
public string cardDescription;
// rare cards
//fill later
// epic cats
//fill later
// legendary cats
//fill later
public void OnButtonPress()
{
var number = Random.Range(1, 4);
Debug.Log(number);
if (number >= 1)
{
var number2 = Random.Range(1, 4);
Debug.Log(number2);
if (number2 == 1)
{
if (cardOwned != true)
{
cardOwned = true;
cardImage.SetActive(true);
SimpleUI.Instance.ScaleTo(cardImage, new Vector3(1, 1, 1), 0.5f);
//add Animation and delay to images
//replace ui text with string text
}
if (cardOwned == true)
{
cardAffection += 50;
}
}
}
// copy for rare cards here
// epic cards here
// legendary cards here
}
}
And I have a separate script for the first array of cards, here, as you showed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CardArrayCommon : MonoBehaviour {
public GameObject[] commonCards;
[System.Serializable]
public class card
{
public GameObject cardObject;
public int affection;
public string cardDescription;
}
}
Should I be declaring the array itself at the beginning of my GachaTest script? And I’m not sure I understand how to access my cards variables within the array, at this point.
You’ve missed the function i was trying to aim you towards a little.
Heres a slightly more expanded script to show what i was getting at for giving you a way to populate the array, and a test button for how you interact with the array
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CardGameManager : MonoBehaviour {
public Card[] cards;
[System.Serializable]
public class Card
{
public string cardName;
public GameObject cardObject;
public int affection;
//...other properties
}
private void OnGUI()
{
if(GUILayout.Button("Draw A Random Card"))
{
int cardIndex = Random.Range(0, cards.Length);
Debug.Log("You have drawn a " + cards[cardIndex].cardName);
Debug.Log("It is a " + cards[cardIndex].cardObject);
//instantiate cardObject
}
}
}
Save this as CardGameManager.cs and attatch it to a GameObject, and you’ll see this in the inspector: