tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/13229-sepparating-data-from-logicRobotlegs: Discussion 2015-10-28T15:48:52Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/382316802015-10-19T07:49:09Z2015-10-19T23:47:58ZSepparating Data From Logic<div><p>This one is a little abstract and not RobotLegs specific but I
was hoping someone may be able to help.</p>
<p>Say I have a User object that has an "xp" value. Now say that I
have an XPLevelsModel which contains an array of XPLevels that
define how many points are required for that level (among other
things):</p>
<pre>
<code>class User
{
public User(levelsModel:XPLevelsModel) : {}
xp : int;
}
class XPLevelsModel
{
levels : XPLevel;
}
class XPLevel
{
...
xpRequired : int;
name : String;
...
}</code>
</pre>
<p>Okay so now say I have a number of methods such as
"getCurrentLevel()" "getPercentProgressThroughLevel()" etc. Where
should those methods live?</p>
<p>I could put them on the User object:</p>
<pre>
<code>class User
{
xp : int;
getCurrentLevel() : int {}
getPercentProgressThroughLevel() : Number {}
...
}</code>
</pre>
<p>but as we add more data such as "lives", "health" etc etc User
will start to grow with lots of functions (not very good for
SoC)</p>
<p>I could try to group these things up into components such as
"XPComponent" which is an object on User:</p>
<pre>
<code>class User
{
xp : UserXPComponent;
}
class UserXPComponent
{
public UserXPComponent(levelsModel:XPLevelsModel) {}
points : int;
getCurrentLevel() : int {}
getPercentProgressThroughLevel() : Number {}
...
}</code>
</pre>
<p>but the problem is accessing this data from the rest of the game
now means user.xp.getCurrentLevel(), which breaks the law of
Demeter, plus its ugly, hard to test and then what happens if you
go more levels deep?</p>
<p>There is one more option I thought of, to have something
like:</p>
<pre>
<code>class User
{
xp : int
}
class UserXPHelpers
{
constructor(levelsModel:XPLevelsModel) {}
getCurrentLevel(user:User) : int {}
getPercentProgressThroughLevel(user:User) : Number {}
}</code>
</pre>
<p>Here we have separated out the logic and the data and pass the
user through the methods. The only thing is, does this not break
encapsulation? As all those methods rely upon a user to be passed
to them, shouldn't they live with the user?</p>
<p>If this IS the correct solution, is "Helpers" the correct name
for this?</p>
<p>I hope you can help a problem that has challenged me time and
time again and have only now been able to articulate it into a
query.</p>
<p>Mike</p></div>mike.canntag:robotlegs.tenderapp.com,2009-10-18:Comment/382316802015-10-19T14:08:07Z2015-10-19T14:08:07ZSepparating Data From Logic<div><p>Hey Mike!</p>
<p>Levels, lives, health... it sounds like you're talking about a
game.<br>
I'm not experienced in game programming. I'm still in the process
of learning about game design.<br>
On the one hand, I don't know enough to give a qualified answer, on
the other hand I think that different types of games require
different approaches ( entity system approach, or a component based
architecture, etc)<br>
From a novice point of view, the question that first popped up in
my mind was: What is User?<br>
The 'player' in a game which has states and levels and each level
creates a player (the UI as a ship or something like this), or is
User the person playing the game and as such outside of the game
logic and you need to store and update the stats ( lives, levels,
health) in a model and eventually send them to an external data
store?<br>
If the User is the latter, I would communicate between the Game and
the classes outside of the 'game-loop' through events.<br>
I would let the Game dispatch events/signals when its state
changes, and I'd let a command update the User Model. But I'm sure
you would have used this approach already, if the use case were
that simple. Could you expand a bit on the scenario?</p>
<p>Your UserXPHelpers would be a Model in my opinion. A
UserLevelsModel? It would aggregate data from a User and a Levels
Model. If you don't mind the coupling, and if it works well for you
(and it is easier to test) then use it, why not? I prefer to use
commands to get data from multiple models and to write it into a
composite model which would act like a junction table in a
database.</p>
<p>Have you heard of Ash? It is a great framework for games
developed by Richard Lord. It seems to be a better fit for games
than the MVCS approach. <a href="http://www.ashframework.org/">http://www.ashframework.org/</a></p>
<p>Strangely enough, I'd also recommend to take a look at
strangeIoC, a framework for Unity3D and C#, inspired by robotlegs
;)<br>
An experienced robotlegs user, like yourself, will easily
understand strangeIoC's philosophy, and you might find the
approaches used in the 2 games-examples mentioned below
inspiring.</p>
<p><a href="https://github.com/strangeioc/strangeioc">https://github.com/strangeioc/strangeioc</a><br>
and the examples :<br>
<a href="https://github.com/strangeioc/strangerobots">https://github.com/strangeioc/strangerobots</a><br>
<a href="https://github.com/strangeioc/strangerocks">https://github.com/strangeioc/strangerocks</a></p>
<p>I'm sorry for not being of much help on this. Let's hope that
someone with more game-programming experience will participate in
the discussion, as well:)</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/382316802015-10-20T00:12:24Z2015-10-20T00:12:25ZSepparating Data From Logic<div><p>Hi Ondina,</p>
<p>Thanks for your reply, really appreciated :)</p>
<p>The question was more of a generic one, rather than a game
specific question. Just to increase my chances for understanding I
also asked it on StackOverflow:</p>
<p><a href="http://stackoverflow.com/questions/33209309/separating-data-from-logic">
http://stackoverflow.com/questions/33209309/separating-data-from-logic</a></p>
<p>I think I understand a little more now but still for my problem
not 100% sure on the correct solution. I think its one of those "it
depends" sort of answers.</p>
<p>As it happens I am very familiar with Ash having built several
games in it and have a Unity version of Ash as an experiment:</p>
<p><a href="http://www.mikecann.co.uk/programming/unity-ash-upgrades/">http://www.mikecann.co.uk/programming/unity-ash-upgrades/</a></p>
<p>I also have used StrangeIoC for a large Unity game (not yet
released) and so am pretty familiar with that library too.</p>
<p>I think this is one im going to have to think more on tho I
really appreciate any other thoughts you may have.</p>
<p>Cheers,<br>
Mike</p></div>mike.canntag:robotlegs.tenderapp.com,2009-10-18:Comment/382316802015-10-20T17:08:52Z2015-10-20T17:08:52ZSepparating Data From Logic<div><blockquote>
<p>As it happens I am very familiar with Ash having built several
games in it and have a Unity version of Ash as an experiment: I
also have used StrangeIoC for a large Unity game (not yet released)
and so am pretty familiar with that library too.</p>
</blockquote>
<p>Haha! I wasn't aware I was talking to an expert:)<br>
Nice work on the Unity port of Ash!!!</p>
<blockquote>
<p>The question was more of a generic one, rather than a game
specific question.</p>
</blockquote>
<p>Yes, I know, you said that in your first post. Nevertheless, I
think that it matters whether it is a game or not. The
relationships between classes might be depending on the game
architecture that you've implemented or on the particularities of a
framework that you've used, like Ash for example.</p>
<p>I'm glad you've got an answer on stackoverflow!</p>
<blockquote>
<p>I think I understand a little more now but still for my problem
not 100% sure on the correct solution. I think its one of those "it
depends" sort of answers. I think this is one im going to have to
think more on tho I really appreciate any other thoughts you may
have.</p>
</blockquote>
<p>Yeah, it depends ;)</p>
<p>I agree with neleus on stackoverflow: "it depends on how the xp
is related to User"</p>
<p>To me it is unclear what is the role of the UserXPComponent and
why would<br>
UserXPComponent be an object on User.<br>
Is UserXPComponent the View in MVCS parlance and User a Model
reflecting the states of UserXPComponent?<br>
Or is User a VO?<br>
Also what is the relationship between Levels and
UserXPComponent?<br>
Who belongs to whom?<br>
What part of the application triggers the transition to the next
level?</p>
<p>If you were to build data base tables for these objects, how
would you structure them? Are xp, level, health, lives properties
of a User? Does<br>
UserXPComponent have layout specific properties, like x, y,
rotation angle, color? Do you need to store and then re-load this
layout info ?</p>
<p>What about using a Model containing kind of a look up table, in
form of a dictionary or another collection, where you store the
user's achievements with user_id as the key and level_id, score,
lives etc as the values? Why wouldn't that work?</p>
<p>I only have questions for you, no real solutions. But, perhaps
just by trying to formulate an answer to my questions, will make
things more clear to you.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/382316802015-10-22T07:37:44Z2015-10-22T07:37:46ZSepparating Data From Logic<div><p>Hi Olinda,</p>
<p>Good points and questions, lots of think about there. I guess
you are right about asking the question, is xp a property of user
or not. XP is, but if XPLevel isnt then perhaps it shouldn't belong
on the User class, it should be derived.</p>
<p>Food for thought :)</p></div>mike.cann