Game Development Community

Passing variable into function as reference/pointer?

by Christian Stock · in Torque Game Builder · 02/04/2006 (11:23 pm) · 2 replies

I'd like to pass two variables into a function as references, so I can modify their values:

new GuiButtonCtrl(Fur_out) {
profile = "GuiButtonProfile";
horizSizing = "right";
vertSizing = "bottom";
position = "295 136";
extent = "20 20";
minExtent = "8 2";
visible = "1";
command = "subGoodTradeDlg( $FurCount, $FurPrice );";
text = "<";
groupNum = "-1";
buttonType = "PushButton";
};

function subGoodTradeDlg( %good_count, %good_price )
{
if ( %good_count > 0 )
{
%good_count--;
$goodstradedlg_money += %good_price;
}
}

I'd like to change the values $FurCount, $FurPrice - and since I have several goods (not just fur) I'd like to do this via a function.

How do I achieve this?

Cheers,
Christian

#1
02/10/2006 (10:44 am)
Torquescript doesn't have the ability to pass by reference. But you can work around this using ScriptObjects with.

So you would do something like this:

$fur = new ScriptObject(fur){
     count = 10;
     price = 10;
};

new GuiButtonCtrl(Fur_out){
	profile = "GuiButtonProfile";
	horizSizing = "right";
	vertSizing = "bottom";
	position = "295 136";
	extent = "20 20";
	minExtent = "8 2";
	visible = "1";
	command = "subGoodTradeDlg($fur);";
	text = "<";
	groupNum = "-1";
	buttonType = "PushButton";
};

function subGoodTradeDlg( %good )
{
	if ( %good.count > 0 )
	{
		%good.count--;
		$goodstradedlg_money += %good.price;
	}
}
#2
02/11/2006 (6:07 pm)
Thanks, Harold

This is actually what I'd like to do.

However, I also have text boxes in the same window:

If I would use

new GuiTextCtrl(Fur_Value) {
profile = "GuiTextProfile";
horizSizing = "right";
vertSizing = "bottom";
position = "185 136";
extent = "8 18";
minExtent = "8 2";
visible = "1";
variable = "$fur.price";
maxLength = "255";
};

I get a blank TextCtrl. Hence I'm using a workaround with $FurPrice.
Anyway, I worked this out by doing something different and it works...

Cheers,
Christian