playMaker

Author Topic: Working with arrays!  (Read 2332 times)

chrislow19

  • Playmaker Newbie
  • *
  • Posts: 5
Working with arrays!
« on: March 30, 2020, 03:23:06 PM »
Hi guys, I'm pulling my hair out over this! Here's the situation - floating combat text that animates the damage variable above the enemy's head RPG style. I've got it working, it's on its own gameobject that is set to activate once the enemy registers a hit. However, it only works once, so I get a nice floating combat text on the first hit only.

I know I need multiples of this gameobject, and I need to be able to iterate through them for each instance of damage so one object activates, plays its animation, and deactivates again ready to be used once more.

The question is how? I've looked into videos explaining arrays, I understand the concept, but I have no clue how to put it into action. Help!  ;)

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Working with arrays!
« Reply #1 on: March 31, 2020, 08:47:01 AM »
Arrays are powerful but also super annoying. I just started to work with them more, and here's what I learned. The Resident Array Expert is DjayDino ;)

You can add to an array with Array Add. To get something from an array, you can use Array Get Next. To create this, you must make a small loop.

You need a central state with array next with two events, loop and done. You also need reset bool (you can of course name them however you want). Fill out the array next (you probably don't need to store the index). It will leave through loop where it grabs the next item from the array list, where you can then do your stuff on that item. Then finish the state back to the central array get next state, where it grabs the next item of the array and so on. Until it's done (when it goes to the done event).

Since you want to reset this after each loop, you place a Set Bool reset to false, in the state after loop (where you handle one individual item). And in the state after done, Set Bool reset to true. So next time, it will restart the loop fresh.

Another important and very pesky thing about arrays is that they can easily trip up when a particular index (one place in the array list) is empty. You can clean this out with array concatenate (from ecosystem). And if you want to renew the entire array, you also need to deliberately array clear and array resize it (to default). Otherwise, if you always add, the arrays becomes a giant list that is empty (arrays can contain "null" entries). If for some reason an array item disappears during the process, you might want to throw in a game object is null action in the state after loop, and somewhere on top, so it skips whatever you do there.
« Last Edit: March 31, 2020, 08:54:55 AM by Thore »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Working with arrays!
« Reply #2 on: March 31, 2020, 11:26:55 AM »
Hi.
This video can help explaining the basics :

@Thore
Hehe i am not an expert, but ye i am comfortable with arrays now.
Jean is the Expert on arrays, he is the one who created Array Maker :D

Quote
Since you want to reset this after each loop, you place a Set Bool reset to false, in the state after loop (where you handle one individual item). And in the state after done, Set Bool reset to true. So next time, it will restart the loop fresh.
You do not have to set to false.
If the reset is 'true' then the action will reset AND set the reset variable to 'false'.

also the 'Reset' is not needed if you know the loop never get broken.
So if you you always loop thru the whole list its not needed, but if you stop looping somewhere half way and then later on you want to loop from start again then you need to use the 'Reset' bool :)

chrislow19

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Working with arrays!
« Reply #3 on: March 31, 2020, 01:04:15 PM »
Thanks for the response guys. This stuff seems to be way over my head even with tutorials. Who knew that activating and deactivating game objects from a list would be so complicated?

With the array FSM created on a holder gameobject, how would I access the next array item with my on hit FSM (located on another game object)?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Working with arrays!
« Reply #4 on: March 31, 2020, 01:41:50 PM »
Hi,
its not really that complicated.

Array Get Next simply get a variable (for example an object)
Then you can do things with that object.
Then go back to the 'Array Get Next' state to get the next object and do again some things to the object.
And so on

If you want to work in multiple fsms/object with an array i would suggest using Array Maker
as it uses References.

Here's a video on Array Maker :


To get familiar make a test project or scene and play around with it (for both array and array maker)

chrislow19

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Working with arrays!
« Reply #5 on: March 31, 2020, 01:46:34 PM »
Thanks! I will definitely look at this. Much appreciated