# Is this possible to make gui\_script parameter?

**URL:** <https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934>\
**Category:** Questions\
**Created:** [January 10, 2017, 12:46pm UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934 "2017-01-10T12:46:08Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicloay](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/nicloay/32/1895_2.png) [@nicloay](https://forum.defold.com/u/nicloay)\
**Post date:** [January 10, 2017, 12:46pm UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934/1 "2017-01-10T12:46:08Z")

</div>

I’m working on tutorial and doing separated gui files for different tutorial steps.  
Each gui has own script some scripts reusable and i need a parameter to define some logic and reuse one script in several gui files.

---

<div class="post-metadata">

**Author:** ![dapetcu21](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dapetcu21/32/25120_2.png) [@dapetcu21](https://forum.defold.com/u/dapetcu21)\
**Post date:** [January 10, 2017, 12:51pm UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934/2 "2017-01-10T12:51:16Z")

</div>

One way of doing this would be to send a message with some initialisation data from your main script file when the game starts. I do that in my game to initialise everything with the current level data and to control the order in which gui objects acquire input.

---

<div class="post-metadata">

**Author:** ![nicloay](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/nicloay/32/1895_2.png) [@nicloay](https://forum.defold.com/u/nicloay)\
**Post date:** [January 10, 2017, 2:16pm UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934/3 "2017-01-10T14:16:55Z")

</div>

yeah. i understand that i can store settings in collection, but i thought to define everything just in gui files.

---

<div class="post-metadata">

**Author:** ![nicloay](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/nicloay/32/1895_2.png) [@nicloay](https://forum.defold.com/u/nicloay)\
**Post date:** [January 11, 2017, 3:10pm UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934/4 "2017-01-11T15:10:00Z")

</div>

I found one temporary solution how to make parameters in GUI files

1. need to create text box and add json data there
2. in `gui_script` decode this parameter as this `self.config = json.decode(gui.get_text(gui.get_node("config")))`

Just one question here will engine create mesh for text which has alpha = 0 or if text bounding box is out of the screen (rendering).

---

<div class="post-metadata">

**Author:** ![Daggett](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/daggett/32/2260_2.png) [@Daggett](https://forum.defold.com/u/Daggett)\
**Post date:** [January 11, 2017, 3:54pm UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934/5 "2017-01-11T15:54:28Z")

</div>

I guess script properties are what you need:  
[http://www.defold.com/manuals/script-properties/](http://www.defold.com/manuals/script-properties/)

Just add a line like this for each parameter you need at the top of your script:  
go.property(“parameter\_name”, parameter\_value)

The gui editor now generates input fields for you to configure your script.

In the script itself you can access your parameters via  
self.parameter\_name

---

<div class="post-metadata">

**Author:** ![nicloay](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/nicloay/32/1895_2.png) [@nicloay](https://forum.defold.com/u/nicloay)\
**Post date:** [January 11, 2017, 3:58pm UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934/6 "2017-01-11T15:58:50Z")

</div>

yeah. it works with game objects. but in my case it’s a gui\_script and if you will try to use it you will get this error

`level1_1step.gui_script:9: go.property can only be called outside the functions.`

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 11, 2017, 6:45pm UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934/7 "2017-01-11T18:45:12Z")

</div>

> [@Daggett](#):
>
> The gui editor now generates input fields for you to configure your script

You can never use anything from the go.\* namespace in a .gui\_script and vice versa. It will not work to put go.property() in a .gui\_script.

---

<div class="post-metadata">

**Author:** ![Daggett](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/daggett/32/2260_2.png) [@Daggett](https://forum.defold.com/u/Daggett)\
**Post date:** [January 11, 2017, 7:26pm UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934/8 "2017-01-11T19:26:24Z")

</div>

True, i somehow forgot that little detail between reading the post in the morning and answering it in the evening… 😳

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 12, 2017, 6:48am UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934/9 "2017-01-12T06:48:52Z")

</div>

> [@nicloay](#):
>
> need to create text box and add json data there

This sounds like a really hackish thing to do! Is it really that important that you are able to enter some configuration values from within the editor?

---

<div class="post-metadata">

**Author:** ![nicloay](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/nicloay/32/1895_2.png) [@nicloay](https://forum.defold.com/u/nicloay)\
**Post date:** [January 12, 2017, 9:19am UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934/10 "2017-01-12T09:19:29Z")

</div>

do you have any another ideas how to make gui\_script flexible and reusable?  
Sure it’s possible to write another game object script but it will just add one more level of abstraction. isn’t it?

---

<div class="post-metadata">

**Author:** ![Ragnar\_Svensson](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@Ragnar\_Svensson](https://forum.defold.com/u/Ragnar_Svensson)\
**Post date:** [January 12, 2017, 9:38am UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934/11 "2017-01-12T09:38:15Z")

</div>

Is it an option to put the reusable parts into a lua module and have separate gui\_scripts that use (and configure) that module?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 12, 2017, 9:45am UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934/12 "2017-01-12T09:45:22Z")

</div>

> [@nicloay](#):
>
> do you have any another ideas how to make gui\_script flexible and reusable?

Do you have an example of a gui\_script that you’d like to reuse? I would imagine that most gui scenes in a game have very few similarities and that gui\_script reuse isn’t really feasible. One place where it might be useful is for different kinds of popups, but in that case I’d pass configuration values when I post a “show” message to the popup.

---

<div class="post-metadata">

**Author:** ![nicloay](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/nicloay/32/1895_2.png) [@nicloay](https://forum.defold.com/u/nicloay)\
**Post date:** [January 12, 2017, 10:14am UTC](https://forum.defold.com/t/is-this-possible-to-make-gui-script-parameter/3934/13 "2017-01-12T10:14:02Z")

</div>

Ok. here is my structure of the project.  
I have separated collection for every level in the game. I do this to make possible to change background and make layout manually

 ![](https://forum-defold-uploads.b-cdn.net/original/2X/b/ba094018681352a6fbe1e0d778cd5921910a066e.png)  
`board` is reusable collection (actually it’s main game logic)  
`camera` is the script which post to the renderer information about camera size and position  
`tutorial_popup` is a game object where i actually attach all my tutorial popups. Each one is a separated gui file with the same script  
`fields_layout` is just a cosmetic element (board tiles and background)

And here is my tutorial step gui scene is

 ![](https://forum-defold-uploads.b-cdn.net/original/2X/8/825e48b0e6a35dc7323ce6e50b04c0a0057e1a23.png)  
Main thing here is the hole and which is inverted clipper for the gray shade.  
Another elements here is a pointer\_from and pointer\_to, i use this positions to animate pointer (hand) movement.  
stripe, left and right parts is a bottom info elements which show info to users.

So. each tutorial steps is almost the same, i need just to change hole position and info boxes. animation and another parts is the same. difference only is on which step do i need to show these popups.

Also i need to mention that in tutorial steps i need to lock user input to specific cells of the board. I’m doing this in the level config which is a json file and it looks like this

```lua
  "move_locks" : {
    "1": {
      "from": [5,4],
      "to" : [5,3]
    },
    "2": {
      "from": [3,4],
      "to" : [4,4]
    },
    "3": {
      "from": [3,3],
      "to" : [4,3]
    }
  },

```

so if board see that it’s a step number 3 it will look user input to cell[3,3] and will allow move only to cell [4,3]

On gui side in config node (that text node) which i use, i specify following text value `{"turn_number": 3}` Later i also plan to add another values like show\_shade or not and maybe something else. in script i use this code `self.config = json.decode(gui.get_text(gui.get_node("config")))`

ok and here is there gui\_script file content which handle it

1. Because all script appeared immediately with level, i need to disable all ui elements
2. In update cycle i listen board status and if turn\_number == turn\_number from config i show and animate elements

```lua
    if self.data_appeared == false and BoardData.turn_number == self.config.turn_number then
      self.initial_timer = (self.initial_timer or 0) + dt
      if self.initial_timer > INITIAL_TIMEOUT_BEFORE_APPEAR then
        self.data_appeared = true
        move_captions(self)
      end
    end

```

1. finally from the same module which indicate board status i listen if target\_cell (the cell where selected dice is moved) is not null and self destroy this component. i do that in the same update cycle

```lua
    if self.data_appeared and BoardData.target_cell and not self.destroy_in_progress then
      print("im here", self.config.turn_number)
      self_destroy(self)
    end

```

full script is available here [tutorial\_popup.gui\_script.lua · GitHub](https://gist.github.com/nicloay/1086ce56435aa9aa2a3c65420f2486ea)
