Function calls another func which returns value into func

Get help using Construct 2

Post » Tue Apr 12, 2016 5:01 pm

Hi guys,

Im stuck here:
When event is triggered (pressing the attack button, for example), the function "eCombatMath_Attack" is called. When this function is activated, it triggers an another function ("eAnimations_DummyAttack") which contains the "start attack" animations. After "start attack" is finished it returns a value to the "eCombatMath_Attack" function, so that the first function knows when to continue. If animation is over and ReturnValue is delivered, this function will check if event is true or false (for example: blocking) and then it will continue; after dealing the damage, it calls an 3th function ("eAnimations_DummyPositioning").

In short:
Press button > call calculation > show animation > if attacked player successfully blocks, then lower the maximum damage; if not, deal damage > show animation where player gets back to his starting position > idle

My problem is:
The function eigther doesnt return a value, or the first function doesnt wait for the ReturnValue of the second (wasnt able to debug that >.>).

Background:
Im trying to minimize the usage of variables (everything, whats possible at least, is contained in several arrays... 3x3D arrays are totally enough at this moment) and would like to only have multiusable functions (1 single function for combat attack maths for all kinds of attacks, 1 routine for all kind of player-/enemy-animations, unregarding which skin/individual animations they got, etc...). The aim is reaching a "lightweight" source and minimize CPU traffic.

Is that even possible (returning something from a function to an earlier triggered function which continues running)?

Bonus:
Yes, multiple questions in one suck, but I dont want to make an another topic for this... - did someone of you already make such "combatsystem" and has tips for me? Or are my thoughts bullsh*t about how the system works | got an better solution for this/has a link/site/wiki for me where I could improove the whole process | encountered the same problem | has an idea how to make this code more "elegant"?



Heres the code (sorry for not providing the *.capx, but I dont want to publish the whole source from this project ;-)):

