It is currently 08 Sep 2025, 20:10
   
Text Size

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

A quick way to obtain game instead of Singletons.getModel().

Postby Max mtg » 28 Mar 2013, 17:44

There was such lines in a recent commit

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

Postby 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
User avatar
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

Postby 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.
Single class for single responsibility.
Max mtg
Programmer
 
Posts: 1997
Joined: 02 Jul 2011, 14:26
Has thanked: 173 times
Been thanked: 334 times


Return to Developer's Corner

Who is online

Users browsing this forum: No registered users and 33 guests

Main Menu

User Menu

Our Partners


Who is online

In total there are 33 users online :: 0 registered, 0 hidden and 33 guests (based on users active over the past 10 minutes)
Most users ever online was 7303 on 15 Jul 2025, 20:46

Users browsing this forum: No registered users and 33 guests

Login Form