Topic awaiting preservation: OOP problem, pushing around methods while maintaining references. (Page 1 of 1) |
|
---|---|
Bipolar (III) Inmate From: Sweden |
posted 01-09-2006 21:29
I've got a rather complex problem, well the problem itself is probably not so complex but the code structure is. code: Data={ Actions:[], currentAction=false, Objectives : { Obj1 : { Done : false, Tasks : [new Task1(2,187),new Task2(1,74),new Task1(0,35)], // The list of Tasks to do before this Objective is completed and the arguments needed to customize them. Run : function(){ // This method is executed var noTask=true; for(var i=0;i<this.Tasks.length;i++){ if(!this.Tasks[i].Done){ First_Action(this.Tasks[i].Tick); noTask=false break; } } if(noTask){ this.Done=true for(var i=0;i<this.Tasks.length;i++){ this.Task[i].Reset() } } } } }, } function Add_Action(func){ var action = { // this is the object to which Tick keeps accessing. Main_Code : func, arguments : [] } for(var arg=1;arg<arguments.length;arg++){ action.arguments.push(arguments[arg]) } Data.Actions.push(action) } function First_Action(){ // Does the same thing as Add_Action but puts the action at beginning of the list. // Enables me to queue sub-actions directly after currentAction. } function Run_Actions(){ if(!Data.currentAction){ Data.currentAction=Data.Actions.shift() } if(Data.currentAction){ if(Data.currentAction.Main_Code()){ Data.currentAction=Data.Actions.shift() if(Data.currentAction){ setTimeout("Run_Actions()",10) } } else{ Write_Log("Waiting...") setTimeout("Run_Actions()",100) } } } function Decide(){ // This Action is the "main loop" which decides what to do by queueing up Actions. if(Math.random()>0.5){ // Add_Action(Action1,Math.random()) } else{ Add_Action(Action2) // When running this Action it will try to run } Add_Action(Decide) // Make a new decicion after the queue has been emptied. return true } function Action1(){ if(Data.currentAction.arguments[0]>0.5){ // Retrieve the argument sent in the Decide function. return true // This Action has been performed successfully } return false // Dang, no luck this time, retry this Action after waiting some time. } function Action2(){ if(!Data.Objectives.Obj1.Done){ // This Action triggers an objective and runs it Task by task until it has ended. Data.Objectives.Obj1.Run() // New Actions will be placed in the queue one or more at a time by the objective. } alert("Objective completed!") return true } function Task#(arg1,arg2){ var private1=arg1 // These are the properties I want to access when Tick is placed in the Queue as the method Main_Code. var private2=arg2 this.Done=false; this.Tick=function(){ // This is the function that will be queued and run later in Run_Actions(). private1+=Math.random() private2-=Math.random() if(private1>5 || private2<5){ this.Done=true; // This Task is done, skip it when trying to complete the Objective next time. // As it is now, setting this has no effect since it only changes Data.currentAction.Done instead of Data.Objectives.Obj1.Tasks[#].Done } return true; } } Add_Action(Decide) Run_Actions()
|
Paranoid (IV) Mad Scientist with Finglongers From: Germany |
posted 01-09-2006 22:05
how about passing your Task in with as an argument to the func in add_action()/first_action (which probably should be prepend_action ;-)? |
Bipolar (III) Inmate From: Sweden |
posted 01-09-2006 22:19
Yes you are making sense. |
Bipolar (III) Inmate From: Sweden |
posted 01-10-2006 22:20
Yes it works. |
Paranoid (IV) Inmate From: schillmania.com |
posted 01-17-2006 04:12
I probably didn't read through your code thoroughly enough, but I typically work around "this" scope issues via closures: code: var SomeObject(oImg) { var self = this; this.o = oImg; this._property = false; this._mouseover = function() { // "this" == oImg due to event handler, however self._property = true; } this.o.onmouseover = this._mouseover; } var so = new SomeObject(someImage);
|
Bipolar (III) Inmate From: Sweden |
posted 01-20-2006 00:33
Scott: Either you didn't understand my post, or I don't understand yours in comparison to mine... don't know which lol. |