First off, I’m sorry if this is a n00b question. I’m a self-taught web developer and this is my first real foray into apps. I’ve tried to find the answer to what I’m looking for, but I’m not sure on the proper terminology and so haven’t found any relevant results. I figure this is because either I don’t know the right way of asking for what I need; the answer is so obvious it doesn’t need writing about or what I’m asking is so stupid the concept doesn’t exist and I should be totally doing it another way.
Anyway, on to my question. I’m working on an app that has 3 classes that are giving me trouble. There is the ‘MainContainer’, which mostly handles the UI; the ‘Assigner’ which picks out tasks to do; and the ‘Worker’ which does the tasks. It’s in a very early stage, but this is kind of how it looks right now.
class MainContainer
def setup
@worker = Worker.new(:x => 200, :y => 200)
@assigner = Assigner.new(:x => 400, :y => 300)
end
# update & draw code
end
class Assigner
def set_target
parent.worker.target self.x, self.y
end
# input, update & draw code
end
class Worker < GameObject
def target(tx, ty)
@target_x = tx
@target_y = ty
@has_task = true
end
# update & draw code
end
The basic flow of the app goes something like this: an instance of the MainContainer loads and shows the UI on the screen including the assigner and worker. The user then uses the assigner to move around the UI and choose a task. Everything works fine up until this point.
What I can’t figure out is how the Assigner can pass a value to the worker. The code I have in the example is the set_target method of the worker. The parent.worker.target self.x, self.y line is the most recent of my attempts. Other things I’ve tried here include things like @worker.target self.x, self.y
I was thinking of passing the @worker into the @assigner, but the plan is for there to be an arbritrary number of workers that will increase/decrease over time. I plan to handle these with a WorkerManager that will keep track of how many workers are currently available, but if I pass this into the @assigner will it not be just a copy of it that is passed? I’m still way off from implementing anything like that so I haven’t tested it, perhaps that is the way to go?
Another thing I’ve thought about is having the worker as a global, but this is a problem that will likely rise again quite frequently so I’d end up with an app full of globals, I’m guessing that’s not the right way!
I’m from web-dev so the only other solution I can think of is to database the whole lot of it. This is a last resort if I can’t get anything else going though.
Anyway, to re-state my question. In an instance of MainContainer, how do I get its Assigner and Worker to pass data between each other?
I’d really appreciate any help here, even if it goes something like “stfu n00b, read this: -relevant link”.
I think there’s a flaw in the design somewhere. You say that the Assigner picks the task and the Worker does it. It doesn’t look like they need to communicate. If they need to, they may be doing more than what you described, which may indicate an opportunity for refactoring into more isolated classes with fewer responsibilities.
So, what if the MainContainer had all control?
The UI should really be separate from the business logic, the pseudocode above just illustrates the idea of giving control to the MainContainer to avoid having the Worker and the Assigner to talk to each other. This will also allow you to handle multiple workers if necessary.
I hope this helps.