Can scripts access Global variables?

This forum is currently in read-only mode.
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • Like the title said, can I make a script that can read a global variable?

    like..

    ip - global variable (xxx.xx.x.x)

    script:

    host = global('ip')

    ??

    tried some ways but none worked, it is possible? if so, how?

    thanks

    edit:

    in script:

    System.Close seems not working, I'm doing something wrong??

    also guess i found a bug..

    700 objects with physics behaviour in the same layout = crash..

  • Global variables can be accessed like this:

    globalvar('ip')[/code:1ptszih6]
    
    System.Close does work but you have to put parenthesis at the end since it is a action:
    [code:1ptszih6]System.Close()[/code:1ptszih6]
    
    And that bug is because scripting is enabled, 700 objects created and no scripts being run.
    There are two ways to avoid that bug:
    1 If you aren't using python at all but uncheck "enable scripting"
    2 Just run a script every frame.  It doesn't even have to do anything, a comment will work.
  • Global variables can be accessed like this:

    globalvar('ip')[/code:1mbdgx9g]
    
    System.Close does work but you have to put parenthesis at the end since it is a action:
    [code:1mbdgx9g]System.Close()[/code:1mbdgx9g]
    
    And that bug is because scripting is enabled, 700 objects created and no scripts being run.
    There are two ways to avoid that bug:
    1 If you aren't using python at all but uncheck "enable scripting"
    2 Just run a script every frame.  It doesn't even have to do anything, a comment will work.
    

    I cant get the global thing to work..

    I made a script like this:

    host = globalvar('ip')

    got an error..

    tryed

    host = System.globalvar('ip')

    but didn't worked too.. i guess doesn't ever have this option on my construct cuz it doesn't appear in the right column..

  • Look at the Online Networking Tutorial with Python on the forums..... it has lots of examples of using Python with Global and Private variables. It should help you with your syntax problems.

    Just make sure you are using the latest version of Construct. Even the version used for the original tut has some major Python bugs.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Look at the Online Networking Tutorial with Python on the forums..... it has lots of examples of using Python with Global and Private variables. It should help you with your syntax problems.

    Just make sure you are using the latest version of Construct. Even the version used for the original tut has some major Python bugs.

    already got the online tutorial and none of them have inside script something like get global value, all of them uses the normal events to do that.

  • already got the online tutorial and none of them have inside script something like get global value, all of them uses the normal events to do that.

    You would think I would know since I wrote the tut...

    Here are a couple of examples from the tut that get a global variable value:

    connection.Send({'action': 'winner', 'player': System.globalvar('myNum')})[/code:1f3vwmrq]
    
    [code:1f3vwmrq]connection.Send({'action': 'gotkey', 'player': System.globalvar('myNum'), 'keyuid': System.globalvar('myuid')})[/code:1f3vwmrq]
    
    This accesses the global variables:  myNum and myuid.
    
    Edit:  Is "host" in your example a Python variable?  If it is not, that example will not work.
  • > already got the online tutorial and none of them have inside script something like get global value, all of them uses the normal events to do that.

    >

    You would think I would know since I wrote the tut...

    Here are a couple of examples from the tut that get a global variable value:

    connection.Send({'action': 'winner', 'player': System.globalvar('myNum')})[/code:2b1isbqs]
    
    [code:2b1isbqs]connection.Send({'action': 'gotkey', 'player': System.globalvar('myNum'), 'keyuid': System.globalvar('myuid')})[/code:2b1isbqs]
    
    This accesses the global variables:  myNum and myuid.
    
    Edit:  Is "host" in your example a Python variable?  If it is not, that example will not work.
    

    here is teh script, would it work?

    Start Layout

    host = System.globalvar('ip')
    port = 1203
    myclient = Client(host, port)[/code:2b1isbqs]
    
    also thanks for replies
  • You can't access global variable strings through Python without a crash...at least the last time I used Construct that was the case.

    My guess is that "ip" is a string... I see what you were probably getting at since you could use events as a workaround to still set a Python variable.

    Hope this helps!

  • You can't access global variable strings through Python without a crash...at least the last time I used Construct that was the case.

    My guess is that "ip" is a string... I see what you were probably getting at since you could use events as a workaround to still set a Python variable.

    Hope this helps!

    so variables it gives crash, ya.. and from what i can see, even the function 'globalvar' doesn't exist in the right table after I write 'System' and a dot...

    global numbers work at least?

    ya IP was a string like '127.0.0.1'

    the solution I found is in the first layout make a edit box with the ip, press enter and it saves in globalvar 'IP' the editbox.text

    next layout there are a invisible text object that at the beginning of the frame it sets his text to global 'IP' and during the python script the host gets the text from the invisible text object..

    there is a simple way??

  • Are global variables broken in python? They work for me. The crash related to text global variables was fixed a few builds ago. I made a typo with the syntax of accessing global variables in my other post. I forgot the "System.".

    System.globalvar('ip')[/code:1gtlnmm4]
    The name of the variable is case sensitive.  Is the name "ip" or "IP" in your cap?
    Also globalvar doesn't show in the right bar, it's just not listed.
  • Are global variables broken in python? They work for me. The crash related to text global variables was fixed a few builds ago. I made a typo with the syntax of accessing global variables in my other post. I forgot the "System.".

    System.globalvar('ip')[/code:1g5s642u]
    The name of the variable is case sensitive.  Is the name "ip" or "IP" in your cap?
    Also globalvar doesn't show in the right bar, it's just not listed.
    

    it was 'IP' and it worked now! thanks alot

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)