Spawn object location based on array index

0 favourites
  • 8 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • I want to be able to spawn objects in locations based on their array index number. I am trying to spawn them in a corresponding image point on a sprite. When I click the button to run the UpdateBagView function it creates a bunch of objects at the sprite origin (number 0) and not on the correct image point. It an item has say array index 2 I want it to spawn on image point 2.

    Here is a capx

    1drv.ms/1Tg3YSU

    Drag objects onto the bag to add to array.

    Right click an object to delete from array.

    This capx has a number of problems I could use help figuring out please.

  • You can't spawn Family objects - you get a random object. You need to decode the name and spawn that item. Use Array.CurX+1 instead of IndexOf in event 4. Also, get rid of the Foreach in event 2 - you have that already in event 4.

  • Thanks for the help. Your suggestions definitely improved it a lot. I now have a situation where adding the second item to the bag spawns a bunch of stuff even though it is a function that I expected to limit it to one. Would appreciate you taking a look at the latest version when you get a min please.

    Capx:

    1drv.ms/1Tg3YSU

  • You are doing it again. You call Foreach at event 11 and again at event 4, so every time you add and item, you call UpdatebagView, and then again for every item spawn an item.

    Are you going to need to read the array from storage at some point and fill the bag again, or are you only filling the bag as each item is dragged in. This will make a big difference to the proper answer.

  • What I want to get to is a bag that I can drop items into that updates immediately, and if an item is dragged out of the bag the slot is emptied. I want the next item to go into the empty slot. I guess like a real bag would act.

    I understand the spawn and respawn problem but I am struggling to deal with it. In fact, event 10 has a second condition that was my attempt at limiting the function running again if the item had already had its local variable InBag set.

    Thanks again.

  • Wow, you nailed it. I'm still trying to get my head around your code. Could you explain exactly what events 19-21 do please? Your comment helps but I'm lost.

    BTW, If I wanted to run this code on a separate layout and fall back to a previous layout during game play, are there any gotchas I should consider? My game has the player visiting a closet (calls another layout) where the items that will go in the bag actually reside and exiting the closet takes the player back to the previous layout.

    Thanks again for all the great help BTW.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 19a: check that the index you are removing from is not the last one

    19b: loop from the NEXT index to the last index

    20: pick only "InBag" stuff (this could technically be moved up, but that's a minor optimization)

    21: pick the Stuff with the name at the current index we care about

    -> move the single Stuff

    This shows how to get the whole bag filled again (I am not dealing with the duplicate items. I'm just showing how to get the items to show in the bag, when the array already has data in it.)

    http://www.blackhornettechnologies.com/ ... fresh.capx

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