python access to global or private variables

For questions about using Classic.

Post » Fri Mar 05, 2010 3:31 am

I can't seem to get Python code to to read either global or private variables... it feels like I'm doing something wrong so maybe someone here like Lucid or Davo knows. Anybodies help if of course appreciated!

For example if mytext1,2,and 3 are python variables and inputtext is a Contruct global:

mytext = text
mytext2 = global('text')
mytext3 = str(Array.CurrentValue)

The first two attempts give an error. Only by sticking the inputtext in an Array could I then assign it to a Python variable.

Here is an example .cap: http://www.box.net/shared/giskjujjgl

How are people interfacing global and other variables in with Python?
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Sun Mar 07, 2010 2:57 am

Python access to global or private variables is currently broken, but here's a few related fixes that hopefully can be put into the next release: [url:3142aikn]http://www.scirra.com/forum/viewtopic.php?f=7&t=5921[/url:3142aikn].

In the mean time you can use a hybrid method like this:[url:3142aikn]http://www.box.net/shared/981crp7jvh[/url:3142aikn]
B
79
S
24
G
53
Posts: 4,735
Reputation: 40,233

Post » Sun Mar 07, 2010 1:19 pm

You just made my week! I had pretty much given up on this so it is absolutely fantastic that you found a workaround and fixed the bugs. Awesome work!
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Thu Mar 18, 2010 2:40 am

Now it seems like I am having problems in the opposite direction. I can't set a global variable from Python script.

For example:

A global called startGame.

In script:

globalvar('startGame') = 1
global('startGame') = 1 <--- i know shouldn't work based on the fixes
globalvar("startGame") = 1

I seem to have tried every combination I can think of and I get an error "can't assign to function call" I've tried casting to string, to int.. all sorts of combinations. Just doesn't work.

Any ideas?
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Thu Mar 18, 2010 10:38 am

Same as with actions in the event sheet:
System.SetGlobalVar("startGame", value)

System.globalvar("startGame") only retrieves the value of the global variable, aka you cannot modify the global with an assignment operator.
B
79
S
24
G
53
Posts: 4,735
Reputation: 40,233

Post » Thu Mar 18, 2010 11:57 pm

Thanks! Makes perfect sense now. :)
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Sat Mar 27, 2010 7:08 pm

Now, I can't seem to get private variable access in Python to work. I'm using the latest .99.84

I have a sprite called "player" and a private variable called "score".

I tried player.Value("score") which doesn't error but doesn't retrieve anything. I regular event retrieves the value fine so I know something is there. I also tried player.GetValue("score") which causes an error.

Any ideas why this doesn't work?
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Sat Mar 27, 2010 9:24 pm

yeah in order to use private variables in python the plugins that use them need to be recompiled. Until then they don't work... I'll see if I can get the necessary plugins recompiled before the next build.
B
79
S
24
G
53
Posts: 4,735
Reputation: 40,233

Post » Wed Apr 14, 2010 3:58 pm

Could python access to global/private variables please be added to the wiki in the python part of it?
Seems like many before me have had the same problems as me >_>
B
1
G
2
Posts: 4
Reputation: 633


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 7 guests