playMaker

Author Topic: How do I create collectable objects?  (Read 1809 times)

mertti

  • Playmaker Newbie
  • *
  • Posts: 10
How do I create collectable objects?
« on: June 09, 2020, 07:01:06 PM »
Hi,

I need collectable objects like coins to my game. The coin can be collected only once, so if player collects the coin and saves the game it should be disappeared next time when he returns to the game.

I got it to work with a single coin but when I duplicate coin objects all disappear when returning to the game.

I am new with the unity and playmake. Any suggestions how to get it work?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How do I create collectable objects?
« Reply #1 on: June 10, 2020, 01:38:19 AM »
Hi,

 you need some data management on top of your actual logic in game for collectables.

 each collectables should have a unique ID, and in your data ( player prefs most likely), you will save the current state of that ID, likely a simple flag saying it's been picked up.

 when you are about to instantiate a collectable with a given ID, you must first check against your data if you should show it or not, that's key when you load a level or restart a level.

Bye,

 Jean

mertti

  • Playmaker Newbie
  • *
  • Posts: 10
Re: How do I create collectable objects?
« Reply #2 on: June 10, 2020, 04:18:57 AM »
Hi,

How do I set up unique for each collectable ID?
How do I set up data management?

Could you please help me? I am new in this :)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: How do I create collectable objects?
« Reply #3 on: June 10, 2020, 06:10:22 AM »
Hi.
For each type you can have a ID

For example :
Bronze coins id : 1
Silver coins id : 2
Gold coins id : 3
Special Token id : 100
(these should be saved and be accessible)

on your collect-able, create a fsm and call it 'Data'

When player click to pick up, Get the 'id' variable then you can do pick up and add to a stack or something else depending on the item (for example a health pot could give health)

on the item it self (for example the 'Special Token') you can do a check if it was already picked up, and if so disable the item.

If you need only a few objects and a single type of data you can use some 'player prefs' to load/save that.

for more complex inventory (like weapons with stats on it for example) i suggest to use Xml (Data Maker) and Hash Tables (Array Maker)

to go more complex might be a bit overwhelming for a beginner tho.

mertti

  • Playmaker Newbie
  • *
  • Posts: 10
Re: How do I create collectable objects?
« Reply #4 on: June 10, 2020, 11:16:17 AM »
Hi,
How do I set up unique ID?
Could you please share a screenshot?


Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: How do I create collectable objects?
« Reply #5 on: June 10, 2020, 01:58:18 PM »
To assign each object a unique ID means it’s done at runtime. My hunch is that all coins are added to an array. Then, you could set up a “for each” loop with array get next. The coin prefab has an int variable (ID). In the array loop, you set the current index as the ID. Then each coin has a unique ID.

Another hacky trick is to get the coin name and extract the number from the game object name with some string or substring action. For a small game or for a beginner project to keep you moving perhaps good enough. But that should be only a temporary solution.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How do I create collectable objects?
« Reply #6 on: June 11, 2020, 02:43:54 AM »
Hi,

 You don't necessarly need to be at runtime to assign a Unique ID, it can be done when you design the level in editor, that's a preferrable way since you can position coins in your level as well.

don't overthink this, it's easy to get started. Let me work on a small sample to show you an easy way to do that to get you started.

bye,

 Jean






jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How do I create collectable objects?
« Reply #7 on: June 11, 2020, 07:38:56 AM »
Hi,

 here's a simple scene showing how it's done ( I'll put it on the ecosystem in a couple of days)




you have one prefab, that act as a generic collectable,

- you can set its level and ID for unique referencing across your project
- you give it a visual prefab that will be instantiated as a child of the collectable
- collectables can be consumed once or forever

all fsms are super simple, let me know if you hit trouble or don't understand some features.


Bye,

 Jean