Can scripts access Global variables?

For questions about using Classic.

Post » Mon Dec 06, 2010 6:10 am

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..
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Mon Dec 06, 2010 10:26 pm

Global variables can be accessed like this:
[code:1ptszih6]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.
B
79
S
24
G
55
Posts: 4,755
Reputation: 41,273

Post » Tue Dec 07, 2010 12:02 am

[quote="R0J0hound":1mbdgx9g]Global variables can be accessed like this:
[code:1mbdgx9g]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.[/quote:1mbdgx9g]

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..
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Tue Dec 07, 2010 2:13 am

Look at the Online Networking Tutorial with Python on the forums.....viewtopic.php?f=8&t=6299 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.
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Tue Dec 07, 2010 2:43 am

[quote="scidave":2wdgoeqz]Look at the Online Networking Tutorial with Python on the forums.....viewtopic.php?f=8&t=6299 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.[/quote:2wdgoeqz]
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.
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Tue Dec 07, 2010 3:41 am

[quote="Guif0DA":1f3vwmrq]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.[/quote:1f3vwmrq]

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

Here are a couple of examples from the tut that get a global variable value:
[code:1f3vwmrq]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.
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Tue Dec 07, 2010 3:59 am

[quote="scidave":2b1isbqs][quote="Guif0DA":2b1isbqs]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.[/quote:2b1isbqs]

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

Here are a couple of examples from the tut that get a global variable value:
[code:2b1isbqs]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.[/quote:2b1isbqs]
here is teh script, would it work?
Start Layout
[code:2b1isbqs]host = System.globalvar('ip')
port = 1203
myclient = Client(host, port)[/code:2b1isbqs]

also thanks for replies
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Tue Dec 07, 2010 4:32 am

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!
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Tue Dec 07, 2010 6:22 am

[quote="scidave":2hf699pm]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![/quote:2hf699pm]
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??
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Tue Dec 07, 2010 11:58 am

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.".
[code:1gtlnmm4]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.
B
79
S
24
G
55
Posts: 4,755
Reputation: 41,273

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 4 guests