playMaker

Author Topic: Get the Name of a Variable instead of it's Value [SOLVED]  (Read 1925 times)

MajorIdea

  • Full Member
  • ***
  • Posts: 131
Get the Name of a Variable instead of it's Value [SOLVED]
« on: April 22, 2017, 02:54:30 PM »
I'm trying to implement some sort of basic decision making to my AIs.

Imagine three possible actions:

Wander
Eat
Sleep

I'm using sample curves to change the priority of each - so if hunger is high I want to send the "Eat" event.

I can use either Arraylists or GetFloatMax (simpler) to get the highest priority value. The problem is that it always returns the value itself, not the value name.

So I get something like:
----
Wander = 0.6
Eat = 0.8
Sleep = 0.2

Result = 0.8
----

What I need is a way to say: Result = Eat

I could then use a Send Event by Name.

Is there a way of achieving this? Even if I use the Array Compare (to find which array item equals 0.8 ) I can only do it two at a time which can get unmanageable quickly.
« Last Edit: April 24, 2017, 07:12:10 AM by MajorIdea »

terri

  • Sr. Member
  • ****
  • Posts: 386
    • terrivellmann.tumblr.com
Re: Get the Name of a Variable instead of it's Value
« Reply #1 on: April 23, 2017, 06:26:18 AM »
Made a quick edit for you

MajorIdea

  • Full Member
  • ***
  • Posts: 131
Re: Get the Name of a Variable instead of it's Value
« Reply #2 on: April 23, 2017, 07:50:34 AM »
You sir, are a gentleman and a scholar!

Thank you, that's exactly what was needed! I happened to use the GetFloatMin instead just because it makes more sense to classify decisions based on cost rather than priority but I just copied your changes to your other action. I hope it's alright for me to leave it here.

Thanks again <3

terri

  • Sr. Member
  • ****
  • Posts: 386
    • terrivellmann.tumblr.com
Re: Get the Name of a Variable instead of it's Value
« Reply #3 on: April 23, 2017, 11:37:24 AM »
great! happy to help

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get the Name of a Variable instead of it's Value
« Reply #4 on: April 24, 2017, 02:06:18 AM »
Hi,

 While this is perfectly fine to do, I strongly advice against this kind of design pattern, you are mixing two different context and it's dangerous and prone to massive headache and tricky bugs down the road.

 what you want here is at the very least a hashtable ( using ArrayMaker) where you reference values against keys.

 Your key is an totally part of your data. Trying to give you an example outside programming, it's like or family name which used to represent us, maybe what we do, where we live, our character, something that defined us so that other could reference us in a discussion, that was a long, long time ago, now our language as evolved and reached such a level of complexity that we have separated reference ( our family name) and how we are defined in society. Same with our name and nickname, it roots into something that defines us ( or we believe it does... depends your beliefs), but it really creates problem down the road because it's too restrictive...

It's like you would go to your local administration and wanted to chance your name to "PlayMaker Programmer", but is that enough to define you ( :) ) no, you are also maybe a father, definitely a son or a daughter, you are maybe a sportsman, someone that play Chess, have a political preference, a religion, what should be your name then if your name defines what you are or what you do?

enters variable naming:

Variable Name:  Variable Value
Name:               Jean
Family Name:     Fabre
Sex:                  Male
Job:                   The best in the world :)
Marital status:    Married
nationality:        French

yes, you should name your variable in plain english and in a meaningfull way, but remember, that when compiled or optimized, the name of the variable is compressed into a unique value because really it's a pointer, nothing else: typical example, javascript minification: https://en.wikipedia.org/wiki/Minification_(programming)

We are simply lucky that inside PlayMaker, variables name are also treated as variable, because it's a framework that works on top of c#.

but, I hear you say: but then how can I programmatically find your name, I need to get the variable labeled "Name". well, one way is to hardcoded this in your logic, but then if you need flexibility, you then switch to a hashtable ( using ArrayMaker on the Ecosystem)


Key:                     value
"Name":               Jean
"Family Name":     Fabre
"Sex":                  Male
"Job":                  The best in the world :)
"Marital status":    Married
"nationality":        French

and the keys become something you can query, get a list of as strings, and get the value associated with that key. The Key become part of your data.

If you are ok with programming, you can also use Enum, which is a special c# type of variable which lets you create a list of references, which can then be used in your logic without fearing messing up with a typo, because with the hashtable above, you could mistype "Nationality" where it's without cap "nationality" and so your logic would not find the key and fail to give you the value you expected.

PlayMaker has an FsmEnum, and I created a wizard that lets you create enums without coding :)

http://hutonggames.com/playmakerforum/index.php?topic=14158.0

To learn more about enums usage: check here for example

http://stackoverflow.com/questions/3519429/what-is-main-use-of-enumeration

Bye,

 Jean