# How should i create a permanent reference to an object

**URL:** <https://discourse.processing.org/t/how-should-i-create-a-permanent-reference-to-an-object/27162>\
**Category:** Coding Questions\
**Created:** [January 18, 2021, 4:08pm UTC](https://discourse.processing.org/t/how-should-i-create-a-permanent-reference-to-an-object/27162 "2021-01-18T16:08:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![canslp](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@canslp](https://discourse.processing.org/u/canslp)\
**Post date:** [January 18, 2021, 4:08pm UTC](https://discourse.processing.org/t/how-should-i-create-a-permanent-reference-to-an-object/27162/1 "2021-01-18T16:08:27Z")

</div>

okay so i’ve recently learned that there are no formal pointers in javascript(??) what’s the best way to create a permanent reference to a dynamic object? i have a class in a game called ‘level’ that has all the information for that level and does all the processing for all the level objects etc, i’d like to have a general pointer called like ‘game level’ that references which level is currently being played. what’s the best way to do that without formal pointers? thanks

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 18, 2021, 4:23pm UTC](https://discourse.processing.org/t/how-should-i-create-a-permanent-reference-to-an-object/27162/2 "2021-01-18T16:23:31Z")

</div>

you can create a singular object that is represents a specific level

```auto
ArrayList<Level> levels = new ArrayList<Level>();
int c = 0;
Level current = levels.get(c);

void setup() {
    ...
}
void draw() {
    current.display();
}
void setLevel(int value) {
    c = value;
    current = levels.get(c);
}

```

---

<div class="post-metadata">

**Author:** ![canslp](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@canslp](https://discourse.processing.org/u/canslp)\
**Post date:** [January 18, 2021, 4:25pm UTC](https://discourse.processing.org/t/how-should-i-create-a-permanent-reference-to-an-object/27162/3 "2021-01-18T16:25:06Z")

</div>

hmm yea that was my backup plan, just to make an array with all the levels in it. is there really no way to make like an actual pointer to a level though?

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 18, 2021, 4:26pm UTC](https://discourse.processing.org/t/how-should-i-create-a-permanent-reference-to-an-object/27162/4 "2021-01-18T16:26:25Z")

</div>

what do you mean with “pointer”. Do you mean a way to reference a specific object?

If you do you can always just do something like:

```auto
int c = 0;
void draw() {
    Level current = getLevel(c);
}

Level getLevel(int id) {
   return ( levels.get(id) );
}

```

---

<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:** [January 18, 2021, 4:44pm UTC](https://discourse.processing.org/t/how-should-i-create-a-permanent-reference-to-an-object/27162/5 "2021-01-18T16:44:51Z")

</div>

> [@CodeMasterX](#):
>
> `levels.get(currentLevel);`

it would be enough to store currentLevel and use it as an index for `levels`

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 18, 2021, 4:46pm UTC](https://discourse.processing.org/t/how-should-i-create-a-permanent-reference-to-an-object/27162/6 "2021-01-18T16:46:53Z")

</div>

> [@CodeMasterX](#):
>
> `int c = 0;`

Its here : P  
(just filling some text to get 20 chars)
