Going NUTS- Tring to return a list from another script
I really need some help here. I have a singleton script with database functions that are made into lists of stats, vitals, skills, etc. I want to display the contents of those lists in another script( in this case it's an HUD that shows your stats and name at the top.)
I've tried everything I can think of to no avail! I've tried calling the list to be made from the HUD script and the database script. --null reference I've tried creating the variables on the database script, even inside the list function --was able to get and return the very first stat and have it display, but the second gives me an index out of range error.
public List<Vitals> Vitals;
public List<Skills> Skills;
public List<Stats> Stats;
void Awake()
{
SQLiteInit();
Vitals=new List<Vitals>();
Skills= new List<Skills>();
Stats=new List<Stats>();
}
static PlayerDatabaseManager instance;
public static PlayerDatabaseManager GetInstance()
{
return instance;
}
void Start()
{
LoadStats();
Constitution=Stats[0].Constitution;
Stamina=Stats[1].Stamina;
}
public int GetCon()
{
return Constitution;
}
public int GetStam()
{
return Stamina;
}
In the HUD script:
void Start ()
{
PlayerDatabaseManager.GetInstance().GetCon();
PlayerDatabaseManager.GetInstance().GetStam();
}
I thought I was on to something there, but alas.
So, what is the best way to either return elements of a list or the entire list to another function that needs to use them? I hope someone has an answer out there and can clearly walk me through. I've watched countless tutorials and cannot find one that addresses this situation. Thanks!
Answer by Jessespike · Apr 06, 2016 at 06:53 PM
Where do you set the PlayerDatabaseManager instance? Also helps if you paste the error in the quesiton.
if (instance == null) instance = this;
wait so you have two static instances of PlayerDatabase$$anonymous$$anager? One in PlayerDatabase$$anonymous$$anager (which I assume is the first script you showed) and another in "database management script"? How are you setting the PlayerDatabase$$anonymous$$anager instance there in there? Paste the error please.
no, just a single instance. I used the generic term of database management script.
One error: Cannot convert int to Stats. Not sure where to cast it given the length of the line.
Answer by aida12_99 · Apr 06, 2016 at 08:47 PM
Let's assume that you have a script in GameObject A with name ScriptA, and another script in another GameObject B with name ScriptB. ScriptB wants to get access to a public method in ScriptA. You need to do this in ScriptB:
GameObject.Find("GameObject_A").GetComponent< ScriptA >().public_method()
Singletons are probably a better choice for manager classes. Which is the case here,
Answer by meat5000 · Apr 07, 2016 at 06:49 PM
You should consider creating a struct to contain your data and creating one List of type YourStruct. It seems you have three custom Types anyway.
Passing Lists is exactly the same as passing other variables. Just make sure your container has been initalised before trying to populate it with data.
Just make public List<Vitals> Vitals;
static
public static List<Vitals> vitals;
Then access it
PlayerDatabaseManager.vitals;
If there's only going to be one of them, this is perfect.
Or you some kind of instance method :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerDatabaseManager : MonoBehaviour {
public static PlayerDatabaseManager instance;
public List<int> vitals = new List<int>();
void Awake()
{
if(instance == null) instance = this;
vitals.Add(32);
vitals.Add(21);
}
}
using UnityEngine;
using System.Collections;
public class CallDatabase : MonoBehaviour {
void Start ()
{
Debug.Log(PlayerDatabaseManager.instance.vitals[0]);
Debug.Log(PlayerDatabaseManager.instance.vitals[1]);
}
}
I simply use GetComponent a lot to access the particular class containing my one list. I tend to use static at a minimum. Sometimes I cache the scripts if I use it a lot and sometimes I make an instant call, so the memory is freed up afterwards.
public class Stats
{
public int Constitution{get; set;}
public int Strength{get; set;}
public int Sta$$anonymous$$a{get; set;}
public int Dexterity{get; set;}
public int Intelligence{get; set;}
public int Wisdom{get; set;}
public int Focus{get; set;}
public float Speed{get; set;}
public Stats( int constitution, int strength, int sta$$anonymous$$a, int dexterity, int intelligence, int wisdom, int focus, float speed)
{
this.Constitution=constitution;
this.Strength=strength;
this.Sta$$anonymous$$a=sta$$anonymous$$a;
this.Dexterity=dexterity;
this.Intelligence=intelligence;
this.Wisdom=wisdom;
this.Focus=focus;
this.Speed=speed;
}
}
Something like this?
Aren't structs for values that don't change and are short lived?
Short lived? No. They have the lifespan of a normal variable.
Structs are created with a constructor and passed, which in essence is the same as creating a Vector3. I cant say Ive ever tried modifying a field directly but if that doesnt work its easy enough to reconstruct the data. In your struct class you can make a second overload of the constructor which accepts a completed block, allows you to modify a field and spits it back out.
A typical usage scenario is a block management game which creates random blocks. A List<Struct>
in that case is ideal for recording all the information about each block with its irregular data and also holds a reference to the actual object itself.
But the advantage is simply working with a single List and having all the data contained there-in, which is very useful when trying to track multiple players. Syncing up multiple Lists is asking for something to go wrong.
Also note that there are two types of 'struct'. One is a struct (who'd have thought it) and the second is a Class which behaves as a struct. Your code there is the Class type (and in JS must extend System.ValueType).
The difference is stack vs heap and reference vs value. If this means nothing to you, it may be worth doing a little research.
Thanks for the info $$anonymous$$eat, but it's not answering my question. I'm trying to pass a list of stats in an RPG from the singleton which has all the database calls, and the script I'm calling it from where I want to display those values.
$$anonymous$$odified my answer.
I dont really use singletons so someone might tell me I've got the pattern wrong. I've certainly seen otherpeople using it all differently but I think thats the point, It's a pattern not a particular operator or keyword.