Have a function run once? Python help?

This forum is currently in read-only mode.
From the Asset Store
Full game Construct 2 and Construct 3 to post on Google Play
  • I'm extremely confused and bewildered and on and off with Construct, so once again I have a question. Is there a way to have a function run once? I honestly assumed that they did run once (since they are functions). I may be missing something, I do not know.

    Basically, I have been trying to make a message box that would appear once and be called anytime. Basically I have an event sheet that holds the functions for the game.

    On page 1 (The layout that sets stuff up), I have this

    File "gmdat" does not exist:
    My function: Add parameters (The parameters for the message box)
    Call function: myfunction (Forget picked objects)
    [/code:eaq1766k]
    
    On my page that contains the functions, one being for the message box,
    [code:eaq1766k]
    System: Create the Infobox object at params (3), (4) (x and y)
    System: Create a second instance of "Text" at Infobox's pivot point (Now there are only two instances of text, or there should be).
    Text: Set height to param(5)
    Text: Set width to param(6) - 30
    Text: Set text to param(7)
    [/code:eaq1766k]
    
    In theory, I should be able to then do this in Python:
    [code:eaq1766k]
    Text[1].Text = "This is a test."
    [/code:eaq1766k]
    ...since only one additional instance is made (I was forced to do it by the editor just to have it a global object that I can pull up anytime). The python script is in a sub event.
    
    Now here is where the problem comes in. When I put line of script in, anything above Text[0].Text did not work and gave me an error. I looked at the debugger and also saw that the game was creating an infinite amount of instances of "Infobox" and "Text".
    
    So I must ask, is there any way to have a function to run once (or is that impossible)? Also, why is the Python not grabbing other instances beyond the first?
  • post the cap file, or a screenshot of your event sheet and a paste of your python

    it could be any number of problems.

    here's some questions which would be helpful in troubleshooting if you decide not post it

    what is 'File "gmdat" does not exist' , and what does it have to do with what follows

    is the function that makes the message box appear made with events then?, and the function object? is it called from events

    is it python that calls it?

    make sure your python is in events, if you don't want it to run every tick, one time things like defining python functions, etc should be done in start of layout.

    The function object and python functions of course let you run them once, or any time they are called. There is another error somewhere.

    before we figure out why python won't respond to the [1] text object, lets try to get rid of the nonstop creation of text objects.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I'm extremely confused and bewildered and on and off with Construct, so once again I have a question. Is there a way to have a function run once? I honestly assumed that they did run once (since they are functions). I may be missing something, I do not know.

    Basically, I have been trying to make a message box that would appear once and be called anytime. Basically I have an event sheet that holds the functions for the game.

    On page 1 (The layout that sets stuff up), I have this

    > File "gmdat" does not exist:
    My function: Add parameters (The parameters for the message box)
    Call function: myfunction (Forget picked objects)
    [/code:3p8a5rqj]
    
    On my page that contains the functions, one being for the message box,
    [code:3p8a5rqj]
    System: Create the Infobox object at params (3), (4) (x and y)
    System: Create a second instance of "Text" at Infobox's pivot point (Now there are only two instances of text, or there should be).
    Text: Set height to param(5)
    Text: Set width to param(6) - 30
    Text: Set text to param(7)
    [/code:3p8a5rqj]
    
    In theory, I should be able to then do this in Python:
    [code:3p8a5rqj]
    Text[1].Text = "This is a test."
    [/code:3p8a5rqj]
    ...since only one additional instance is made (I was forced to do it by the editor just to have it a global object that I can pull up anytime). The python script is in a sub event.
    
    Now here is where the problem comes in. When I put line of script in, anything above Text[0].Text did not work and gave me an error. I looked at the debugger and also saw that the game was creating an infinite amount of instances of "Infobox" and "Text".
    
    So I must ask, is there any way to have a function to run once (or is that impossible)? Also, why is the Python not grabbing other instances beyond the first?
    

    A function is executed when you call it. If you call it once, it will be executed once. Events are checked on every tick. If its condition is true, the event will be executed.

    If 'File "gmdat" does not exist:' means that you check for the absence of a file then this condition will be true on every tick unless a file "gmdat" is created.

    Tick 1: Does the file exist? No. Call function

    Tick 2: Does the file exist? Still no. Call function again

    etc.

    To prevent calling the function more than once, create a global or private variable and use it as a switch. Set it to 0 when creating, then in your 'File "gmdat" does not exist:' add the condition my variable = 0

    and add the action 'add 1 to my variable'

    Now the event will only become true the first time it is checked and the function will only be called once.

  • Oh wait, I completely assumed that stuff on a page is read through once (except in some occasions). If I got you right, it means that runs through the page of events every single tick, a loop in and of itself? Oh wow. o.o

    This means I have to alter my approach to Construct drastically.

  • All event sheets are executed per tick in a loop, yes. (And the python scripts also)

    This thread might help you further understanding the workflow: http://www.scirra.com/forum/viewtopic.php?f=8&t=6880&p=53876#p53876

    EDIT: Ah, and before newt hops in, I have to tell you, that in case you will never again need the textbox again but really only once, you may also use the system condition 'trigger once' instead of the variable. The advantage of the variable use is that you can set that variable to 0 again at any time to let the text create again.

  • post the cap file, or a screenshot of your event sheet and a paste of your python

    it could be any number of problems.

    here's some questions which would be helpful in troubleshooting if you decide not post it

    what is 'File "gmdat" does not exist' , and what does it have to do with what follows

    is the function that makes the message box appear made with events then?, and the function object? is it called from events

    is it python that calls it?

    make sure your python is in events, if you don't want it to run every tick, one time things like defining python functions, etc should be done in start of layout.

    The function object and python functions of course let you run them once, or any time they are called. There is another error somewhere.

    before we figure out why python won't respond to the [1] text object, lets try to get rid of the nonstop creation of text objects.

    I first managed to solve the problem with the infinite functions. I totally deserve a fish in the face for that. :Op

    However, the python is still not working (It is on the Event Sheet, RPG functions)

    http://www.box.net/shared/cpzg7t01jm

  • I don't know what happens (seems like python isn't aware of instances of the text object), but while looking at your cap I thought I should say this:

    It seems you want to test for the absence of the file when the game starts. There is a system condition for events that need to be executed only once at start. You just create subevents to this one, and all those will only be executed once, when the layout starts:

    + System: Start of layout

    ++ File: [negated] File "gmdat" exists

    -> call the function

  • For the python bit, change it from this:

    Text[1].Text = "This is a test"[/code:33d0t2mh]
    to this:
    [code:33d0t2mh]SOL.Text.Text = "This is a test"[/code:33d0t2mh]
    
    SOL stands for selected object list, and is mainly useful for accessing objects just created it that event.
    
    You can avoid python altogether in this situation.  Just use the Set Text action in that event.  The newly created text object will be the only one affected.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)