# Why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup

**URL:** https://discourse.processing.org/t/why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup/19585
**Category:** Summer of Code
**Created:** [April 9, 2020, 2:26pm UTC](https://discourse.processing.org/t/why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup/19585 "2020-04-09T14:26:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![endurance21](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/endurance21/32/8149_2.png) [@endurance21](https://discourse.processing.org/u/endurance21)
#### Post date: [April 9, 2020, 2:26pm UTC](https://discourse.processing.org/t/why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup/19585/1 "2020-04-09T14:26:02Z")

</div>

hi!  
i was setting up PhantomJS for headless browser testing with the existing code ,  
i get following error saying

exact output

```auto
type or paste code hereRunning "connect:server" (connect) task
Started connect web server on http://localhost:8000

Running "mocha_phantomjs:all" (mocha_phantomjs) task
Did you just try to use p5.js's text() function? If so, you may want to move it into your sketch's setup() function.

For more details, see: https://github.com/processing/p5.js/wiki/Frequently-Asked-Questions#why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup
TypeError: undefined is not an object (evaluating '(self.AudioContext||self.webkitAudioContext).prototype')

   at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:2978
   at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:2978
   in __webpack_require__ at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:84
   at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:2899
   at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:2972
   in __webpack_require__ at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:84
   at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:120

```

i didn’t understand the part saying

` why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup `  
which i think is something to do p5.js code

@lmccart @therewasaguy  
please help!

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [April 10, 2020, 2:39am UTC](https://discourse.processing.org/t/why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup/19585/2 "2020-04-10T02:39:36Z")

</div>

Might want to paste the rest of the code

---

<div class="post-metadata">

### Author: ![endurance21](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/endurance21/32/8149_2.png) [@endurance21](https://discourse.processing.org/u/endurance21)
#### Post date: [April 10, 2020, 8:23am UTC](https://discourse.processing.org/t/why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup/19585/3 "2020-04-10T08:23:29Z")

</div>

@paulgoux

this is what i did in gruntfile.js

```auto
  connect: {
      server: {
        options: {
          port: 8000,
          base: '.',
        }
      }
    },

    mocha_phantomjs: {
        all: ['test/*.html']
    }
  });

```

here is the index.html in test folder that is to rendered on headless browser

```auto
<html>
<head>
	<meta charset="utf-8">
	<title>TESTS</title>
	<script src="../lib/p5.js"></script>
	<script src="../lib/p5.sound.js"></script>
	<script>
		window.setup = function setup() {};
		new p5();
	</script>
	<script src="./testDeps/mocha.js"></script>
	<script data-main="test.js" src="./testDeps/require.js"></script>
	<link rel="stylesheet" href="./testDeps/mocha.css" />
</head>
<body>
	<div id="mocha"></div>
	<script>
		mocha.setup('bdd');
	</script>
</body>
</html>

```

dependecies installed for using headless browser id mocha-phantomjs

please see if it enough to know have a idea about the issue!

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [April 10, 2020, 10:36am UTC](https://discourse.processing.org/t/why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup/19585/4 "2020-04-10T10:36:20Z")

</div>

Just looking at the error would suggest that there is an issue with one of your variables. P5.js can assign variables before setup, however they do have limitations such as cos and sin can only be used in setup and draw, so if paired with variables called or created outside it might cause a problem. However your issue reminds me of when i declare an object variable without initialisation which then causes a problem later as the program cannot understand it, so i set the object to null.

---

<div class="post-metadata">

### Author: ![endurance21](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/endurance21/32/8149_2.png) [@endurance21](https://discourse.processing.org/u/endurance21)
#### Post date: [April 11, 2020, 5:06pm UTC](https://discourse.processing.org/t/why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup/19585/5 "2020-04-11T17:06:27Z")

</div>

@paulgoux thanks for your help !
