Loops and Asynchronous Actions

0 favourites
  • 8 posts
From the Asset Store
For Adventure, Action, Heroes, Sci-fi and Action-RPG games.
  • I'm trying to build a combat system for a JRPG. Please pardon the placeholder assets.

    There are multiple types of actions. First the player selects. Then the AI. Then, the actions are meant to resolve in a predetermined sequence. Actions of the same type resolve simultaneously.

    What I hoped to do was:

    1) Create a text box above the head of each character using "Magic", containing text that indicates the skill that character is using.

    2) Destroy that text box and its text.

    3) Spawn an effect animation object at the location of the character's target, using an instance variable that tracks the target's UID.

    4) Display the skill animation

    5) show hitstun, calculate damage, and do a bunch of other stuff that I haven't been able to implement yet.

    So it should looks something like this:

    media.giphy.com/media/v1.Y2lkPTc5MGI3NjExNjFub2k1dG0waHZ3Y29mamJlc3dmbDhwcThlbGdweWt3MGQzbGljbyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/dZIUKNJS52E5tqdTHP/giphy.gif

    Right now, the system is doing what I want it to do, however, this is how I've had to code it:

    It seems to me that the effect I've produced is illusory, in that it will go straight to hell as soon as there are multiple instances of enemy and/or player. And there will probably be a lot of clutter and nonsense in the meantime. However, this is the most workable solution I've been able to reach.

    Everything else I've tried seems to have been stymied by one or more of the following problems:

    1) I'm not sure how to deal with targeting, since both the acting character and its target will both be members of the character family.

    2) Asynchronous weirdness caused by the "Wait" actions.

    3) Weird scope problems, where some elements within the loop appear not to be accessible, even after these loops are completed.

    If anybody can help me understand these problems, or point to possible solutions, I would much appreciate it.

    drive.google.com/file/d/18kHmHn-rUvJiba-nGwPef4jUxKr3al8O/view

  • As you mentioned in the post, this way of making it will get overblown when you add detail and many things happening at once with many instances. You need more control over the gameplay and since it's JRPG I would create a turn system in an array. For each action you can add the person's turn to a new row and then run each row in order and pop off the top row when it's done. This also provides visibility of what's happening. Within the row you can include things like the target UID, even the damage calculated so you can see it before it's acted out.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It's difficult to tell from these screenshot and your project file is inaccessible. But I would change the way the whole thing is triggered. That "Else Trigger once" event probably won't work correctly because numberMagic is a local non-static variable, it gets reset at the end of every tick. (unless it's inside a function)

    Be very careful with Trigger Once condition, it can cause all kinds of problems when used incorrectly.

    Also instead of "waits" I suggest using Timer behavior - it gives you much better control, you can pause/cancel it, run different timers per each instance, and you can use "On timer" event to schedule the next action.

  • As you mentioned in the post, this way of making it will get overblown when you add detail and many things happening at once with many instances. You need more control over the gameplay and since it's JRPG I would create a turn system in an array. For each action you can add the person's turn to a new row and then run each row in order and pop off the top row when it's done. This also provides visibility of what's happening. Within the row you can include things like the target UID, even the damage calculated so you can see it before it's acted out.

    I haven't used arrays much before. Can you give an example of how you might organize one in this case?

  • Thanks for your suggestions!

    It's difficult to tell from these screenshot and your project file is inaccessible. But I would change the way the whole thing is triggered.

    I've changed the accessibility on the file, if that helps.

    That "Else Trigger once" event probably won't work correctly because numberMagic is a local non-static variable, it gets reset at the end of every tick. (unless it's inside a function)

    That part of the code seems to be working alright. That is, it skips over the magic resolution step if no characters are using magic. It's when characters are actually using magic that I run into various problems. Do you think it's still likely that the non-static variable is causing problems? Any idea how?

    Be very careful with Trigger Once condition, it can cause all kinds of problems when used incorrectly.

    Yeah, I'm kind of fumbling in the dark, doing guesswork based on other projects I'm learning from. What is the correct way to use "Trigger Once"?

    Also instead of "waits" I suggest using Timer behavior - it gives you much better control, you can pause/cancel it, run different timers per each instance, and you can use "On timer" event to schedule the next action.

    This seems like a good suggestion. I will look into it.

  • Of course it depends on the exact design of the game and how it works but for example on start of turn you can predetermine the enemy actions then have the player do theirs and add each action to a row as such :

    1. enemy1 > block > target=enemy1

    2. player1 > magic > target=enemy1

    then you run the actions in turn, popping the top of the array each time so you are always running row 0.

    1. run block function on family instance enemy1(updates variable that they are blocking)

    2. run magic function on family instance enemy1(apply damage to enemy1, show magic related UI etc)

  • I don't recommend applying state-machine approach for everything, this often results in incredibly entangled code. Instead of checking variables on every tick and using "trigger once" condition, it's much easier to create a function. Call the function from the event where the state changes (for example when the target is selected). This way you can be certain that it will be executed once only. You can add console logging to see if and when the event is executed.

    What is the correct way to use "Trigger Once"?

    I can tell you where you shouldn't use it - inside of loops, triggers, functions, and with objects that have multiple instances. It's often difficult to predict how it will work in these situations. For example, this code seems so convenient, but it will glitch if there is more than one enemy instance with HP=0

    Enemy HP=0
    Trigger once: Enemy play "death" animation
    
  • Thanks to both of you for your advice!

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