How do I pick objects individually without For Each?

0 favourites
  • 12 posts
From the Asset Store
In this template the music plays without looping in your game
  • Hi. I am having issues with picking in my project.

    I used to have it set up so that Fighter Control (the object the player controls) was in a container with Fighter Image (a single object with all the fighter animations). However, I was forced to turn Fighter IMage into a family of objects so the game would only have to load the sprites for the characters being used into image memory. But because of this, the game has broken because of the events depicted in the following image:

    (The dictionary is in a container with the control object, it's used for storing a lot of variables and whatnot.)

    This is the latest iteration of what I have been trying to make this work. Essentially, each control object has a different number for the AI variable. I use this to link Control and Image without using a container. However, when two players using the same fighter use the same move, it creates a ton of issues because the events have both fighter images picked at the same time, when they should be picking both images individually.

    In it's current state, whenever the same move is used at the same time, both projectiles fly in the same direction, since both images are picked both Not Mirrored and Mirrored will always be true at the same time.

    • If I use a For Each loop, the events break because I have a couple of triggers for while certain animations are supposed to be playing.
    • If I use Pick by UID, it picks only one of the fighters, and the other one does not even spawn the projectile.
    • If I use Pick a Random Instance, the triggers will trigger multiple times because some frames last more than one tick.
    • If I use Pick by Comparison, the same thing happens as it currently does.

    For whatever reason, I am unable to get the events to pick these objects individually. I have tried just about every way I can think pick these objects and I am very frustrated. Can someone please help me figure this out?

    Thanks in advance.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • So, the main object is Fighter_Control, it's in a container with a Dictionary. And you also have Fighter_Images family, which should be linked with each Fighter_Control. Is this correct?

    Is the Control_AI variable unique for each Fighter_Control instance? If it is, then it should be enough to associate Fighter_Images instances with their corresponding Fighter_Control, and to pick them in any events.

    But from your screenshot it looks like Control_AI is not unique, for example can it be -1 for both Fighter_Control?

    If Control_AI is not unique, or if its value can change during the game, then you should add another instance variable Fighter_Control_UID on the Fighter_Images family, and use it to link images with the control instance.

  • So, the main object is Fighter_Control, it's in a container with a Dictionary. And you also have Fighter_Images family, which should be linked with each Fighter_Control. Is this correct?

    Is the Control_AI variable unique for each Fighter_Control instance? If it is, then it should be enough to associate Fighter_Images instances with their corresponding Fighter_Control, and to pick them in any events.

    But from your screenshot it looks like Control_AI is not unique, for example can it be -1 for both Fighter_Control?

    If Control_AI is not unique, or if its value can change during the game, then you should add another instance variable Fighter_Control_UID on the Fighter_Images family, and use it to link images with the control instance.

    Yes that is correct, each Control_AI is unique to each Fighter_Control and ranges from 0-4. Usually it is enough, but for whatever reason for this set of events whenever I use Control_AI to link the Fighter_Control and Fighter_Image, the PickedCount always seems to indicate that every instance is being picked (I usually Pick by Comparison, Fighter_Control.Control_AI - Fighter_Image.Control_AI).

    I did try that, using the UID and as indicated by my first post doing that causes it to pick only one of the Fighter_Control/Fighter_Images (usually the one with the lowest Control_AI) and only applies the events to that link.

  • It's hard to tell from the screenshot what are you trying to do there. If these events are located at the top level (not nested under a function or something) and executed on every tick, then the problem must be with the condition where you pick dictionary instance - D_Fighter Key "Animation_Name"=Fighter_Control.Control_ID.

    If you don't pick the correct Fighter_Control instance first, then this condition will use Control_ID value of the first instance. And will probably pick only the first dictionary instance also. So if you need to process all Fighter_Controls in this event, you should add "For each" at the top.

  • It's hard to tell from the screenshot what are you trying to do there. If these events are located at the top level (not nested under a function or something) and executed on every tick, then the problem must be with the condition where you pick dictionary instance - D_Fighter Key "Animation_Name"=Fighter_Control.Control_ID.

    If you don't pick the correct Fighter_Control instance first, then this condition will use Control_ID value of the first instance. And will probably pick only the first dictionary instance also. So if you need to process all Fighter_Controls in this event, you should add "For each" at the top.

    I've uploaded my CAPX, if that helps. [ https://drive.google.com/open?id=1YBoR9H8Qt4lTANerj-xSNh080ign-wNk ]

    Normally, I would use a For Each loop but there are more events I need to use for this where a trigger is involved, which don't work under a For Each loop.

  • Whoa, that's a very cool game!

    I didn't notice you have two very similar looking variables Control_ID and Control_AI.. But I still think the mistake is in the first condition - D_Fighter_Ruleset Key "Animation_Name"=Fighter_Control.Control_ID

    When you have two Fighter_Control objects with different Control_ID values, this condition will pick only one D_Fighter_Ruleset instance, belonging to the first instance of Fighter_Control. If you need to check the dictionary for both fighters, you need to add For Each above the condition.

    .

    The rest of the event looks ok to me, although I would strongly recommend removing Trigger Once from sub-events. Trigger Once doesn't work "per instance", so in case of multiple instances it will almost certainly cause bugs and problems.

  • Whoa, that's a very cool game!

    I didn't notice you have two very similar looking variables Control_ID and Control_AI.. But I still think the mistake is in the first condition - D_Fighter_Ruleset Key "Animation_Name"=Fighter_Control.Control_ID

    When you have two Fighter_Control objects with different Control_ID values, this condition will pick only one D_Fighter_Ruleset instance, belonging to the first instance of Fighter_Control. If you need to check the dictionary for both fighters, you need to add For Each above the condition.

    The rest of the event looks ok to me, although I would strongly recommend removing Trigger Once from sub-events. Trigger Once doesn't work "per instance", so in case of multiple instances it will almost certainly cause bugs and problems.

    Thank you!

    That makes sense, although what could I use as an alternative? If I wanted to run something only once when the animation starts, or only once when a frame starts what would I use instead of trigger once?

  • That makes sense, although what could I use as an alternative? If I wanted to run something only once when the animation starts, or only once when a frame starts what would I use instead of trigger once?

    You start animations in some events, use the same events to run your code, or call a function from there. For frames - you can use "On frame changed" event, it's triggered only once for each frame.

  • You start animations in some other events, you can run your code there, or call a function. For frame changes use "On frame changed" event, it's triggered only once for every frame per each object instance.

    I also use this little trick, where I define an image point on one frame in a long animation, to mark it as a "key" frame:

  • You start animations in some other events, you can run your code there, or call a function. For frame changes use "On frame changed" event, it's triggered only once for every frame per each object instance.

    I also use this little trick, where I define an image point on one frame in a long animation, to mark it as a "key" frame:

    Thanks for helping me out. I've changed things around and but this into practice, but I'm still having a few issues.

    Normally this event would be nested under the animation playing events, but this is the new version. However, it's become apparent that for some reason when this triggers, only the Fighter_Control created first is picked as the one colliding-- even if it is completely not in motion (Family_Control being just a family with only Fighter_Control). I am... not quite sure how to fix this?

    Additionally, I want to go over part of what I did to fix it just for future reference.

    Essentially I have it set up so that the dictionary stores the previous animation used by changing the key to the current animation name at the end of the first loop; so if the key is not equal to the current animation name then that means this is the first loop of that animation. I use this to reset a bunch of keys back to false, ones that are named to be triggers for this animation and fighter specifically. I have all of the events I need to trigger once only trigger when its false, and immediately set it back to true.

  • only the Fighter_Control created first is picked as the one colliding-- even if it is completely not in motion

    It doesn't matter which sprite is in motion. Collision event between two instances Obj and ObjFamily only triggers once.

    If you need to process pairs of fighters, to decide which one makes a punch and which one gets hit, update animations etc., use this code:

    The function will be triggered two times, for Fighter1+Fighter2, and Fighter2+Fighter1.

    .

    If you want to process each instance separately, don't use a family. "Obj on collision with Obj" will pick both instances, so inside this event you can do "For Each Obj" and it will loop two times.

  • If you need to process pairs of fighters, to decide which one makes a punch and which one gets hit, update animations etc., use this code:

    Oh! That is really smart! Thank you for the help.

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