playMaker

Author Topic: Arraymaker help  (Read 15539 times)

Splankton

  • Sr. Member
  • ****
  • Posts: 268
Re: Arraymaker help
« Reply #30 on: December 17, 2013, 02:50:57 PM »
Well, all I need at the moment is to hold 5 items.  So I'm thinking a hashtable maybe is too much.  So if I created another array to hold the names of the items using strings, would that be inside the loop, or create another loop just for the names?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Arraymaker help
« Reply #31 on: December 18, 2013, 05:22:14 AM »
Hi,

 hashtables make sense starting at 2 items :) having only five items is totally OK: I even use Hashtables for preferences and settings. It's always better than hardcoding these properties and values in the code or fsm themselves.

if using arrays, yes, use the same loop, because you must expect all arrays forming a table to be consistent, so same number of items, you delete an item only by its indexes and always on all arrays, you add items to all arrays as well, etc etc. And so you would then only use one loop to got through them.

Bye,

 Jean

Splankton

  • Sr. Member
  • ****
  • Posts: 268
Re: Arraymaker help
« Reply #32 on: December 18, 2013, 12:23:21 PM »
Hi,

 hashtables make sense starting at 2 items :) having only five items is totally OK: I even use Hashtables for preferences and settings. It's always better than hardcoding these properties and values in the code or fsm themselves.

if using arrays, yes, use the same loop, because you must expect all arrays forming a table to be consistent, so same number of items, you delete an item only by its indexes and always on all arrays, you add items to all arrays as well, etc etc. And so you would then only use one loop to got through them.

Bye,

 Jean

Thanks Jean, I had a look at using a hashtable, couldn't quite get it working because I dont fully understand which values I would need.  All my 5 items are the same, except I've named them item1 - item5.  I think I will have to take your expert advice though, because I've realised that because the player is taking out index 0 every time he picks an item, it shifts the other items up in the index, which then confuses my NPC and he goes for items that arent there.

I've even tried (for the player) removing the last index in the array on each pickup. This works because the index doesnt shift up, but again it confuses the NPC, because when say, item2 is picked, the item at the end of the index is removed which could be item4.

So I think hash tables are my only hope, but I dont know if I need to loop through one, or just set it up at the start, then loop through the array?
By the way, one more thing, if I set my global transition outside of the loop so the main loop isn't touched at all, it doesn't make any difference to my NPC picking the items. Items are removed, then added back in without a loop.  So do I need a FOR loop?  I only have one array, no temp ones and looking at the loop all I'm doing is getting the list and adding 1 to my count.  I dont know what else I need to do to each gameobject as it iterates?

Sorry for the trouble and the really long thread, but I'm so close!
thanks for any help.

mweyna

  • Full Member
  • ***
  • Posts: 242
Re: Arraymaker help
« Reply #33 on: December 18, 2013, 01:24:55 PM »
Why not just use 3 arrays? You have a Master Item List, an array on your player, and an array on your NPC. Upon start copy all items from the Master list to both Player and NPC. Then if you create a new item, have it add to all the respective lists on its own FSM. When a player interacts with it, have it remove from their respective array?

Splankton

  • Sr. Member
  • ****
  • Posts: 268
Re: Arraymaker help
« Reply #34 on: December 18, 2013, 02:37:58 PM »
Why not just use 3 arrays? You have a Master Item List, an array on your player, and an array on your NPC. Upon start copy all items from the Master list to both Player and NPC. Then if you create a new item, have it add to all the respective lists on its own FSM. When a player interacts with it, have it remove from their respective array?

Thanks mweyna, that makes sense. But when my player or NPC removes the item, should I remove from all 3 arrays so they have all the same array length?
Also, can you answer what I would iterate through. I understand you use the loop to get or change something to each count, but I dont know why I would need one?

mweyna

  • Full Member
  • ***
  • Posts: 242
Re: Arraymaker help
« Reply #35 on: December 18, 2013, 06:00:56 PM »
Just have the "OnPickup" action remove it from the individual array. As for the Master Array, depending on the scenario you could do it a couple ways depending on choice, but if I was crafting something like that, I'd ...

