[erlang-questions] If you are homesick for object.selector

Loïc Hoguin <>
Thu Jan 24 16:18:36 CET 2013

On 01/24/2013 03:16 PM, David Goehrig wrote:
> On Jan 23, 2013, at 9:37 PM, Loïc Hoguin <> wrote:
>> Character.current_weapon.ability.points_req
>> Or
>> Character.inventory[0].name
> Putting my game programmer hat back on, given a choice I would prefer:
>    Character weapon ability cost
> Where each is an application of a method on the prior or in Erlangy speak:
>      cost(ability(weapon(Character)))
> Where in each function destructs and returns an element of the nested structure. I might for sake of argument want to further name them:
>    costOf(abilityOf(weaponOf(Character)))
> Now in a typical game, weaponOf vectors to the currently selected item from the inventory. AbilityOf vectors off of the currently selected special assigned to the B button / right click slot, and the cost is tied to the level of the character for scaling (if you assume a fixed sized mana pool).
> Usually each of these behaviors require implicit knowledge of other state, and would likely make extensive use of get/put  in the Character's process dictionary to vector accordingly.
> This would imply these structures are best managed in an entirely different manner, where the Character process should memoize the values in a process dictionary, and the interrogation should happen only once upon an equip(Weapon) method.
> Same issue goes with inventory. We probably need a weapons manual process that can be interrogated for details based on names.  These changes would make it more likely to be a bunch of message sends. To WeaponManual, Inventory, Character, etc.
> It would be difficult to justify the dot notation as it wouldn't even remotely resemble how one would structure the message sends.

Process dictionary? Messages? I don't understand.

Assume a variable Character. This variable contains everything about the 
character. How do you best access and modify Character? The answer must 
not involve the process dictionary, processes or message passing. Today 
I have to write each access and modification function. Or I can generate 
it, but either way I end up with hundreds of functions in many modules. 
Or I could use records, and have one line per sub-record per 
modification function I write. That's not *easy* nor *practical*. Easy 
and practical is:


for access, and:

   Character.weapon.ability.cost = 123

for modification.

Now I know this isn't the Erlang way, but I'm sure there can be a 
solution that allows to write something similar. Frames looked like a 
pretty good step in the right direction. Regardless of syntax, I'm sure 
we can have something like:

   Character2 = Character{weapon{ability{cost=123}}}

Where it would be checked against the proper type, evaluated at runtime, 
and return a copy of Character with the requested modifications. This 
can work for access, "modification", and pattern match. The user would 
be aware that it has runtime costs, but will likely still choose this 
because it allows him to actually get the work done. There's many 
projects that I wouldn't even dream to do because of how tedious it 
would be, as I want to get my project running and not battle endlessly 
to modify my data.

Loïc Hoguin
Erlang Cowboy
Nine Nines

More information about the erlang-questions mailing list