Function calls another func which returns value into func

0 favourites
From the Asset Store
Game "Little Dino Adventure Returns" with complete Source-Code (Construct3 / .c3p) + HTML5 Exported.
  • 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)

    (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

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

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

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

    Have a great day,

    Proxy

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

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

    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

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

  • 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

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

    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 :/

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

  • Look at this.

    https://

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

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

  • Try Construct 3

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

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

  • 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

    The LiteTween behavior does easings ( . 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.

  • How sure are you that the function wil not get called again in the time that the wait action waits + the time that the delayed actions excute ?

  • How sure are you that the function wil not get called again in the time that the wait action waits + the time that the delayed actions excute ?

    Up to 100% the "attack" process can only be active 1 time for each player; if cooldown=true (or "1".. no bools as global vars :/ ) then you wont be able to attack a second time. Cooldown will switch back to 0 (false) when the whole attacking process and returning to "starting position" is finished.

  • So there is more then one player? And they call the same function ?

  • So there is more then one player? And they call the same function ?

    Currently Im planning only 1 vs 1 combats (player vs. AI (PvE) and later Ill do an "arena-like" multiplayer (PvP)). Both players - or player and AI - will have access to the same "atack-process"; in other words there is only 1 routine for both. The "attack preparation" will be the same for both; the function called after "attack preparation" which shows the animations will have several if-clauses for identifying which player/AI got which hero and which animation (and set movement) has to be shown. Ild like to hold the source slim and compact, thats why.

    (The long version:)

    I thought, making a single "attack"-routine would be the easiest way to get this done, regarding that there will be several protagonists/anthagonists/whatever which will be doing exactly the same (regardeless of the animations, attackspeeds, etc. they got). The game will have its focus on stats/combats. I want to make it possible for (for example in PvP) a new player who doesnt really got good equipment to win against a player who already spend several hours in the game if he plays "better" (meaning: countering/bloking/studying the opponent). If you know the game (series) "Lost Souls"/"Dark Souls" - thats what I took as a "reference" in complexity and gameplay (yes, its nearly impossible to port such a milestone in gaming history into a mobile game, but Im not doing a clone - Im focussing on its complexity about what you can do with your hero/character).

    Magistross: just got home from work, tested it - and it works perfectly! Once again: thanks!

  • Not against a single "attack"-routine. But, i know for sure that the logic will break if (yet exactly do as advertised) if you gooing to call that function by differend players.

    There is only one wait action i will use. The wait 0 seconds action. I will not use it in any other cases. And excuse me for trying, but i still try to get you to do the same. My intentions are good, althought they probaly sound stubborn.

    I dont use 'wait' actions because:

    1/

    The time they wait is not reliable. That 0.5 seconds you wait looks fine. But then youre charakters get in a big fight. Trowing particles all over. Dropping the framerate temporaly. And before you know it, that 0.5 second will be 2 seconds in reality.

    2/

    If the event containing the wait actions executes again before the waiting is over, the logic will collapse. You will never know wich results pair with wich call.

    3/

    Pauzing the game will mess up the logic.

    4/

    And in youre case (and in so many other cases i have seen) the wait actions have no 'feeling' with the reality of the game. They just happen unconditional. At one point in developping you will not want players to overlap. But then 1 Collision is enough to break the logic. Because 'the wait' is not personal, it is not attached to a certain instance (player). There will (by instance) still be wait actions waiting while the objects that are meant to wait are killed and destroyed.

    5/

    Timers still are a more reliable solution. Because its a behaviour attached to an instance, and each instance has its own running timer. They dont mix up. On top you get triggers for free. You can leave them in the wild until a trigger gets your attention. Althougt there time is not reliable, they will never cross over.

    Thank you for taking me serious independed of my childish English. I cant do better.

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