playMaker

Author Topic: Enum Creator wizard for non coders  (Read 699 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12342
  • Official Playmaker Support
    • View Profile
Enum Creator wizard for non coders
« on: January 12, 2017, 01:39:46 AM »
Hi Everyone,

 ok, new wizard for the non coders amongst us :) you will now be able to enjoy the power of Enums and FsmEnums with your own sets, without coding.

If you don't know what enums are, it's a very important part of solid and reliable coding strategy, it simply defines some hardcoded references, for example the days in a week:

Code: [Select]
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };

and then within your code, you are safe and can't mistype or misuse week days.

PlayMaker introduced with 1.8 the ability to work with enums and create FsmVariables of type enum (FsmEnum). You can pick any available enums withint he projects, but until then if you wanted your own enum, you had to write some c# code. With this wizard you won't have to anymore.

Get the wizard on the Ecosystem



open the wizard from the PlayMaker Menu /Addons/tools/Enum Creator Wizard


and off you go,

--you can add/reorder entries, select a namespace, a name and a folder to save the enum
-- validation helps pointing out where things are not correct
-- you have a list of all your created enums at the top, you can edit them
-- full preview of the generated code



and then, you can create an FsmEnum within PlayMaker and start using it with the related actions for enum




I tried to make it as simple and obvious as possible. But of course if you have questions, don't hesitate to ask!!

Bye,

 Jean

krmko

  • Sr. Member
  • ****
  • Posts: 422
    • View Profile
    • Fat Pug Studio
Re: Enum Creator wizard for non coders
« Reply #1 on: January 26, 2017, 02:01:04 PM »
Hurray!!! I was waiting for this :)

Thore

  • Playmaker Newbie
  • *
  • Posts: 47
    • View Profile
Re: Enum Creator wizard for non coders
« Reply #2 on: September 24, 2017, 05:40:58 PM »
Wanted to add a bit of what I found out, and which might be useful to other coding newbies.

The Path
The enums, like other definitions are stored away in a specific path, which is visible when you create the enum in the FSM, and when you set the Enum Type (which is "Days" in the example above). You can sort this neatly with a namespace. This is simply a bracket around everything, like this:

Code: [Select]
namespace Gameplay.Enum {
         >enum days stuff here<
}

Gameplay.Enum and then the Enum variable is pretty much like a path like Gameplay/Enum/Days. You can call these any way you want, and create the path the way you want. This example will produce Gameplay > Enum > Days (nested twice) in Playmaker. Try this somewhere to get an idea.

It makes sense to do this, when you have several enums, and to keep a bit of order. You can also group your other scripts that way. Just put the namespace bracket around everything and it's sorted into the menu entry. However, you then also need to reference the path correctly. So when do this to your own NPCdata script, you can access it with Gameplay.NPCdata (Gameplay can be any name you want). Or Gameplay.Enum.Days etc.

You can also put it into a class. Classes have the same path structure, but don't produce a nested menu entry. Say, you have namespace Gameplay, then a class Enum, and then your stuff, you get again the menu Gameplay (as before), but this time, the specific enum looks like this "Enum+Days". So if you see strange errors, try to figure out the path, and it might fix it.

Enum in Data Script
See MdotStrange's tutorial on simple data holder or interface scripts (there are a couple of good ones).

I found it confusing first and had lots of erros when I tried to add enums to my custom data holder scripts. But it is actually simple: Once, the enum is defined, you make use of it by treating it like a custom variable type. Let's say you use the namespace snipped as I shown above (and no class) then all you need to do is place this in your variable list.

Code: [Select]
public Gameplay.Enum.Days currentDay;
I know, it looks weird for non-coders at first. But this is really the same principle as public int myNumber. IN other words, Gameplay.Enum.Days is like your own variable type, and currentDay is the variable name in the current context. Once you place this in your script, you get a drop-down list with the enums in the editor. To get or set this to the Action, you simple refer to the variable like this public FsmEnum currentDay; i.e. like all the other variables.

In other words. Somewhere (same file or another), you define the enum type(s). And once they exist in the Unity context, you can use them like your own variable types.

Changing Enums
Behind the scenes enums are integers (int), where in Jean's example Sunday is 0, Monday = 1 and so on. The names are simply counted. When you delete Wednesday (which was at the 4th position, with zero), then Thursday slips down from 4 to 3 internally and all your references are screwed up.

Code: [Select]
0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday

when deleting Wednesday, becomes...

Code: [Select]
0 = Sunday
1 = Monday
2 = Tuesday
3 = Thursday
4 = Friday

Now 3 refers to Thursday and everything that referenced Wednesday now point at Thursday, everything that pointed at Thursday now points at Friday etc.

It's thus better to not remove or add within the list (only to the end), merely replace. You can also manually assign the ints to enums, by simply writing Sunday = 0, Monday = 1, etc. When you then delete or rearrange the list, the numbers stay the same, and are not reassigned.

Enums as Flags
Last thing. Enums can also be used as flags, which allows to assign multiple categories (instead of just one drop down list), say your character has multiple attack types, not just one. The keywords to search for is bitwise and enum flags. Here's an article for this. There's are cheap assets that provide basic scripts on the Unity Store for this.

Hope this is useful for someone ;)
« Last Edit: September 25, 2017, 07:05:34 AM by Thore »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12342
  • Official Playmaker Support
    • View Profile
Re: Enum Creator wizard for non coders
« Reply #3 on: September 26, 2017, 12:50:59 AM »
Hi,

 Cool write up thanks for the effort :)

 yes, The next update of the enum creatore wizard will include proper numbering to avoid the reordering issues.

 For Enum Flags. I'll be waiting for PlayMaker to support enum flags because otherwise it's pointless since you won't benefit from the flag selections because the User interface won't allow for this.

 Bye,

 Jean

Gustav

  • Playmaker Newbie
  • *
  • Posts: 30
    • View Profile
    • Pyrit Design
Re: Enum Creator wizard for non coders
« Reply #4 on: October 17, 2017, 08:03:17 AM »
Great, I found it! Thank you for this wizard. :)

Maybe you want to change the broken link on this page to this topic.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12342
  • Official Playmaker Support
    • View Profile
Re: Enum Creator wizard for non coders
« Reply #5 on: October 18, 2017, 03:48:58 AM »
Hi,

Good point, thanks for spotting this, done :)

 Bye,

 Jean