Have a function run once? Python help?

For questions about using Classic.

Post » Thu Mar 10, 2011 6:10 am

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
[code:eaq1766k]
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?
B
2
G
3
Posts: 13
Reputation: 876

Post » Thu Mar 10, 2011 6:55 am

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.
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Thu Mar 10, 2011 7:28 am

[quote="WontoonRoo":3p8a5rqj]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
[code:3p8a5rqj]
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?[/quote:3p8a5rqj]

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.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Thu Mar 10, 2011 7:42 am

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.
B
2
G
3
Posts: 13
Reputation: 876

Post » Thu Mar 10, 2011 8:02 am

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: [url:3jktd6nz]http://www.scirra.com/forum/viewtopic.php?f=8&t=6880&p=53876#p53876[/url:3jktd6nz]

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.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Thu Mar 10, 2011 8:25 am

[quote="lucid":1357qq3e]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.[/quote:1357qq3e]

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
B
2
G
3
Posts: 13
Reputation: 876

Post » Thu Mar 10, 2011 8:50 am

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
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Thu Mar 10, 2011 11:47 am

For the python bit, change it from this:
[code:33d0t2mh]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.
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 3 guests