A quick way to obtain game instead of Singletons.getModel().
Post MTG Forge Related Programming Questions Here
Moderators: timmermac, Blacksmith, KrazyTheFox, Agetian, friarsol, CCGHQ Admins
3 posts
• Page 1 of 1
A quick way to obtain game instead of Singletons.getModel().
by Max mtg » 28 Mar 2013, 17:44
There was such lines in a recent commit
Aside from the fact that two parts of condition on a first glance look a bit excessive, I just want to note that player instances now hold reference to a game object. That is the game that player is involved in.
So if you have a reference to player and have to recieve its game, you don't need to get the game from sinletons... just invoke player.getGame(). That is a bit shorter and removes reference to that singletons, incompatible with multiple games in one application (when we build the server).
Same is true when you need game given an abiliy or card - derive player from there and you know what to do next.
- Code: Select all
if (this.isOpponentTurn() && Singletons.getModel().getGame().getPhaseHandler().isPlayerTurn(activator)) {
return false;
Aside from the fact that two parts of condition on a first glance look a bit excessive, I just want to note that player instances now hold reference to a game object. That is the game that player is involved in.
So if you have a reference to player and have to recieve its game, you don't need to get the game from sinletons... just invoke player.getGame(). That is a bit shorter and removes reference to that singletons, incompatible with multiple games in one application (when we build the server).
Same is true when you need game given an abiliy or card - derive player from there and you know what to do next.
Single class for single responsibility.
- Max mtg
- Programmer
- Posts: 1997
- Joined: 02 Jul 2011, 14:26
- Has thanked: 173 times
- Been thanked: 334 times
Re: A quick way to obtain game instead of Singletons.getMode
by moomarc » 29 Mar 2013, 07:42
Thanks Max. Just to clarify, in the example you used there isn't actually a double check in the condition. 'IsOpponentTurn' is actually just the name of the restriction set if "OpponentTurn" is a restriction, not a reference to player or turn. So for this example I assume it ould now read
- Code: Select all
if (this.isOpponentTurn() && activator.getGame().getPhaseHandler().isPlayerTurn(activator)) { return false; }
-Marc
-
moomarc - Pixel Commander
- Posts: 2091
- Joined: 04 Jun 2010, 15:22
- Location: Johannesburg, South Africa
- Has thanked: 371 times
- Been thanked: 372 times
Re: A quick way to obtain game instead of Singletons.getMode
by Max mtg » 29 Mar 2013, 09:04
Marc, that sample code was from an older revision and it was changed already.
Probably from left side of that code - http://svn.slightlymagic.net/websvn/dif ... &peg=20636
I am writing that just to remind of easier ways of doing common things.
Probably from left side of that code - http://svn.slightlymagic.net/websvn/dif ... &peg=20636
I am writing that just to remind of easier ways of doing common things.
Single class for single responsibility.
- Max mtg
- Programmer
- Posts: 1997
- Joined: 02 Jul 2011, 14:26
- Has thanked: 173 times
- Been thanked: 334 times
3 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 33 guests