Store Once In Python?

This forum is currently in read-only mode.
From the Asset Store
Change the size and position of everything without calculating anything!
  • 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?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

  • Thanks i will try this

  • 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?

  • 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)

  • 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?

  • 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

      count = 10[/code:5i4fg5ka]

    + On button left clicked

    -> Python script

      count += 1[/code:5i4fg5ka]

    + On button right clicked

    -> Python script

      count -= 1[/code: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

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

    + Always

    -> Python script

      move(200) # calls your previously defined function[/code:5i4fg5ka]

    This all from my head (my pc is broken-down). But it should work.

  • 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.

    <img src="http://i52.tinypic.com/2uyjhiq.png">

    .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.

  • 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

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

    + is moving

    -> Python script

      move(Sprite8Direction.speed)[/code:2p20bdhr]

    + Always

    -> Text: Set text to Sprite.X

    -> Text2: Set text to str(Python("temp"))

  • You are defining a function over and over again on every tick. With Python's garbage collection this will be slow (and not needed)

    Good point! After taking the screenshot and posting I realized that and then got too lazy to repost. Plus your use of Python is much cleaner over all.

    So go with Tulamide's example...it is much better!

  • Thanks i have it working well now, the main thing was using a extra start of layout event as it makes things a lot simpler than having a single code in a always event. I then just use what tulamide suggests with a extra y position also if i ever need that.

    Btw a offtopic question i have is if anyone else find getting a python errors/debugging annoying? when i get them it seems to keep triggering popups and crashes. Hopefully this could be fixed somehow so there is only 1 error instead or a list/debug view rather than popups to make it a nicer process.

    Usually i fix any errors without a problem but they can show with a bad indent so sometimes they make things frustrating. I usually have to click close about 6 times each error i found but you can keep clicking for much longer if you are not closing them.

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