Store Once In Python?

For questions about using Classic.

Post » Thu May 26, 2011 2:14 pm

Hi, i am just wondering how can you store a value once that is always changing with python?

I can do it easily enough if i put separate code in a start of layout event then send it in but i am running the needed script using a always event but want to store a temp value for comparisons/math and keep it in the same code. I need temp to be the original location -

def Move(speed):
temp = Sprite.X

I can't find the needed option in System so i am not sure how to do this. Right now it always updates as expected but i can't find how to do it how i need. I am quite new to python so i don't know if there is a non construct based way to do this. Does anyone know?
B
2
S
2
G
1
Posts: 121
Reputation: 1,092

Post » Fri May 27, 2011 2:47 am

You could do this in Construct as well (System compare with a global variable..say x). Just have an if statement that says, "if x ==0 " then set your variable.... then the first time it happens set x = 1. That way the variable will never be set again after that.
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Fri May 27, 2011 8:04 pm

Thanks i will try this :)
B
2
S
2
G
1
Posts: 121
Reputation: 1,092

Post » Sat May 28, 2011 9:55 am

This method doesn't seem to work for me, i do this (with indents, for some reason it doesn't like to copy/paste well)

tempa = 0
if tempa == 0:
tempb = Sprite.X
tempa = 1
Text.SetText(tempb)

but the tempb value always increases still. I know you said do it by variables but i need this to be dynamic without the need to setup a global in the main Project editor beforehand (do it all in a single bit of code directly basically).

I do this -

System.SetGlobalVar(tempb) = Sprite.X

but i see no Get global variable available in the System list. I tried just tempb and also System.global(tempb) etc but get errors like can't assign to function call etc.

Do you have a simple python code example to set then get globals if possible?
B
2
S
2
G
1
Posts: 121
Reputation: 1,092

Post » Sat May 28, 2011 10:24 am

It's a bit confusing. What exactly are you trying to achieve?
1) Do you need Python code or Construct events?
2) When will Sprite.X need to be temporarily saved? Once at start of layout? At several times when certain conditions are met?
3) How long will it need to be saved? Just before changing Sprite.X until it is changed? Or for a specified amount of time?

Please give an example (not programming specific)
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Sat May 28, 2011 10:02 pm

Yes this is in python code. Basically what i am trying to do is store a variable once like you would with a start of layout event however the python code i need it for is running using a always event in the editor.

I did this because i basically need it for a movement code i made which works fine other than this variable i need. The reason i want the variable is so i can work out the traveled distance from a start point and then run things in the code depending on the value.

So say a sprite was at X 80 rather than storing the value as 80 i need it to be dynamic for example if i randomize the start position which is why i was trying to get it via Sprite.X instead.

So basically once set the starting value would never be updated like it is currently. I need something like get original X position rather than the current x position which updates but i can't find the option for that.

Any ideas?
B
2
S
2
G
1
Posts: 121
Reputation: 1,092

Post » Sun May 29, 2011 2:14 am

You shouldn't think of Python code being seperate just because it is visually seperated. Python is active from the moment you activate it, you just don't see it.

+ Start of layout
-> Python script[list:5i4fg5ka][code:5i4fg5ka]count = 10[/code:5i4fg5ka][/list:u:5i4fg5ka]

+ On button left clicked
-> Python script[list:5i4fg5ka][code:5i4fg5ka]count += 1[/code:5i4fg5ka][/list:u:5i4fg5ka]

+ On button right clicked
-> Python script[list:5i4fg5ka][code:5i4fg5ka]count -= 1[/code:5i4fg5ka][/list:u:5i4fg5ka]

Although visually seperated, every time button is left clicked it will add to count: 11, 12, 13, etc. And if button is right clicked it will substract from it. That's just one variable 'count' throughout the whole project, not three independent variables.

So, just use the events to your advantage, to control how and when Python is used.

+ Start of layout
-> Python script[list:5i4fg5ka][code:5i4fg5ka]temp = Sprite.X

def move(speed):
# your movement code here, incl. using temp[/code:5i4fg5ka][/list:u:5i4fg5ka]

+ Always
-> Python script[list:5i4fg5ka][code:5i4fg5ka]move(200)
# calls your previously defined function[/code:5i4fg5ka][/list:u:5i4fg5ka]

This all from my head (my pc is broken-down). But it should work.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Sun May 29, 2011 2:54 am

Here is an example of what I was trying to show with the temp == 0 approach:

NOTE: This assumes you have to set the initial value of temp in the move function..obviously in this simple example it would make more sense to just set it to start on layout.



.cap download:
http://www.box.net/shared/kytvo2uq4h

Pyshell.cap came in handy looking for Python commands...If you don't use it I would encourage anybody that uses Python to try it out! It is in one of Silent Cacophany's posts.
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Sun May 29, 2011 6:12 am

I wouldn't recommend doing it this way.
You are defining a function over and over again on every tick. With Python's garbage collection this will be slow (and not needed)

A better way would be:

+ Start of layout
-> Python script[list:2p20bdhr][code:2p20bdhr]check = 0
temp = 0

def move(speed):
global check, temp
if(check == 0):
temp = Sprite.X
check = 1[/code:2p20bdhr][/list:u:2p20bdhr]

+ is moving
-> Python script[list:2p20bdhr][code:2p20bdhr]move(Sprite8Direction.speed)[/code:2p20bdhr][/list:u:2p20bdhr]

+ Always
-> Text: Set text to Sprite.X
-> Text2: Set text to str(Python("temp"))
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Sun May 29, 2011 2:44 pm

[quote="tulamide":az7clncy]You are defining a function over and over again on every tick. With Python's garbage collection this will be slow (and not needed)[/quote:az7clncy]
Good point! After taking the screenshot and posting I realized that and then got too lazy to repost. :wink: Plus your use of Python is much cleaner over all.

So go with Tulamide's example...it is much better!
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 8 guests