playMaker

Author Topic: How to deactivate a group of objects using bool value  (Read 1855 times)

QueenM56

  • Junior Playmaker
  • **
  • Posts: 55
How to deactivate a group of objects using bool value
« on: May 29, 2018, 04:09:35 AM »
Hi,
I'm currently working on an in-game shop of 2D ios game.
What I want to archive is for the player to be able to select a character from the shop and play with it in the game scene.
I used 'set bool value' to determine which character is activated.
So, when one of the characters is touched/selected, the activation bool value of it will be set to true, at the same time the rest of the characters bool value will be set to false.
It works fine when I only have 3 characters to be selected. However, it will be too much trouble if I have say like 50 characters for the players to choose, that means I have to put 49 'set bool value to false' action in one touch.
I was wondering if there is any smarter way to make it work.
Can anyone help?

Thanks!
M

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: How to deactivate a group of objects using bool value
« Reply #1 on: May 29, 2018, 10:07:07 AM »
Hi!

When you select a character, there are only two changes that occur: the current character is deselected (bool set to false) whilst the new character's bool is set to true.
All the other characters' bools don't need to be flipped.

The way I'd solve this is by using a Hash Table, which requires the Array List / Hash Table package to be installed. It's clearer that way so you can associate a bool to each character in a way that's easy to read.
This way you'll have your keys (1st column) with the names of your characters, the second column set to bools.

First, you use the key of the current character stored in a FsmString and find it in the array, get its bool value and flip said value (bool switch) or set it to false.
Then you parse again the table by using the key of the character that is selected, flip its bool or set it to true.

You can also avoid using the Hash Table by directly using a FsmArray, but then you'll have to write the list of your characters on a piece of paper or something because the array (set to bools in the variable inspector) cannot store both the name of a character and its bool, it stores one type of data only*. Instead of the names, you'll use the index values (0, 1, 2, 3, etc.). From there you do something similar to above, you get a bool value at index x, flip ip and then get the bool at the index of the selected character and flip it too.

* you could use strings too in that array and either put a X or V in front of each character's name (separated by a blank space, like X Rodriguez, X Samantha, O Rashid, etc.). This would allow you to see the status and name of each character in that FsmArray, but you wouldn't be using bools; instead you'd have to parse the beginning of each string, more precisely the first character, to see if it's X or O (or any other convention of your choice). This would sort of emulate an array with two columns.

If bools are not so much necessary, you could also simply have a FsmArray set to strings so you'd write the names of each character in it. Then use a FsmString called 'selected_char' which you'd simply fill with the string value picked at the index that aligns with the character you select: it would automatically suppress the old character's name with the new one.

QueenM56

  • Junior Playmaker
  • **
  • Posts: 55
Re: How to deactivate a group of objects using bool value
« Reply #2 on: May 29, 2018, 11:35:33 PM »
Hi Broken Stylus,

Thank you so much for your detailed advise, you made things much clear to me.
Now I have used 'hash table get next' to do bool test and wish to use 'current bool value' (false) to deactivate the characters.
I've stored the picked bool value with a string. now the problem is, how can I connect the string to a game object, so I can deactivate it?

Thanks!
M

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: How to deactivate a group of objects using bool value
« Reply #3 on: September 29, 2018, 11:44:58 AM »
By "connecting" a string to a character, you meant associate it?

If you use a hash table, the strings (character names) are in the left column (use condensed view option); they're the "keys".
In the right column, you'd use booleans (the "values").

You can also use another hash table to associate names to game objects, if that's what you mean by characters.
In fact, create it first. It seems more logical to list your characters you want to (de)activate, then other properties in other tables.

In other words, you'd simply have a first hash table that handles the names (keys) and the characters (values as game objects: you'll have to add them one by one manually).
Then a second one of the same size, that uses the same keys, but the values are booleans.
For that second table, go to game object's Inspector window, right click "copy component" on the Hash table proxy component and then right-click "copy component as new", then change the type of variables to bools instead of game object.

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: How to deactivate a group of objects using bool value
« Reply #4 on: September 29, 2018, 05:04:19 PM »
To me this looks like a case for enums.

There's a Enum Creator Wizard in the menu Playmaker > Addons > Tools, but it's a good idea to understand a little how they work. Enums are basically a list of variables where one is true, and the others are considered false. Multiple choice is also possible, for which you need to google [EnumFlags] and [System.Flags].

By default, they are made exactly for this purpose: showing a dropdown list, where one thing is selected. Try this out: As usual, scripts work anywhere in asset folder. Make a new file, named "MyCharacters", and put this code into it. Or just download my example.

Code: [Select]
namespace MyNamepace.MyCharacters
{

    public enum MyLittleEnumList
    {
 
        Alice = 1,
        Bob = 2,
        Charly = 3
        Dwayne = 4,
        Erica = 5,
        Finn = 6,
        Gerald = 7,
        Harry = 8,
    }


} // END Namespace

This creates your new enum list. The numbers behind are optional, but you want this to make it a bit more robust. If you don't assign them, they are given at runtime, which means that when you later on delete Alice, then Bob becomes 1 and Harry becomes 7. A player who plays Erica will then play with Finn and so on. Also, you want to keep the numbers glued to the character, once things go live, but you can change the order around as you want.

The name of the file must match MyNamespace.<here>. The purpose of doing it like this is that it creates a submenu <MyNamespace> where you can keep all your enums in one place. If you never ever have more than one, and you swear to your chosen deity, you can simply leave the "MyNamespace." away. You generally want to use a namespace, because to find the enum often involves navigating through lists, and that makes them easy to find and keeps them all together.

You have now created a new enum list that has these character names as possible options. To wrap your head around this, think of this as new variable type. E.g. float is the type and "myFloat" is the individual variable that then contrains one float. Likewise, <MyLittleEnumList> is the type, and then you need, as usual create your actual variable in the FSM, e.g "activeCharacter".

Simply set it to Enum, and then click on the newly created variable, and in Enum Type, navigate the list to your namespace and et voila, there's <MyLittleEnumList>. You can then treat this as any other variable, set it to "Bob" or whatever.

If you end up having more enums, you can rename the file to "Enums.cs" and likewise the namespace line as e.g. "MyGame.Enums". You can then simply append any number of enum definitions as you like. If you want to reference them anyway, you access them as MyGame.Enum.MyLittleEnumList, i.e. Namespace.File.EnumlistInFile.

Hope that helps.
« Last Edit: September 29, 2018, 05:09:39 PM by Thore »

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: How to deactivate a group of objects using bool value
« Reply #5 on: October 03, 2018, 12:15:24 PM »
EDIT: found the downloadable tool... https://hutonggames.fogbugz.com/default.asp?W1244  (but the package seems to be full of components not related to the Enum Creator)

Okay, I only picked the EnumCreator, the Extensions and External Libraries (which contains the entire Rotors folder).
Works like a charm!
« Last Edit: October 03, 2018, 01:02:07 PM by Broken Stylus »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to deactivate a group of objects using bool value
« Reply #6 on: October 04, 2018, 03:41:42 AM »
Hi,

 you can keep the rest, it's part of the PlayMaker Utils library, chances are you'll be needing more from that library

 Bye,

 Jean

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: How to deactivate a group of objects using bool value
« Reply #7 on: October 10, 2018, 04:17:12 AM »
Well thus far it has worked flawlessly so... dunno. :D