{"action":"created","issue":{"url":"https://api.github.com/repos/Source-Python-Dev-Team/Source.Python/issues/131","repository_url":"https://api.github.com/repos/Source-Python-Dev-Team/Source.Python","labels_url":"https://api.github.com/repos/Source-Python-Dev-Team/Source.Python/issues/131/labels{/name}","comments_url":"https://api.github.com/repos/Source-Python-Dev-Team/Source.Python/issues/131/comments","events_url":"https://api.github.com/repos/Source-Python-Dev-Team/Source.Python/issues/131/events","html_url":"https://github.com/Source-Python-Dev-Team/Source.Python/issues/131","id":160583949,"number":131,"title":"Pass the whole player instance to a menu instead of only his index","user":{"login":"Mahi","id":11020883,"avatar_url":"https://avatars.githubusercontent.com/u/11020883?v=3","gravatar_id":"","url":"https://api.github.com/users/Mahi","html_url":"https://github.com/Mahi","followers_url":"https://api.github.com/users/Mahi/followers","following_url":"https://api.github.com/users/Mahi/following{/other_user}","gists_url":"https://api.github.com/users/Mahi/gists{/gist_id}","starred_url":"https://api.github.com/users/Mahi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Mahi/subscriptions","organizations_url":"https://api.github.com/users/Mahi/orgs","repos_url":"https://api.github.com/users/Mahi/repos","events_url":"https://api.github.com/users/Mahi/events{/privacy}","received_events_url":"https://api.github.com/users/Mahi/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Source-Python-Dev-Team/Source.Python/labels/feature%20request","name":"feature request","color":"bfe5bf"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":12,"created_at":"2016-06-16T06:02:50Z","updated_at":"2016-06-18T07:10:22Z","closed_at":null,"body":"## Suggestion\r\n\r\nInstead of menu instances receiving the player's `index` like they currently do when you use `send()`:\r\n\r\n    my_menu.send(player.index)\r\n\r\nI suggest it should receive the whole player and internally handle the sending with `player.index`:\r\n\r\n    my_menu.send(player)\r\n\r\n## Reasoning/Current Issue\r\n\r\nIf you subclass `players.entity.Player` and add an attribute to its instances inside of  the `__init__()`:\r\n\r\n    class MyPlayer(players.entity.Player):\r\n        def __init__(self, *args, gold=0, **kwargs):\r\n            super().__init__(*args, **kwargs)\r\n            self.gold = gold\r\n\r\nYou can no longer use this class like you could with SP's `Player`, where you simply create the instance with `player = Player(index)` every time you need to access it (not that you should do that with SP's `Player` either though). The reason it **can't** be used in the subclass is due to `gold` being reset for the new instance, instead of remembering the old one's value:\r\n\r\n    >>> my_player = MyPlayer(my_index, gold=500)\r\n    >>> my_player_again = MyPlayer(my_index)\r\n    >>> print(my_player.gold, my_player_again.gold)\r\n    500 0\r\n\r\nThis means that I can not create the player instance in the menu's `build_callback` nor `select_callback` if my menu needs to access the `gold` attribute.\r\n\r\n## Current workarounds\r\n\r\nThere are a few workarounds for this, none of which I believe to be the \"correct\" way:\r\n\r\n1. Cache the custom `Player` class's attributes into an external dictionary to allow the functionality of creating a new instance every time you need to use it. This makes very little sense, and instead I believe `PlayerDictionary` should be used to store the instances for later use.\r\n2. Have a separate file for project-wide global variables, and add the `PlayerDictionary` instance there so it can be accessed from anywhere. Not really a good solution either, global variables usually lead to nothing good, and I would prefer not to have to create a separate file with just one line *just* because menus don't support players.\r\n3. Have all of the menus defined in the main file where your global `PlayerDictionary` is also contained. Just no.\r\n\r\nNone of these really appeal to me, and they break many good coding rules and conventions. This leaves me with one last option: create a custom menu class/subclass the existing one. But if this is the \"correct\" solution, why not just make the custom menu class the default one for SP?\r\n\r\n## Issues with the suggested functionality\r\n\r\nThis will greatly harm those who *don't* need to store their players, or don't even need to access the player's entity inside their menus. They would have to create a player entity where they don't really need it."},"comment":{"url":"https://api.github.com/repos/Source-Python-Dev-Team/Source.Python/issues/comments/226926593","html_url":"https://github.com/Source-Python-Dev-Team/Source.Python/issues/131#issuecomment-226926593","issue_url":"https://api.github.com/repos/Source-Python-Dev-Team/Source.Python/issues/131","id":226926593,"user":{"login":"Ayuto","id":5445604,"avatar_url":"https://avatars.githubusercontent.com/u/5445604?v=3","gravatar_id":"","url":"https://api.github.com/users/Ayuto","html_url":"https://github.com/Ayuto","followers_url":"https://api.github.com/users/Ayuto/followers","following_url":"https://api.github.com/users/Ayuto/following{/other_user}","gists_url":"https://api.github.com/users/Ayuto/gists{/gist_id}","starred_url":"https://api.github.com/users/Ayuto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Ayuto/subscriptions","organizations_url":"https://api.github.com/users/Ayuto/orgs","repos_url":"https://api.github.com/users/Ayuto/repos","events_url":"https://api.github.com/users/Ayuto/events{/privacy}","received_events_url":"https://api.github.com/users/Ayuto/received_events","type":"User","site_admin":false},"created_at":"2016-06-18T07:10:22Z","updated_at":"2016-06-18T07:10:22Z","body":"There you go!\r\n```\r\nclass MyPagedMenu(PagedMenu):\r\n    player_cache = PlayerDictionary(YourPlayerClass)\r\n\r\n    def __init__(self, **wkargs):\r\n        super().__init__(\r\n            select_callback=self.select_callback,\r\n            build_callback=self.build_callback,\r\n            **kwargs)\r\n            \r\n    @staticmethod\r\n    def select_callback(self, index, option):\r\n        return self.on_select(self.player_cache[index], option)\r\n    \r\n    @staticmethod    \r\n    def build_callback(self, index):\r\n        self.on_build(self.player_cache[index])\r\n        \r\n    def on_select(self, player, option):\r\n        # Overwrite this method\r\n        pass\r\n        \r\n    def on_build(self, player):\r\n        # Overwrite this method\r\n        pass\r\n```"}}
{"actor":{"display_login":"Ayuto"}}