# How globals works in mouse interactions functions (python)

**URL:** https://discourse.processing.org/t/how-globals-works-in-mouse-interactions-functions-python/28333
**Category:** Processing.py
**Tags:** homework
**Created:** [March 8, 2021, 9:56am UTC](https://discourse.processing.org/t/how-globals-works-in-mouse-interactions-functions-python/28333 "2021-03-08T09:56:53Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![nuanz](https://avatars.discourse-cdn.com/v4/letter/n/c37758/32.png) [@nuanz](https://discourse.processing.org/u/nuanz)
#### Post date: [March 8, 2021, 9:56am UTC](https://discourse.processing.org/t/how-globals-works-in-mouse-interactions-functions-python/28333/1 "2021-03-08T09:56:54Z")

</div>

Hi guys !

We’re actually trying to create an interface in processing to play chess.  
The problem is the following:

- if the user clicks on something that isn’t empty (mouseClicked()), a global var named “pieceClicked” turns into a boolean True
- in the other function mouseDragged(), it normally catches the boolean and print the image on mouseX, mouseY
- but we tried to print these values in draw(), mouseClicked() then mouseDragged():  
- draw() is True as default  
- mouseClicked() returns True  
- mouseDragged() is False (??)

The global is announced in the draw()

---

<div class="post-metadata">

### Author: ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)
#### Post date: [March 8, 2021, 10:19am UTC](https://discourse.processing.org/t/how-globals-works-in-mouse-interactions-functions-python/28333/2 "2021-03-08T10:19:32Z")

</div>

Perhaps you need to use `global` in your mouse event functions?

```python
state = 'idle'

def draw():
    global state
    if state != 'idle':
        print('{} on frame #{}'.format(state, frameCount))
        state = 'idle'

def mouseClicked():
    global state
    state = 'click released'

def mouseDragged():
    global state
    state = 'dragged with button down'

```

---

<div class="post-metadata">

### Author: ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)
#### Post date: [March 10, 2021, 10:29pm UTC](https://discourse.processing.org/t/how-globals-works-in-mouse-interactions-functions-python/28333/3 "2021-03-10T22:29:47Z")

</div>

Hello, @nuanz, and welcome to the Processing Foundation Forum!

See [What are the rules for local and global variables in Python?](https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python). Though that references Python 3, and Processing Python Mode uses Python 2, the rules discussed there apply to both.

Essentially, if you assign a value to a variable with a function, and you wish that assignment to apply to a global variable, then declare the name of that variable as `global` within that function. If you do not declare it as `global` within that function, then you will have created a local variable by that name within the scope of the function, even while there is also a global variable with the same name outside the scope of that function.
