Can I use a string as a command?

I have 55 images named alien1 - alien55 (I have a reason not to use a PImage array), instead of typing the draw() command for each one, I used a for loop:

void draw() {
  
  for (int i; i=0; i<54 i=i+1){
    if (aliendraw[i]==true){
      "alien"+i+".draw()";

    }
  }

obviously, this won’t work outside of a string, which brings us to my question, is there a way to make this work? in other coding languages there’s eval() but it’s not in processing.
help will be appreciated, thanks in advance.

There is a data structure for this.

You can place the function (drawAlien()) inside
a package (class) together with its properties (for example aliendraw and x,y)

here is an example of a class that gets used by the aliens in an array



Alien[] aliens = new Alien [54];

PImage img;

void setup() {
  size(800, 600);

  img=loadImage("Alien.jpg");

  for (int i=0; i<54; i++) {
    aliens[i] = new Alien();
  }
}

void draw() {
  for (int i=0; i<54; i++) {
    if (aliens[i].drawThis) {
      aliens[i].draw();
    }
  }
}

// ============================================================================

class Alien {

  boolean drawThis=true;

  float x=random(width);
  float y=random(height);

  void draw() {
    ellipse(x, y, 5, 5);
    // image ( img, x, y);
  }
}//class
//



1 Like

Welcome to the forum.

I assume you are working in ‘Java’ mode and it is true that Java does not have the equivalent of the eval() so you will not get Java to execute a String variable.

The basic syntax errors in the for statement you posted indicate that you are very new to programming so I would need to see more code especially the part where you load the images.

I would be interested to know why you don’t want to use an array to store the images when you use an array to decide on their visibility.

2 Likes

I wrote in the post that I have a reason not to use a PImage array.

Under the for loop I wrote “obviously, this won’t work outside of a string”, I was just trying to show roughly what I’m trying to do.

They mean that for loop itself has a syntax error, not the eval part.

Like others say, maybe you could explain what you’re doing, or what about giving us a simplified example with 3 objects?

If you definitely cannot avoid making 50 variables, I think Java reflection is the way to access class members programmatically, but using this is indicates that it is a bad design pattern.
https://forum.processing.org/two/discussion/23721/access-processing-pconstants-dynamically-using-reflection.html

Hi @Naorius,

What is this reason?

Cheers
— mnse

1 Like

I know what you want to do - you want to

  1. use an index value (in your example 1 - 55) to access an image.
  2. avoid using a PImage array (for some unknown reason)

As pointed out you don’t want to have 55 variables e.g.
PImage image1, image2, image3,... image55;
If you have done this then think again as it would be an absolute nightmare to work with.

What is needed is a named container to enable access to the images individually using their ID number (1 - 55).

Given the minimal information you have provided I can think of two possibilities the humble array or a HashMap. For the problem you have described I cannot think of a realistic way of solving the problem without using a container.

I suspect that you are using these images as sprites in which case I would encapsulate the image inside a user defined sprite class. If you do this then the sprite is aware of its own visibility and could use a HashMap which will give you freedom to use different keys.

2 Likes

In my example you have an image inside the class
and give each alien an id and with that load
a different image for each alien

So no array of images, just an array of aliens :wink:

You can do the loading in the constructor of the class

At the moment the class doesn’t have an explicit constructor

2 Likes

This is a working example based on @Chrisir’s suggestion and no arrays LOL.

HashMap<Integer, Sprite> sprites = new HashMap<Integer, Sprite>();
int nbr = 100;

void setup() {
  size(600, 400);
  for (int i = 0; i < nbr; i++) {
    PImage img = loadImageSimulator(i, (int)random(20, 80), (int)random(20, 80));
    Sprite s = new Sprite(random(width - img.width), random(height - img. height - 30), img);
    sprites.put(i, s);
  }
  randomiseVisibility();
}

void draw() {
  background(64);
  for (int i = 0; i < nbr; i++) {
    sprites.get(i).draw();
  }
  textAlign(CENTER, CENTER);
  textSize(20);
  fill(255);
  text("Click mouse to see different sprites", 0, height-30, width, 30);
}

void mouseClicked() {
  randomiseVisibility();
}

// Each has approximately 10% chance of being visible
void randomiseVisibility() {
  for (int i = 0; i < nbr; i++) {
    sprites.get(i).visible = random(1) > 0.1 ? false : true;
  }
}

class Sprite {
  float x, y;
  PImage img;
  boolean visible = true;

  Sprite(float x, float y, PImage img) {
    this.x = x;
    this.y = y;
    this.img = img;
  }

  void draw() {
    if (visible) {
      image(img, x, y);
    }
  }
}

// This method simulates the loadImage function in that gien
// some parameters it returns a PImage object
PImage loadImageSimulator(int id, int w, int h ) {
  PGraphics pg = createGraphics(w, h);
  int r = (int) random(128, 255);
  int g = (int) random(128, 255);
  int b = (int) random(128, 255);
  pg.beginDraw();
  pg.background(r, g, b);
  pg.textSize(min(w, h) * 0.5);
  pg.textAlign(CENTER, CENTER);
  pg.fill(0);
  pg.noStroke();
  pg.text("" + id, 0, 0, w, h);
  pg.endDraw();
  return pg.get(); // convert PGraphics object to PImage object
}
2 Likes

I already tried and when I launched the code it was extremely laggy, also my teacher says he doesn’t want me to use stuff he didn’t teach yet.

I think you misunderstood me, I already have 55 variables, what I need help with is identifying them within the for loop, because they don’t have brackets (these things ] [ ) I can’t use i to identify them normally, then I remembered how you can “merge” variables and words inside strings, what I want to do is merge “alien” and i so that the for loop will execute the command “alien0.draw()” than “alien1.draw()” all the way to 54.

I don’t really understand this code, so I’ll have trouble if I need to change something

In that case ignore my code above since you obviously have not been taught some of the Java language features used.

So has your teacher taught arrays yet???

If the answer is NO then

  • then why have you used an array in the code you posted if (aliendraw[i]==true){?
  • did your teacher expect you to create and manipulate as many as 55 alien variables without arrays?

If the answer is YES then

  • use arrays as trying to manipulate 55 variables as a group without a collection object (array, HashMap …) is nonsensical.
2 Likes

Sorry I missed this post but it explains what you are trying to do much better than the original one.:smile:

So the short answer is no, there is no simple Java language feature that would take a String literal such as “alien42” and use that to manipulate a variable called alien42

It maybe possible to use Java Reflection to do something like this but that is an advanced language feature well beyond arrays and other Java collections which would be the preferred way of handling data sets.

I think you need to go back to the teacher for guidance on what is expected of you.

1 Like

The course I’m taking provides custom classes that are easier to use, one of those is a custom class for images, we learned arrays for all the classes that weren’t replaced but since images are replaced we weren’t taught PImage arrays, I just googled “an array of images processing” and implemented a PImage array, but as I said in a previous post it was extremely laggy and my teacher didn’t want me to use that.

Oh well, thanks for the help anyway.

You can simply store all images in
an Hashmap

  • you can use “alien”+str(i)+“.jpg” (for loading images) or “alien”+str(i) as a name (for the hashMap)
HashMap<String, PImage> listHM = new HashMap();
float [] alienX=new float [56], alienY=new float [56];

// ----------------------------------------------------------------------------------

void setup() {
  for (int i = 1; i <56; i++) {

    String name1="alien"+str(i)+".jpg";
    println(name1);
    PImage img = loadImage(name1);

    String name2="alien"+str(i);
    listHM.put(name2, img);

    alienX[i]=random(width);
    alienY[i]=random(height);
  }
}

void draw() {
  for (int i = 1; i <56; i++) {
    String name="alien"+str(i);
    PImage img = listHM.get(name);

    image(img, alienX[i], alienY[i]);
  }
}

1 Like