Get variables from another script method loop

Hi there,

I have a method that returns random data from “drivers/users” table in my Database.cs

Database.cs

public void getRandomDriver()
    {

        List<string> fields = new List<string>();
        fields.Add("ID");
        fields.Add("name");

        SQLiteDB.DB_ConditionPair condition = new SQLiteDB.DB_ConditionPair();
        condition.fieldName = "ID";
        condition.condition = SQLiteDB.DB_Condition.EQUAL_TO;
        condition.value = "2"; // TODO: random ID

        DBReader reader = db.Select("drivers", fields, condition);
        while (reader != null && reader.Read())
        {
            string **driverID** = reader.GetStringValue("ID");
            string **driverName** = reader.GetStringValue("name");
        }

    }

Another.cs

Database database;

void getRandomDriver()
    {
        database = GameObject.Find("Database").GetComponent<Database>();

        database.getRandomDriver();
        //get those variables here

    }

I would like to access driverID and driverName from Database.cs getRandomDriver() method.
How can I do that?

Could you at least point me direction?

I do not have much knowledge about C# but I have to create a prototype and I have no time for basics (in a perfect world I would have).

Thank you.

You should have a Driver class in order to hold those data IMHO.

// Driver.cs
public class Driver
{
    public string Name { get ; private set ; }
    public string ID { get ; private set ; }
    public Driver( string id, string name ) { ID = id ; Name = name ; }
}

  // Database.cs
 public Driver getRandomDriver()
 { 
     List<string> fields = new List<string>();
     fields.Add("ID");
     fields.Add("name");

     SQLiteDB.DB_ConditionPair condition = new SQLiteDB.DB_ConditionPair();
     condition.fieldName = "ID";
     condition.condition = SQLiteDB.DB_Condition.EQUAL_TO;
     condition.value = "2"; // TODO: random ID

     DBReader reader = db.Select("drivers", fields, condition);
     while (reader != null && reader.Read())
     {
         return new Driver( reader.GetStringValue("ID"), reader.GetStringValue("name") ) ;
     } 
     return null ;
 }

// Another.cs

 Database database;
 
 void getRandomDriver ()
 {
     database = GameObject.Find ("Database").GetComponent<Database> ();
     
     Driver driver = database.getRandomDriver ();
     if( driver != null )
         Debug.Log( driver.ID + " " + driver.Name ) ;         
 }

Other possibility, using out parameter

  // Database.cs
 public void getRandomDriver( string out ID, string out name )
 { 
     ID = string.Empty;
     name = string.Empty;
     List<string> fields = new List<string>();
     fields.Add("ID");
     fields.Add("name");

     SQLiteDB.DB_ConditionPair condition = new SQLiteDB.DB_ConditionPair();
     condition.fieldName = "ID";
     condition.condition = SQLiteDB.DB_Condition.EQUAL_TO;
     condition.value = "2"; // TODO: random ID

     DBReader reader = db.Select("drivers", fields, condition);
     while (reader != null && reader.Read())
     {
         ID = reader.GetStringValue("ID") ;
         name = reader.GetStringValue("name") ) ;
     } 
 }

// Another.cs

 Database database;
 
 void getRandomDriver ()
 {
     database = GameObject.Find ("Database").GetComponent<Database> ();
     
     string ID;
     string name ;
     database.getRandomDriver ( out ID, out name );
     Debug.Log( ID + " " + Name ) ;         
 }

@Mehul-Rughani Thank you so much!

Hello, @JaRo234

Database.cs

public string getRandomDriver ()
	{
		
		List<string> fields = new List<string> ();
		fields.Add ("ID");
		fields.Add ("name");
		
		SQLiteDB.DB_ConditionPair condition = new SQLiteDB.DB_ConditionPair ();
		condition.fieldName = "ID";
		condition.condition = SQLiteDB.DB_Condition.EQUAL_TO;
		condition.value = "2"; // TODO: random ID
		
		DBReader reader = db.Select ("drivers", fields, condition);
		string driverIdAndName = "";
		while (reader != null && reader.Read()) {
			driverIdAndName += reader.GetStringValue ("ID") + "$";//You can Put any special character you want
			driverIdAndName += reader.GetStringValue ("name");
		}
		return driverIdAndName;
	}

and

Another.cs

Database database;
	
	void getRandomDriver ()
	{
		database = GameObject.Find ("Database").GetComponent<Database> ();
		
		string driverIdAndName = database.getRandomDriver ();
		string driverId = driverIdAndName.Substring (0, driverIdAndName.IndexOf ("$"));
		string driverName = driverIdAndName.Substring (driverIdAndName.IndexOf ("$") + 1);
		
	}

Hope you are looking for this. :slight_smile: