jamessimo Posted August 24, 2015 Share Posted August 24, 2015 Hey guys, I have a question about game loops for large games in javascript. I am making a tycoon style game where at any time I can have many Sales Men who are working simultaneously. This means they all need to have accsess to the GAMELOOP at any given time. I have provided pseudo code on how I currently do this, however this feels messy. The code is simply, making two Sales Men, setting there current task with the assignJob function, then the loop will detect their Job and add to there profit. Is there a better way to do this (inside the step function)? init();//Set JobsassignJob(SalesMen[0], 'Door to Door Vacuum salesman');assignJob(SalesMen[1], 'Door to Door Ironing Board salesman');function init(){ //Set up SalesMan SalesMen = [ {id: 1, name: 'example1', job: false,profit: $0.00}, {id: 2, name: 'example2', job: false,profit: $0.00}, ]; step();}function assignJob(salesMan,jobType){ salesMan.job = jobType;}function step(){ //HERE IS THE ISSUE - THIS FEELS WRONG for(var i = 0 ; i < SalesMen.length ; i++){ if(SalesMen[i].job){ SalesMen[i].profit += 0.10; } } //LOOP GAME step();} (fyi I also had a version where each function that needed a loop would just loop internally, I removed this as I know having n-nunber of loops is bad) Quote Link to comment Share on other sites More sharing options...
kcaze Posted August 24, 2015 Share Posted August 24, 2015 The solution I tend to go for to manage complexity is something like the following:function SalesMan(properties) { for (var key in properties) { this[key] = properties[key]; }}SalesMan.prototype.name = '';SalesMan.prototype.job = '';SalesMan.prototype.profit = '';SalesMan.prototype.update = function () { this.profit += 0.1;};function init(){ //Set up SalesMan SalesMen = [ new SalesMan({ id: 1, name: 'Alice', job: 'Vacuum Saleswoman', }), new SalesMan({ id: 2, name: 'Bob', job: 'Ironing Board Salesman', }), ]; step();}function step(){ SalesMen.forEach(function (salesman) { salesman.update(); });}So basically, for all objects in your game that you care about, you define a class for them and then write the logic for updating them in the class's update function. In your game loop then, you just loop over each object and call its update function. This has the advantage that you can write the update functions for each of your classes separately and even spread them across Javascript files, instead of having all the logic for your game in a single game loop function. Hope this helps! Feel free to ask follow-up questions in case my example code wasn't clear / you find this way of organizing your code weird! Eamon and jamessimo 2 Quote Link to comment Share on other sites More sharing options...
jamessimo Posted August 24, 2015 Author Share Posted August 24, 2015 The solution I tend to go for to manage complexity is something like the following:function SalesMan(properties) { for (var key in properties) { this[key] = properties[key]; }}SalesMan.prototype.name = '';SalesMan.prototype.job = '';SalesMan.prototype.profit = '';SalesMan.prototype.update = function () { this.profit += 0.1;};function init(){ //Set up SalesMan SalesMen = [ new SalesMan({ id: 1, name: 'Alice', job: 'Vacuum Saleswoman', }), new SalesMan({ id: 2, name: 'Bob', job: 'Ironing Board Salesman', }), ]; step();}function step(){ SalesMen.forEach(function (salesman) { salesman.update(); });}So basically, for all objects in your game that you care about, you define a class for them and then write the logic for updating them in the class's update function. In your game loop then, you just loop over each object and call its update function. This has the advantage that you can write the update functions for each of your classes separately and even spread them across Javascript files, instead of having all the logic for your game in a single game loop function. Hope this helps! Feel free to ask follow-up questions in case my example code wasn't clear / you find this way of organizing your code weird! This is brilliant, i was thinking of adding an update function to the salesman OBJ. But does this have negative implications if say I have a lot of sales men and other similar objects who need to be iterated over? Or is this just a reality of game development? Again thanks a ton, this has got me on the right track. Quote Link to comment Share on other sites More sharing options...
kcaze Posted August 24, 2015 Share Posted August 24, 2015 There is some overhead to calling a function to update instead of inlining the code in your game loop but it should be neglible.As for if you need to worry about performance when you have a lot of objects to update, here's a back of the envelope estimate for when you might need to start worrying. CPUs can usually process about 10^9 instructions per second. Modern games should at 60 fps, so each update had better take less than 1/60 of a second or your game will start to lag. This means you can execute around 10^7 instructions per second. If your update function is relatively simple (e.g. increase profit by a fixed amount) then this means you can have up to 10^7 objects and still have your game run fine. If you have a more complicated update function (e.g. check for collisions with all other objects), the number of instructions grows quadratically with the number of objects so you can have around 3000 objects.This is a really rough estimation though and you shouldn't worry about performance too much until you get to a point where your game actually lags. There's a famous quote by Knuth which says "Premature optimization is the root of all evil."! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.