Game Development Community

Copy a custom ScriptObject

by RollerJesus · in Torque Game Builder · 05/25/2010 (6:48 pm) · 4 replies

Hey all...

I created a custom object in source called FillModel that extends ScriptObject. I also created a method that I exposed to script to check if this FillModel is playable by letting the AI run through and simulate a game - isBoardFair(). All is well here.

My problem is that when I check to see if the board is fair, the game is already over when I start. I'm trying to create a copy of the FillModel object in script to do the simulation but it's creating a reference rather than a copy.

Here is the relevant part of the script I'm using:
...
   %this.model = new FillGridModel();
   %this.model.initialize( %maxX, %maxY );
    
   %tempModel = new FillGridModel();
   %tempModel = %this.model;  //here is my problem...
      
   if(!%tempModel.isBoardFair(10, 2))
   {
      echo("This board is not fair, restarting game.");
      %this.startGame();
   }
...

Is this possible or do I need to create a copy of the object in source and do the simulation on that?

Any help appreciated!

Patrick

#1
05/25/2010 (8:09 pm)
It looks like you are replacing your new object with a pointer to the existing object (effectively wiping out the new object). For the new model to be a copy you would have to clone the properties of %this.model. I don't know if torque has a clone method. Otherwise, you have to do it manually.
#2
05/26/2010 (10:55 am)
Thanks Kevin.

I guess I can expose the relevant properties via console methods and copy each of them, then check the copied board for fairness. That was just wishful thinking I guess.

I'm quite out of my comfort zone here but it's been fun tooling with C++.

If you, or anyone else, have any other suggestions, I'm all ears.

Patrick
#3
05/28/2010 (6:35 am)
In case anyone was considering explaining to me that I need to go read about deep vs. shallow copies, copy constructors and C++ programming concepts in general - which you should because I'm a complete hack and I need to be told that kind of thing - don't bother...

I solved this problem by taking a different direction but in the end, I found the same result with similar performance and even an unexpected perk. Nice!

What was going on?
In source, I was populating a t2dTileLayer with random values between 0 and 5 and instantiating several plain data type fields (S32's) and 2 vector stacks. The vector stacks were the problem for me as I needed to copy over the values in these stacks and that wasn't possible doing:
other->stack = this->stack.
It would require some helper methods to pop all the values from the stack, push them onto a new stack and push them back onto the original stack. Possible, nay easy for some. Not for me...

What I ended up doing
Since the grid was populated with random S32's, I simply added a line to the initialize method in source that accepts a seed for the random number and exposed that value to script. This allows me to pass an S32 during initialization and control what board is created.

So instead of creating a board, copying it, checking the copy for fairness (which modifies the board) and playing the original if it's fair.

I simply create two boards with the same randomly generated seed:
%seed = getRandom(1, 60000);
Check one of the boards for fairness and if it's fair, play the other board.

A very cool side effect of this approach is that I can recall boards using just the seed value!

Well, that's what I've been doing at nights for the past week. Good luck with all your projects!

#4
05/28/2010 (6:52 am)
Congrats on your elegant solution!