We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › retreiving value from an object
Page Index Toggle Pages: 1
retreiving value from an object (Read 380 times)
retreiving value from an object
Mar 25th, 2009, 11:00pm
 
new to processing, and new to programming, so i appreciate any assistance.

I am reading values from a text file into objects. the file has 3 columns.  

 for (int i=0; i< playerDots.length; i++) {
   float[] values = float (split(data[i], TAB));
   playerDots[i] = new PlayerDot(values[0], values[1], values[2]);
 }

this works fine. however, i only want to display objects that have a specific value, so i added this 'if' statement. this doesn't work.  also, the println that references this value doesn't spit out the value, but something like "texttoobjecttest$PlayerDot@df48c4
".  

void draw() {
 background(255);
 for (int i=0; i<playerDots.length; i++) {
  if (i+1 == playerDots[0]){ //this doesn't work
   playerDots[i].display();
   println(playerDots[0] + " " + playerDots[1]);
// }
 }
}

I am clearly not understanding how to access a value from an object.  this doesn't seem too complicated, but i am stuck.  any assistance would be appreciated.

Thanks!




Re: retreiving value from an object
Reply #1 - Mar 26th, 2009, 12:24am
 
I assume that you save the values in your PlayerDot constructor into a variable within the object?

e.g.
Code:

class PlayerDot
{
float a,b,c;
PlayerDot(float var1,flaot var2, float var3)
{
a=var1;
b=var2;
c=var3;
}
}


If it was like that, then you could do:

Code:

if(playerDots[0].a==i+1)
{
println(playerDots[0].a + " " +playerDots[0].b); //or something.
}
Re: retreiving value from an object
Reply #2 - Mar 26th, 2009, 12:29am
 
The problem is that we don't have an idea of what PlayerDot looks like...
But indeed, you can't compare an object with an integer. You have to get a value out of that object.
Either by accessing it directly, like if (i+1 == playerDots[0].value){ or on a cleaner way by getting it via a method: if (i+1 == playerDots[0].getValue()){ with the additional bonus that getValue() can compute something out of internal values.

The best way to print out an object is to make a String toString() method: this method can return any string, in general we put there a selection of the most significant values, eg. return "A=" + a + ", B=" + b;

PS.: I see I am late, I hope my answer is still useful...
Re: retreiving value from an object
Reply #3 - Mar 26th, 2009, 1:40am
 
Thank you very much for both of your quick replies. This is exactly what I was looking for.
Page Index Toggle Pages: 1