How do I access numbers stored in array?

  • During the past several days, I have pursued multiple strategies for implementing a collection scheme. My current attempt involves the use of two arrays. An inventory array keeps track of item names, item costs, and item weights. I use separate animation frames inside a sprite named 'objTreasure' for different types of treasure. Upon collision with an item, C3 destroys the item and records the frame number in a collection array.

    When I get to the final level, I'm trying to loop through the array and spawn the collected objects on the final page. Debugger shows me that the array is being populated, but I cannot -- for the life of me -- get anything to spawn on the final level. I added a loop counter as a global variable and confirmed that the loop is actually running.

    I would be grateful for any suggestions! The link is:

    transmedia.trinity.edu/~adelwich/temp/array-inventory-animation-frames.c3p

    Thanks!

  • Hiya, putting a loop inside a function isn't a good idea as it just calls it once. I've made an example here using a variable which works, take a look.

    dropbox.com/s/7pe3c9vakbxh875/array-inventory-animation-frames_edited.c3p

    notes: borrow the logic but do not use the file going forward as I deleted some layers to allow editing, note that I used 'array.width' which is the number of X values in the array and I used 'loopindex' which is the current number of loops

  • Thanks!!

  • Yep, no problem. ^^

  • Looping though the array with loopindex is exactly what I was trying to do. Awesome!

    Just to clarify, you said that placing loops in functions is a bad idea because the loop will only be called once. I didn't realize this. Are there any other limitations on functions that I should be aware of?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If you need to toggle something on/off use a variable, which is what you needed here. Use a function if you want to perform an action where you're going to be using it a bunch of times, passing variables through it as parameters and also may want to return some values at the end, such as if you wanted to attack an enemy with some damage you could run a damage function once to pass through some player stats, run some calculations in the function that result in an amount of damage returned at the end. There aren't really any limitations, just call it when you want to run something once but often and don't nest loops inside it. They are good for making your code more efficient and also for being able to track what is going on if you encounter bugs.

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