oTree Forum >

set player fields in oTree studio

#1 by Lior

Hi,
I want to use set fields for player in oTree studio.
e.g.:
player.color = C.COLORS[player.id_in_group-1]
player.type = (player.id_in_group + 1) % 2

But oTree studio doesn't allow me to enter formulas as initial values.
How can I solve this?
(In the meanwhile, I call a function that sets those values in the begining of every page but it doesn't feel right)


thanks,
Lior

#2 by Lior

I figured it out - using ROLEs.
"
If each group has multiple roles, such as buyer/seller, principal/agent, etc., you can define them in constants. Make their names end with _ROLE:

class C(BaseConstants):
    ...

    PRINCIPAL_ROLE = 'Principal'
    AGENT_ROLE = 'Agent
oTree will then automatically assign each role to a different player (sequentially according to id_in_group). You can use this to show each role different content, e.g.:

class AgentPage(Page):

    @staticmethod
    def is_displayed(player):
        return player.role == C.AGENT_ROLE
In a template:

You are the {{ player.role }}.
You can also use group.get_player_by_role(), which is similar to get_player_by_id():

def set_payoffs(group):
    principal = group.get_player_by_role(C.PRINCIPAL_ROLE)
    agent = group.get_player_by_role(C.AGENT_ROLE)
    # ...
If you want to switch players’ roles, you should rearrange the groups, using group.set_players(), subsession.group_randomly(), etc.

"
set roles in oTree studio:
https://otree.readthedocs.io/en/latest/multiplayer/groups.html#roles:~:text=group%20and%20subsession.-,Roles,-If%20each%20group

More info about roles:
https://otreecb.netlify.app/rolesandmatching/roles#:~:text=store%20the%20roles-,Roles,-%C2%B6

"
Another option is to manage heterogeneous groups is to use the self-defined built-in function role().

The role() method should be defined under Player classs in models.py and the method should return a string indicating the role of the player. For instance:

def role(self):
    if self.id_in_group == 1:
        return 'trustor'
    if self.id_in_group == 2:
        return 'trustee'
        
Then you can get the first player with the certain role using get_player_by_role() function. For instance:

trustee = get_player_by_role('trustee')
"

Write a reply

Set forum username