Game Development Community

SOLVED] Updating Waterblock color from script?

by Ted Southard · in Torque Game Engine Advanced · 12/29/2008 (4:18 pm) · 9 replies

The closest I got to an answer was this thread here in the first post, but it's quite old so I figured I'd ask and get a better answer :)

I'm trying to make a function that ultimately changes the baseColor of a waterblock. While I have the function working and actually modifying the field, the color of the water does not change. I can change it in the Editor, and have looked and not found anything that would specifically update the object that is much different than setting a field value.

Has anyone had any experience in getting a waterblock to update it's baseColor during runtime? Thanks in advance!

#1
12/29/2008 (5:30 pm)
I've had some wacky experience with waterblocks. It seems like the color is not applied at mission load time correctly - something in the ordering of client/server loading causes it to load the color with the default value.

To work around this, I added this function to C++ in WaterBlock.CPP

ConsoleFunction(UpdateWaterBlocks, void, 1, 1, "UpdateWaterBlocks()")
{
   argv; argc;
	SimpleQueryList sql;
   if (gServerSceneGraph)
   {
		gServerSceneGraph->getWaterObjectList(sql);

   	for (U32 i = 0; i < sql.mList.size(); i++)
   	{
   		WaterBlock* pBlock = dynamic_cast<WaterBlock*>(sql.mList[ i ]);

		   if (pBlock)
         {
            pBlock->inspectPostApply();
            pBlock->setMaskBits(4);
		   }
	   }
   }
}

Then, after the client loaded in ClientCommandSetClientPlayer (playgui.cs), I called a server command to update the blocks so they were resent:

echo("Updating water blocks");
commandToServer('UpdateWaterBlocks');


in commands.cs in server/scripts, I added the function to be called from the client:

function serverCmdUpdateWaterBlocks(%client)
{
UpdateWaterBlocks();
}

My guess is that you are seeing a similar problem.
#2
12/29/2008 (5:39 pm)
That's what I suspected was happening. I just came across it today when playing with the waterblocks and testing out a feature to "freeze" a lake over.

Your code works like a charm too! Awesome stuff!
#3
12/29/2008 (6:57 pm)
Here's something interesting: I created a timer function that interpolates the color of the water from "0 0 0 0" to "255 255 255 255" ten times per second over a period of 5 seconds. Now regardless of whether or not the water updates that fast, it should go from a black water to a white water with shades of grey between, but I'm seeing yellows and blues pop during that time, ending with white. Makes me wonder if the color values are transmitting correctly?

I just slowed the updates to 5/sec, with the same results: green and blue alternating, then yellow, very light gray, yellow again, and then white, but echo's of the interpolation function show greyscale values, as they should be. Go figure...
#4
12/29/2008 (8:11 pm)
Hmmm, that is weird. Perhaps there is some sort of interpolation going on in the engine itself, combining it with other values for fogging or depth purposes...
#5
12/30/2008 (7:39 am)
There's something going on, that's for sure. I can get the grayscale gradient to come out correctly if I use a 1 second schedule over a 5 second period of time. That works perfect- except the colors pop because of the 1-second timers. Anything more or less than 5 seconds using both 1 second and less than 1 second timers have resulted in multicolored lights.

Also, I decided to echo the getFieldValue for BaseColor, and it looks like only the red channel is getting set fully:

At: 63.75 63.75 63.75 0 the getFieldValue echoes: 63 11 0 255

But even stranger than that is if I use the BaseColor = %newcol; method of changing the color (vs SetFieldValue), the echo of BaseColor is: 63 220 0 255

Two different colors from two different methods that supposedly do the same thing- and neither are the color that I assign it, except in the red channel... I'm going to have to dip into the source code now ::sigh::
#6
12/30/2008 (8:17 am)
Maybe the fractional values are causing a problem? Have you tried truncating the values to an integer before assigning them?
#7
12/30/2008 (5:21 pm)
@Jaimi: You're totally right. I've been working with scripts so long that I completely forgot about the non-conversion of data types, lol. Thanks for the tip! Here's the result of the fruits of this labor:

Freezing Lake
#8
12/30/2008 (6:50 pm)
Now that is one cool effect.
#9
12/30/2008 (8:31 pm)
Thanks. It worked out to be a lot more blingy than I thought it would be ;)