# Global Scope

If you do not define your variables locally then they will be available globally within your scripts. This can be very dangerous, can cause difficult to debug bugs so use with care. It can be better to share data between scripts with a Lua module rather than to put data into the global scope for example.

Sometimes you do want to put functions / modules / data into the global scope. It's as easy as not putting local before your definitions. For example, you might want to use the [log module](https://github.com/subsoap/log) and to have it able to be used in all of your scripts without needing to require it in every script. To do so, you would require it once in your main entrypoint. For example, in the main.script of your main.collection.

```lua
log = require("log.log") -- log is global!
```

It's also possible to replace some tables within the global scope at runtime - such as replacing the builtin print function. Know that it's possible but use it with care as you can break the way the engine is meant to work if you're not careful!

If you don't want to mess with the global scope then always remember to add local before every definition you make in your scripts.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://read.bookofdefold.com/defold-a-z/lua-tips/global-scope.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
