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 › getting a for loop to reffer to a variable
Page Index Toggle Pages: 1
getting a for loop to reffer to a variable (Read 3469 times)
getting a for loop to reffer to a variable
Jun 10th, 2010, 11:29am
 
hi
i'm totally new here and i hope that i'm posting this question in the right place. i'm sure it's a very simple answer. i just can't seem to find the right search phrase to help myself. my question is why does this not work?

int maxPho1 = int(gugs[2]);
int maxPho2 = int(gugs[6]);
etc...
String name1 = gugs[0];
etc...
String home1 = gugs[2];
etc
gugs refers to the lines of a text doc

for(int i = 1; i < 16; i++){
 float newNum = map(maxPho[i],maxPhotos,minPhotos,newRectMax,newRectMin);
 rect(rectXStartPos,10*i,newNum,rectYsize);
 println(test);
text(name[i],leftDent,15);
text(home[i],leftDent,25);
}

there are various variables already defined correctly like maxPho1, maxPho2 etc, name1, name2 etc which are not causing the problem.
what i'm trying to do is get the for loop to loop through maxPho[i], name[i] etc and replace the i with a number so i was hoping to get maxPho1, maxPho2 etc. how is this done i processing?
any help is greatly appreciated.
all the best
Re: getting a for loop to reffer to a variable
Reply #1 - Jun 10th, 2010, 12:20pm
 
Quote:
what i'm trying to do is get the for loop to loop through maxPho[i], name[i] etc and replace the i with a number so i was hoping to get maxPho1, maxPho2 etc. how is this done i processing

You are effectively trying to specify a variable name from information stored in another variable This is not possible in Java and since Processing is based on Java then it is not possible in Processing.

You should use Arrays for maxPho1, maxPho2, name, home etc i.e.
name1 becomes name[1] etc.
Re: getting a for loop to reffer to a variable
Reply #2 - Jun 12th, 2010, 4:14am
 
hi quark
thank you for your reply. your suggestion certainly helped and my script is working Smiley
many thanks
ps if you are interested here's where i used the correct code www.lyndondaniels.com/processing/
Re: getting a for loop to reffer to a variable
Reply #3 - Jun 12th, 2010, 8:23am
 
Ehm.. it looks like I want something similar; only I would call it 'creating a dynamic object reference'.

I have objects that sprout children of their own. Each object has an id, which it passes to it's children, creating a 'heritage' array of id's. What I was planning to do was to feed this heritage array back to a global variable that determines what object is currently in focus.

Does the answer above mean that it's not possible to create a reference like 'object[0].object[2].object[0].object[5]' from an int array containing those index id's?
Re: getting a for loop to reffer to a variable
Reply #4 - Jun 12th, 2010, 8:48am
 
Rapatsk1, we probably need more detail on your object hierarchy and relations (perhaps show some simplified classes illustrating it), but if these are int arrays, you certainly cannot do that... Smiley
But there is probably another solution.
Re: getting a for loop to reffer to a variable
Reply #5 - Jun 12th, 2010, 10:54am
 
alright, actually it concerns the program I posted to the Exhibition yesterday; I'm actually using it to read xml data, and manipulate & extend it in the sketch. Ie selecting a node and adding children to it.

When it came to selecting the object, I ran into troubles. I wanted a to manipulate the data through a global selection-object:

Element currently_selected = [dynamic ref, set by the Element selected]

If you look through the code of the Text Universe example, you'll find some nasty measures (overloaded contructors, appending children after initialization) I took to cope with certain problems that probably complicate life.

Shall I open a new topic with simplified code that assesses the problem, or can you make something out of this?

Thanks in any case.
Re: getting a for loop to reffer to a variable
Reply #6 - Jun 12th, 2010, 4:56pm
 
This is a simplified view on the recursive class. How would I handle the focus of such object?

Code:
public class Element {

float x, y, z;
float orbitradius, sze; // radius determines the distance of children, sze is size of object
String txt; // content

Element[] heritage;
Element[] children;

boolean mouseover = false;
boolean is_selected = false;
boolean is_clicked = false;

Element(float psize, float radius, String txt) {

this.txt = txt;

this.sze = psize;
this.orbitradius = radius;

children = new Element[0];

}

Element(int id, Element parent, String txt) { // constructor for children

this.txt = txt;

this.sze = parent.sze/2;
this.orbitradius = parent.orbitradius/4;

children = new Element[0];

}

void updatePositions(float newx, float newy, float newz) {

x = newx;
y = newy;
z = newz;

// calculating position of children
for(int i = 0; i < children.length; i++) {
Point p = returnPosition(i, children.length, orbitradius);
children[i].updatePositions(p.x, p.y, p.z);
}

}

void updateSelection(boolean p_selected) {

if(p_selected) {
is_selected = true;
}

for(int i = 0; i < children.length; i++) {
children[i].updateSelection(is_selected);
}

if(!mousedragged) {

if(mouseover) {

if(mousePressed) {
is_clicked = true;
is_selected = true;

//set global selection reference here?
}

}
else {
// mouseover is false
}

}

}

void draw() {

pushMatrix();
translate(x, y, z);

for(int i = 0; i < children.length; i++) {

// draw lines to children
stroke(scolor);
line(0,0,0, children[i].x, children[i].y, children[i].z);

// recursive draw
children[i].draw();

}

// rectangle
stroke(scolor);
rect(-sze/2.0, -sze/2.0, sze, sze);

// using translated matrix to determine mouseover
mouseover = isMouseover(mouseX, mouseY);

popMatrix();

}
}
Page Index Toggle Pages: 1