Restarting a level
Ok, I think there is only 1 person on this forum that could help me, but with my game backend, I have a level class which holds and processes the objects in the game. I have added basic cloning in for objects and mods, and am trying to have the level reset the object list from an 'originalObjects' array that has clones of the original objects in their original state.
Is there an easier method for doing this than what I am currently attempting? So far it looks like I am going to have to go in and manually add every method and variable from extended classes to the inherited class, using a "cloneDeep()" method for the base objects and mods, and a 'clone()' method in every extended class that is called from the cloneDeep()(which will clone the GameObject classes variables and methods.
Please tell me there is a better way, or am I on the right track?
https://microstudio.io/i/SynKrown/darkmatter/
I read on Stackoverflow how to copy a regular deck. The problem is more serious than it seems.
The threads are long and there are a lot of issues that you need to understand what javascript does to copy correctly.
Copying objects is probably a step above the problem.
In my opinion, you should not copy objects - because, as you noticed, it requires recreating the entire network of dependencies.
Objects that are supposed to be models for others also take up memory.
It is better to do it so that the reset means building the level from scratch from the data.
Something like Deserialization. If you do this, you actually have a ready system for saving the game state (serialization). reset = loading initial settings.
When creating a class, you send data to the constructor in the object.
Test = class{
constructor( data ){
({ x : this.x, y : this.y, z : this.z } = data);
}
getData(){
return { x: this.x, y: this.y, z: this.z };
}
}
init = function() {
data = { x :10, y:20, z:30 }
test = new Test( data )
print( "test.x = " + test.getData().x )
test2 = new Test( test.getData() )
print( "test2.x = " + test2.getData().x )
}
Hmm that explains why its so hard to find code anywhere for proper deep copying of classes in javascript. I will experiment with the serialization idea you have brought forward and see what I can do. Will 'data' and its variables replace the "this." Variables in the constructor?
The example assigns data fields named data.x
, data.y
, data.z
to this
.
This is equivalent to this.x = data.x
.
If you wrap all the data for the constructor into an object, you don't have to remember the order of the constructor's arguments, especially since there can be more than a dozen of these parameters. You can also omit unnecessary parameters or those that are identical to the default ones set by the constructor.
Matter and PIXI this coding style.
constructor( data ){
this.width = data && data.width !== undefined ? data.width : 0;
this.height = data && data.height !== undefined ? data.height : 0;
if ( this.width < 0){ this.width = 0 }
if ( this.height < 0){ this.height = 0 }
}
Building the entire level from scratch will be easier than copying/cloning with dependencies.
The only easy way I came up with is for each object to remember its state - it can be initial or from some point in time. Reset is the restoration of fields from that point in time.
However, you need to be careful with this action - because, for example, you can restore pointers to objects that have been deleted.
Ok that sounds like a much less tedious way of doing it than what I was going to attempt.
I'm just trying to work out how hard it would be to allow the extended classes(like ship, rock etc) without losing the inherited objects data. Also, the event methods, as I have onCreate, onUpdate etc, each extended game object or mod will have its own. Will that still carry over fine? I guess instantiating a new "ship" object will just take in the data of the "cloned" object and the methods will still be in tact.
This is definitely an outside the box way of doing it imo. Experiments on!
Cheers
I have a great idea for a game. I will make it happen eventually but with your javascript and programming knowledge in general, I think the idea could come to fruition at a much faster pace. Its going to be a complicated game, but also open world(or open space). No doubt you will be giving me tips and advice through here when I end up stuck with something, but if you would like to work on this game with me directly, or are even interested in the possibility, I would like to be able to talk to you directly.
I am currently working full time but this game backend has been my main focus outside of work. And given the rise in indie games in the past few years, I think this idea is original enough to hit the mark. All I need is to be able to talk to you directly about any issues I face, and I am keen on a 50/50 split. Whether a Kickstarter type thing is potential money to help fund progress or steam etc, all 50/50.
If you aren't interested i understand, but you have been such a great help so far, and I would love to cut you in
So I expanded on your idea and after a day of trial and error, I have it functioning as intended. It even successfully copies the mods AND it doesnt require any extra adding variables to the data. It iterates through the objects properties and adds them to the data object automatically, so even the extended objects variables are automatically copied.
I am so stoked right now, cause this felt like a massive hurdle. I probably have some redundant code in there from the experimenting but the main thing is it seems to be working consistently.