# Can I use a string as a command?

**URL:** <https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [April 1, 2024, 4:34pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200 "2024-04-01T16:34:02Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naorius](https://avatars.discourse-cdn.com/v4/letter/n/2bfe46/32.png) [@Naorius](https://discourse.processing.org/u/Naorius)\
**Post date:** [April 1, 2024, 4:34pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/1 "2024-04-01T16:34:02Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 1, 2024, 4:56pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/2 "2024-04-01T16:56:35Z")

</div>

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)

> **[Objects](https://processing.org/tutorials/objects/)**
>
> The basics of object-oriented programming.

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

```auto

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
//

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 1, 2024, 5:22pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/3 "2024-04-01T17:22:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Naorius](https://avatars.discourse-cdn.com/v4/letter/n/2bfe46/32.png) [@Naorius](https://discourse.processing.org/u/Naorius)\
**Post date:** [April 1, 2024, 5:51pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/4 "2024-04-01T17:51:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Naorius](https://avatars.discourse-cdn.com/v4/letter/n/2bfe46/32.png) [@Naorius](https://discourse.processing.org/u/Naorius)\
**Post date:** [April 1, 2024, 5:52pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/5 "2024-04-01T17:52:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 2, 2024, 5:17am UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/6 "2024-04-02T05:17:24Z")

</div>

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](https://forum.processing.org/two/discussion/23721/access-processing-pconstants-dynamically-using-reflection.html)

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [April 2, 2024, 7:28am UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/7 "2024-04-02T07:28:31Z")

</div>

Hi @Naorius,

> [@Naorius](#):
>
> I have 55 images named alien1 - alien55 (I have a reason not to use a PImage array)

> [@Naorius](#):
>
> I wrote in the post that I have a reason not to use a PImage array.

**What is this reason?**

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 2, 2024, 9:01am UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/8 "2024-04-02T09:01:59Z")

</div>

> [@Naorius](#):
>
> I was just trying to show roughly what I’m trying to do.

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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 2, 2024, 9:16am UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/9 "2024-04-02T09:16:31Z")

</div>

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 😉

You can do the loading in the constructor of the class

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

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 2, 2024, 9:28am UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/10 "2024-04-02T09:28:38Z")

</div>

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

```auto
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
}

```

```auto

```

---

<div class="post-metadata">

**Author:** ![Naorius](https://avatars.discourse-cdn.com/v4/letter/n/2bfe46/32.png) [@Naorius](https://discourse.processing.org/u/Naorius)\
**Post date:** [April 2, 2024, 12:18pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/11 "2024-04-02T12:18:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Naorius](https://avatars.discourse-cdn.com/v4/letter/n/2bfe46/32.png) [@Naorius](https://discourse.processing.org/u/Naorius)\
**Post date:** [April 2, 2024, 12:22pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/12 "2024-04-02T12:22:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Naorius](https://avatars.discourse-cdn.com/v4/letter/n/2bfe46/32.png) [@Naorius](https://discourse.processing.org/u/Naorius)\
**Post date:** [April 2, 2024, 12:26pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/13 "2024-04-02T12:26:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 2, 2024, 2:49pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/14 "2024-04-02T14:49:57Z")

</div>

> [@Naorius](#):
>
> also my teacher says he doesn’t want me to use stuff he didn’t teach yet.

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.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 2, 2024, 3:04pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/15 "2024-04-02T15:04:58Z")

</div>

> [@Naorius](#):
>
> 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.

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

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.

---

<div class="post-metadata">

**Author:** ![Naorius](https://avatars.discourse-cdn.com/v4/letter/n/2bfe46/32.png) [@Naorius](https://discourse.processing.org/u/Naorius)\
**Post date:** [April 2, 2024, 4:07pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/16 "2024-04-02T16:07:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Naorius](https://avatars.discourse-cdn.com/v4/letter/n/2bfe46/32.png) [@Naorius](https://discourse.processing.org/u/Naorius)\
**Post date:** [April 2, 2024, 4:08pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/17 "2024-04-02T16:08:10Z")

</div>

Oh well, thanks for the help anyway.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 2, 2024, 8:48pm UTC](https://discourse.processing.org/t/can-i-use-a-string-as-a-command/44200/18 "2024-04-02T20:48:44Z")

</div>

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)

```auto
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]);
  }
}

```