(1) How the function is triggered (that you know where the params came from. (Arrays Y values: 9=current stamina; 11=cost for attack-type | checks if attack can be performed)
Image

(2) This is the "eCombatMath_Attack" function which triggers the animations (Math should be done here, Animations should be done there) and waits for the ReturnValue to continue
Image

(3) This is the "eAnimations_DummyAttack" function which is triggered from the previous function and should return value to the previous one after finishing
Image

The "eAnimations_DummyPositioning" function isnt implemented yet - first I need a solution for this question :D

If anything is unclear, please ask... I really need a way to get this done :/

Have a great day,
Proxy
B
24
S
14
G
9
Posts: 118
Reputation: 6,799

Post » Tue Apr 12, 2016 5:28 pm

Deferring function calls isn't really done that way. You would probably be better off using callback functions than return values. By the time line 4 of your second screenshot gets evaluated, the function isn't over, and it won't wait to evaluate, so "ReturnValue" will evaluate to "" (or the last returned value). Instead of line < Set return value to "Return" > in your last screenshot, call another function, like "eAni_DummyAttackFinished". This particular function will signal that the animation is done, and you can continue with the rest of your logic.
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Tue Apr 12, 2016 5:41 pm

Magistross wrote:Deferring function calls isn't really done that way. You would probably be better off using callback functions than return values. By the time line 4 of your second screenshot gets evaluated, the function isn't over, and it won't wait to evaluate, so "ReturnValue" will evaluate to "" (or the last returned value). Instead of line < Set return value to "Return" > in your last screenshot, call another function, like "eAni_DummyAttackFinished". This particular function will signal that the animation is done, and you can continue with the rest of your logic.


Thanks @Magistross!

You just confirmed my thoughts about the way I "wrote" that. Then I think, Im going on plan B, making a few more functions for the process.
may you can answer me the wiser way: calling the first function with all needed parameters for all following functions (like who attacks and what kind of attack is done) and handing them over from function to function untill the main process of attacking is done or putting them into vars (or storing them in an array)? Not quite sure what would result in a higher cpu-usage (even if its not worth mentioning those few bytes :D).

It is the first time that I deal with the logic for a dynamic combat system.. but Im optimistic, we'll be friends (somewhen ;) ).

Have a great day,
Proxy
B
24
S
14
G
9
Posts: 118
Reputation: 6,799

Post » Tue Apr 12, 2016 7:39 pm

It depends a lot on the scope of your system. Will it be used generically for a variable amount of entities or will it be more static ? If it's static, go ahead with arrays and vars. Otherwise, it'll be easier to propagate the arguments function to function.
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Wed Apr 13, 2016 6:24 am

Magistross wrote:It depends a lot on the scope of your system. Will it be used generically for a variable amount of entities or will it be more static ? If it's static, go ahead with arrays and vars. Otherwise, it'll be easier to propagate the arguments function to function.


Thanks again for your answer @Magistross!

Hmm... on which point you could say "static"? The first function is called with ~3-4 params (which wont change during the whole process; containing the source (player-number), attacking type, attacking duration (influenced by stats and needed to calculate the duration of animations), etc..); all other informations are gathered through the family instancevariables (or name of animations of sprites or X and/or Y coordinates from objects). The whole functions-process can be called (max.) 2 times parallel (if both players attack the same time). Think Ill hand over the params from func to func.

Have a great day,
Proxy
B
24
S
14
G
9
Posts: 118
Reputation: 6,799

Post » Wed Apr 13, 2016 11:18 am

After a function (with params) calls an another function (handing over the params) which also calls another function (with handing over too), the params are getting lost somewhere:

Image

eCombatMath_Attack (at the bottom) returns Param0 as 0, Param1 as 0, Param2 as 0 - should be (0)=1,(1)=1,(2)=5 here... the local vars are from the last test I did. After the "OwnedBy=Param0" everything is getting lost - the animation stuff is shown correctly, but then its messed up.. someone knows what Im doing wrong?


#Edit:
Ok, the wait-action is messing it up (even when I set both timers to "SprProtagonist.LiteTween.Duration")... someone knows a workarround for that? Forum search didnt returned anything usable :/
B
24
S
14
G
9
Posts: 118
Reputation: 6,799

Post » Wed Apr 13, 2016 2:14 pm

I dont think that 'wait' action does what you expect it to do.
Do the follow test.

Make 1 sprite.

Make 1 'every tick' condition.
Add 1 action : system > wait 5 seconds
Add second action: sprite > set x > x = Sprite.X + 10

What do you think it wil do ?

Also, i think the 'wait' action only delays the next action.
Also, i think a wait action nested deep in subevents wil not stop the other events from ticking. So the other functions get called, wait or not.

I dont know the LiteTween plugin. But it sounds as inbetweening. I suppose it does something that takes time. All other plugins that i know and do time-conditional things have triggers and expressions build in. So you can find out in what state they are.

De Platform (i know that one) has a 'on landing trigger'.

After a jump you dont code a wait action because you think that it takes x seconds for it to land. Nope, you use the 'on landing' trigger.
Does the LiteTween plugin has those triggers ?
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Wed Apr 13, 2016 2:27 pm

Look at this.

https://
drive.google.com/open?id=0B1SSuCVV8v74OTB4V2Foc21KWW8

Do you know what it wil do before you run it ?
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Wed Apr 13, 2016 3:00 pm

"Wait" action and function parameters don't mix that well as you figured, and local variables don't do better for that matter. And that's a shame, because it would allow us to do some kind of "closure" where variables have their own scope. However, the SOL is saved even after a wait action. Knowing this you can create a temporary object that will be used to store all variables for the duration of all functions.

Here's a quick exemple : https://dl.dropboxusercontent.com/u/700 ... osure.capx
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Wed Apr 13, 2016 3:45 pm

99Instances2Go wrote:Look at this.

https://
drive.google.com/open?id=0B1SSuCVV8v74OTB4V2Foc21KWW8

Do you know what it wil do before you run it ?


Thanks for your thoughts and example @99Instances2Go.

The LiteTween behavior does easings ( behavior-litetween_t70700 ), yes ;-). But in my project its just a placeholder for the "real animations" that Im going to make when the backend (the important stuff) is done.

The reason to have waits at the moment is: once an attack started, the attacking player "runs" to the attacked one (time needed for that should decrease with stats and increase with the equipment worn (platemail is heavy, this will have a high impact on your movement speed; cloth-armor will have no effect on your movement speed)), then the attack is done (damage will be modified if attacked player blocks on the right moment (different shields or different spells will give you a X,X seconds blocking"time", in which blocking is "true")).

I planned to realize this with timers - at least the part with movement/attacking speed.

Also thanks @Magistross, your example is great. That should do the job!

Wondering that no one else ran into this on the forums... is it a C2 issue or a JS problem in general? With some other scripting languages I never had issues with params in functions, even when infi-giving them through.
B
24
S
14
G
9
Posts: 118
Reputation: 6,799

Next

Return to How do I....?

Who is online

Users browsing this forum: Animate, BackendFreak, cb130felix, Kermad, Lancifer, piddlyPoo, yiuyiu6666 and 7 guests