How to break up code to effect multiple instances uniquely

0 favourites
  • 5 posts
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • So, I'm trying to make a turn based, grid-based puzzle shooter thing. My issue is that I finally worked out the AI for the enemy slimes, which is that they determine if the player is closer up and down, or left and right, then moves one tile(32 pixles) in the cardinal direction the player is further from. The problem with this is that one slime moves correctly, but when you have more then one slime, they all move in the same direction. it appears that the slime with the lowest id determines the movement of all the others. I really don't want to copy and modify the code for every instance of the slime, as I plan to have many different kinds of slimes running the same basic movement. How would I break this out so the code runs for each slime individually?

    I am super new to construct 2, so it could well be something basic.

    Thanks!

    -Aidan

    imgbb.com image hosting services free

  • you need to do "picking".

    when you have multiple of the same object you need to filter it down to the exact one you want.

    The question is how do you figure out which one you want. You can easily do System> Pick nearest/furthest and that should do it. But I don't know your game design so it's hard for me to say.

    what I usually do if I have a static number of the same object - all in the layout before runtime - I assign the object an instance variable called "which". And then I assign each object a 'which' value. Like for buttons I might assign "info", "exit" and "start" as which values. Then when the mouse clicks on MenuButton (I have 3 of them in my layout) I make a sub-event that says

    IF MenuButton.which = "info" THEN xyz...

    IF MenuButton.which = "start" THEN xyz...

    IF MenuButton.which = "exit" THEN xyz...

    but that all depends if you create your objects through events.

  • Since you are planning to have more types of slimes, there are two ways to do this:

    1. add them as different animations to the same "slime" sprite,

    2. or add new sprites (slime2, slime3 etc.) and combine them into "Slimes" family.

    One sprite with multiple animations may be easier, but if you want them to be different objects, go with the family.

    Use instance variables, I believe variables xMove, yMove, xDirection, yDirection, way should be instance variables, as they will be unique for each slime instance.

    If you decide to use the family, you should do these changes while the project is still small (this will save you a lot of time in the future):

    Move all instance variables and behaviors from the slime1 sprite to the family level.

    Change all your events to work with the Slimes family.

    So your function could look something like this:

    On Function "Compare"

    For each Slimes

    ...Set Slimes.xMove ...

    .....

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Is there no way to tell the algorithm to apply it to only one object? for instance, in the below image, I have the slimes set to destroy themselves and the bullet that hits it. When the objects hit each other, they only delete the pair that connect. all the other slimes and all the other bullets stay unaffected. I want to do this for the slimes.

  • It's called picking, and it's the most important concept in Construct programming.

    Use events to pick instances of slime1 object.

    In your example "On collision" event picks 1 instance of slime and 1 instance of bullet. Inside this event you can only control these instances.

    There are lots of events that can pick instances, say "Slime1 is visible" event will pick all visible instances.

    "System -> For each slime1" event will loop through all slime1 instances, picking one instance at a time.

    If you don't pick any instances in the condition (left) part of the event, than everything you do in the action (right) part will affect all instances!

    And by the way, read my comment above about families. You should add all your bullets to a family, and remove duplicate "On collision" events.

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