- Depending on the scene have the Master Array copy to both Player and enemy  arrays on load.
- On a new object creation, add to both player and enemy arrays.
- Have an FSM on the enemy/player that gets the object from an array and paths to it. Have a FAIL action set if the object cannot path to it.
- Upon pick up, remove the object from the individual array and de-spawn/delete the object as appropriate.
- Have the FAIL action on the locomotor remove the item from the Array.

I can see problems with this if you're doing a lot of items, but that kinda theory in some form.


Splankton

  • Sr. Member
  • ****
  • Posts: 268
Re: Arraymaker help
« Reply #36 on: December 19, 2013, 01:52:40 PM »
Just have the "OnPickup" action remove it from the individual array. As for the Master Array, depending on the scenario you could do it a couple ways depending on choice, but if I was crafting something like that, I'd ...

- Depending on the scene have the Master Array copy to both Player and enemy  arrays on load.
- On a new object creation, add to both player and enemy arrays.
- Have an FSM on the enemy/player that gets the object from an array and paths to it. Have a FAIL action set if the object cannot path to it.
- Upon pick up, remove the object from the individual array and de-spawn/delete the object as appropriate.
- Have the FAIL action on the locomotor remove the item from the Array.

I can see problems with this if you're doing a lot of items, but that kinda theory in some form.

Thanks again mweyna, I have set it up that way, I still cant get a reference of the item for the player though.  The enemy gets the index from GetClosestGameObject, so thats done, but its they same problem as before.  When my player picks up an item, I dont know how to reference to that item so I can remove it.

- Have an FSM on the enemy/player that gets the object from an array and paths to it. Have a FAIL action set if the object cannot path to it.

If I can do this for my player, I'm happy :) But I dont know how.
Can I ask for a little more help please ;)

mweyna

  • Full Member
  • ***
  • Posts: 242
Re: Arraymaker help
« Reply #37 on: December 19, 2013, 02:11:26 PM »
So in the GetClosestGameObject action there should be an output to a GameObject variable. Then in the Remove action, just feed that GameObject variable back in.

As for the second part, I believe I was assuming you were using A* pathfinding, so I apologize if you are not. I'm not too familiar with non A* pathfinders, so maybe someone else can speak to this.

Splankton

  • Sr. Member
  • ****
  • Posts: 268
Re: Arraymaker help
« Reply #38 on: December 19, 2013, 02:32:07 PM »
So in the GetClosestGameObject action there should be an output to a GameObject variable. Then in the Remove action, just feed that GameObject variable back in.

As for the second part, I believe I was assuming you were using A* pathfinding, so I apologize if you are not. I'm not too familiar with non A* pathfinders, so maybe someone else can speak to this.

Ah, pathfinding of course, yes I am using a navmesh so I know what you mean now lol.
But I have already done the ClosestGameObject action, I used the index from that and fed that into the Remove, so that works fine.  My problem is for my player I cant use ClosestGameObject for that, so I need another way of getting the item that has been picked up.
I know Jean mentioned using a hashtable to reference the items, but they are all the same, except the names i.e item1 - item5 so I dont know what to enter for the keys/values.
You know of a way I can get a reference to one of the items in the array for my player, so I can remove it and add the same one back in?

mweyna

  • Full Member
  • ***
  • Posts: 242
Re: Arraymaker help
« Reply #39 on: December 19, 2013, 03:02:06 PM »
I'm not understanding why you cant use the Closet Game Object variable for that? If you're using the GameObject to remove it, why not just quickly passed the ClosetGameObject to a new empty GO  - "Temporary_Game_Object" then use Get Position for that Vector 3?

Splankton

  • Sr. Member
  • ****
  • Posts: 268
Re: Arraymaker help
« Reply #40 on: December 22, 2013, 11:51:14 AM »
I'm not understanding why you cant use the Closet Game Object variable for that? If you're using the GameObject to remove it, why not just quickly passed the ClosetGameObject to a new empty GO  - "Temporary_Game_Object" then use Get Position for that Vector 3?

Yes, you are right, I used ClosestGameObject for my player, its not ideal but I finally have the array working.  I set it up like you suggested, a master array and 2 others arrays for the player and enemy, tweaked it a little - I remove the items from the master which triggers the 2 other arrays and updates.
Didn't take too long, my PC went down 2 days ago, had to format and reinstall :( but its all working now, so thanks for your help! I can relax and enjoy Christmas now :)