Local variables in recursive functions across multiple objs?

Get help using Construct 2

Post » Thu Sep 04, 2014 1:48 am

Do local (non-static, non constant) recursive function variables get overwritten when another object calls that same function?

To put it another way, I have an object type that I want to perform a given function over and over again until a set point but whenever another instance of that object type calls that function, the first object stops its recursion.

I've attached a screenshot and my capx to show the specific problem I'm having.

Screenshot.PNG


Battle Farm.capx


Thanks!
You do not have the required permissions to view the files attached to this post.
B
6
Posts: 13
Reputation: 283

Post » Thu Sep 04, 2014 8:26 am

Hi

If you read here: https://www.scirra.com/manual/149/function

You can see there are under: "Advanced function features"
"Note that functions calling other functions or recursing create a new "stack" of local variables. In other words, like in programming languages, local variables are unique at each level of function call. This does not apply to static local variables or global variables."

Anyway, got a question, how do i add functions to my game ? O.o Can't find it anywhere.
B
7
S
1
Posts: 55
Reputation: 810

Post » Thu Sep 04, 2014 8:43 am

Djip123 wrote:Hi

If you read here: https://www.scirra.com/manual/149/function

You can see there are under: "Advanced function features"
"Note that functions calling other functions or recursing create a new "stack" of local variables. In other words, like in programming languages, local variables are unique at each level of function call. This does not apply to static local variables or global variables."

Anyway, got a question, how do i add functions to my game ? O.o Can't find it anywhere.


Add the Function plugin
Add an event and choose Function -> On Function
Type function name in quotes
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Thu Sep 04, 2014 8:53 am

codah wrote:
Djip123 wrote:Hi

If you read here: https://www.scirra.com/manual/149/function

You can see there are under: "Advanced function features"
"Note that functions calling other functions or recursing create a new "stack" of local variables. In other words, like in programming languages, local variables are unique at each level of function call. This does not apply to static local variables or global variables."

Anyway, got a question, how do i add functions to my game ? O.o Can't find it anywhere.


Add the Function plugin
Add an event and choose Function -> On Function
Type function name in quotes


@codah

Could you be a nice guy to provide a link for downloading it ? ^^
B
7
S
1
Posts: 55
Reputation: 810

Post » Thu Sep 04, 2014 8:56 am

It's a standard plug-in within Construct 2, it's an Object you have to make in your Layout mode. Then, you can use it in your Event with a call.
Founder of Jadelight Studio, Developer of Libertas Aedifex, regular C2 Help contributor and fan of ambient music and Star Wars.
B
12
S
4
Posts: 255
Reputation: 2,008

Post » Thu Sep 04, 2014 9:00 am

Edit: what @inquiesco said

Right click on Object types (under Projects) and choose Insert new object
Double click 'Function'

Click on "Add event" in the event sheet
Double click on Function
Double click on "On function"
Type MyFunction between the quotes and click Done

You now have a function called "MyFunction"

To add code to it:

Click on "Add Action" on the event sheet next to Function | On "MyFunction"
Add any normal actions

You can also add sub events under the "Function | On "MyFunction" event
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Thu Sep 04, 2014 2:27 pm

Thanks for the reply but I've read the documentation and what it explains seem to contradict the behavior in my project.

According to the documentation, my function's variables (the Solder IDs) should be unique to each time the function is run. However, when the project is run, the variables are overwritten by more recent calls of the function.
B
6
Posts: 13
Reputation: 283


Return to How do I....?

Who is online

Users browsing this forum: divsyntax, volumetv and 13 guests