Flaw in a test version of my shop system...
by Orion the Hunter · in Torque Game Builder · 01/04/2013 (12:14 pm) · 7 replies
Hi, can someone tell me why this won't work?
function itemShopGui::onWake(%this)
{
ItemList.entryCount = 0;
ItemList.clear();
for(%file = findFirstFile("*.itm"); %file !$= ""; %file = findNextFile("*.itm"))
{
ItemList.fileName[ItemList.entryCount] = %file;
ItemList.addRow(ItemList.entryCount, fileBase(%file));
ItemList.entryCount++;
}
ItemList.sortNumerical(0);
for(%i = 0; %i < ItemList.entryCount; %i++)
{
%rowId = ItemList.getRowId(%i);
%text = ItemList.getRowTextById(%rowId);
%text = restWords(%text); //%text = %i + 1 @". "@ restWords(%text);
ItemList.setRowById(%rowId, %text);
}
ItemList.setSelectedRow(0);
PlayerBalance.setText($pref::points);
}
function ItemList::onSelect(%this, %row)
{
%fo = new FileObject();
%fo.openForRead(%this.fileName[%row]);
%text = "";
while(!%fo.isEOF())
%text = %text @ %fo.readLine() @"\n";
%fo.delete();
itemText.setText(%text);
}
function getitem(%itemName)
{
Canvas.pushDialog(itemShopGui);
if(%itemName !$= "")
{
%index = ItemList.findTextIndex(%itemName);
ItemList.setSelectedRow(%index);
}
}
function contextitem()
{
for(%i = 0; %i < Canvas.getCount(); %i++)
{
if(Canvas.getObject(%i).getName() $= itemShopGui)
{
Canvas.popDialog(itemShopGui);
return;
}
}
%content = Canvas.getContent();
%itemPage = %content.getitemPage();
getitem(%itemPage);
}
function GuiControl::getitemPage(%this)
{
return %this.itemPage;
}
function GuiMLTextCtrl::onURL(%this, %url)
{
echo("\c4A GuiMLTextCtrl: "@ %this @", is opening url:"@ %url);
gotoWebPage(%url);
}
function Shop()
{
canvas.pushdialog(ItemShopGui);
}
function PurchaseItem()
{
call($ItemID);
}
function getPrice()
{
//Bunch of if statements!!!
$ItemID = ItemID.getValue();
if($ItemID = "PINE")
{
$ItemCost = 10000;
}
if($ItemID = "FPNE")
{
$ItemCost = 50000;
Cost.setText($ItemCost);
}
if($ItemID = "MPNE")
{
$ItemCost = 55000;
Cost.setText($ItemCost);
}
if($ItemID = "FMPN")
{
$ItemCost = 100000;
Cost.setText($ItemCost);
}
//if($ItemID = )
//{
//$ItemCost = ;
//Cost.setText($ItemCost);
//}
//if($ItemID = )
//{
//$ItemCost = ;
//Cost.setText($ItemCost);
//}
//if($ItemID = )
//{
//$ItemCost = ;
//Cost.setText($ItemCost);
//}
//$ItemCost = ;
//}
//if($ItemID = )
//{
//$ItemCost = ;
//Cost.setText($ItemCost);
//}
//$ItemCost = ;
//}
//if($ItemID = )
//{
//$ItemCost = ;
//Cost.setText($ItemCost);
//}
//$ItemCost = ;
//}
//if($ItemID = )
//{
//$ItemCost = ;
//Cost.setText($ItemCost);
//}
//$ItemCost = ;
//}
//if($ItemID = )
//{
//$ItemCost = ;
//Cost.setText($ItemCost);
//}
//$ItemCost = ;
//}
#2
It's for my shop. I'm not getting any errors, but when I use my "get price" button, nothing happens. It probably has to do with the "if" statements.
01/04/2013 (12:17 pm)
new GuiScrollCtrl() {
canSaveDynamicFields = "0";
isContainer = "1";
Profile = "GuiScrollProfile";
HorizSizing = "relative";
VertSizing = "relative";
Position = "526 168";
Extent = "394 280";
MinExtent = "8 8";
canSave = "1";
Visible = "1";
tooltipprofile = "GuiToolTipProfile";
hovertime = "1000";
willFirstRespond = "1";
hScrollBar = "alwaysOff";
vScrollBar = "alwaysOn";
constantThumbHeight = "0";
childMargin = "0 0";
new GuiTextListCtrl(ItemList) {
canSaveDynamicFields = "0";
isContainer = "1";
Profile = "GuiTextListProfile";
HorizSizing = "right";
VertSizing = "bottom";
Position = "2 2";
Extent = "374 8";
MinExtent = "8 8";
canSave = "1";
Visible = "1";
tooltipprofile = "GuiToolTipProfile";
hovertime = "1000";
enumerate = "0";
resizeCell = "1";
columns = "0";
fitParentWidth = "1";
clipColumnText = "0";
};
};
new GuiMLTextCtrl(PlayerBalance) {
canSaveDynamicFields = "0";
isContainer = "0";
Profile = "GuiDefaultProfile";
HorizSizing = "relative";
VertSizing = "relative";
Position = "222 487";
Extent = "277 13";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
hovertime = "1000";
lineSpacing = "2";
allowColorChars = "0";
maxChars = "-1";
};
new GuiMLTextCtrl(Cost) {
canSaveDynamicFields = "0";
isContainer = "0";
Profile = "GuiLargeBlackProfile";
HorizSizing = "relative";
VertSizing = "relative";
Position = "222 384";
Extent = "277 13";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
Variable = "$ItemCost";
hovertime = "1000";
lineSpacing = "2";
allowColorChars = "0";
maxChars = "-1";
};
new GuiTextEditCtrl(ItemID) {
canSaveDynamicFields = "0";
isContainer = "0";
Profile = "GuiNewTextEditProfile";
HorizSizing = "relative";
VertSizing = "relative";
Position = "98 280";
Extent = "408 48";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
Variable = "$ItemID";
hovertime = "1000";
maxLength = "4";
historySize = "0";
password = "0";
tabComplete = "0";
sinkAllKeyEvents = "0";
password = "0";
passwordMask = "•";
};
new GuiBitmapButtonCtrl() {
canSaveDynamicFields = "0";
isContainer = "0";
Profile = "GuiDefaultProfile";
HorizSizing = "relative";
VertSizing = "relative";
Position = "96 560";
Extent = "416 48";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
Command = "getPrice();";
hovertime = "1000";
groupNum = "-1";
buttonType = "PushButton";
UseMouseEvents = "0";
bitmap = "~/gui/invisible.png";
};
};
//--- OBJECT WRITE END ---It's for my shop. I'm not getting any errors, but when I use my "get price" button, nothing happens. It probably has to do with the "if" statements.
#3
I might be wrong but instead of the normal "Command" variable have the AltCommand = "getPrice();";
variable. I think I ran across this problem some time before.
01/04/2013 (2:12 pm)
So you know that it is being called put an echo command in the "getPrice" function. Once you are certain of it being called then you can debug the rest.I might be wrong but instead of the normal "Command" variable have the AltCommand = "getPrice();";
variable. I think I ran across this problem some time before.
#4
What that code does is say "if I assign 'PINE' to $ItemID" and in Torquescript it only results in making $ItemID equal "PINE" and treating that as "false". So none of your cases work. Replace = with $=.
The Command field should be ok, but personally I name my gui elements and then handle the callbacks, so:
I rarely use the Command and AltCommand fields.
Also, it's tough to test without data to fill that list - had to fake some up.
01/04/2013 (2:25 pm)
Ok, if statements are a bad place to make assignments - like this:if($ItemID = "PINE") // this is wrong - = is not the same as $=
What that code does is say "if I assign 'PINE' to $ItemID" and in Torquescript it only results in making $ItemID equal "PINE" and treating that as "false". So none of your cases work. Replace = with $=.
The Command field should be ok, but personally I name my gui elements and then handle the callbacks, so:
function PurchaseButton::onClick(%this)
{
// do my button stuff here
}I rarely use the Command and AltCommand fields.
Also, it's tough to test without data to fill that list - had to fake some up.
#5
Cost.setText($ItemCost);
make sure this command actually works.
01/04/2013 (11:44 pm)
Also for something like this try using Case statements it's a lot cleaner and easier to work with on something like this.Cost.setText($ItemCost);
make sure this command actually works.
#6
01/05/2013 (6:40 am)
Naw, use a SimSet of ScriptObjects to hold the data - so you set the internal name of the script object to be your ID, use fields on that object to hold your item data like cost, associated images, game stats, etc. Save and load the simset. then your getPrice() looks like this:// in load, just do this
$ItemSet = exec("data/itemdata.cs"); // where we assume this is where you saved the simset
function getPrice()
{
//Bunch of if statements!!!
$ItemID = ItemID.getValue();
%obj = $ItemSet.findObjectByInternalName($ItemID);
Cost.setText(%obj.price);
$ItemCost = %obj.price;
}No huge if block or monolithic switch to maintain. Item data loading is simplified vastly.
#7
01/07/2013 (5:29 am)
Thanks, it seems to work!
Orion the Hunter
Fritterflames
//if($ItemID = ) //{ //$ItemCost = ; //Cost.setText($ItemCost); //} //$ItemCost = ; //} //if($ItemID = ) //{ //$ItemCost = ; //Cost.setText($ItemCost); //} //$ItemCost = ; //} //if($ItemID = ) //{ //$ItemCost = ; //Cost.setText($ItemCost); //} //$ItemCost = ; //} //if($ItemID = ) //{ //$ItemCost = ; //Cost.setText($ItemCost); //} //$ItemCost = ; //} //if($ItemID = ) //{ //$ItemCost = ; //Cost.setText($ItemCost); //} //$ItemCost = ; //} //if($ItemID = ) //{ //$ItemCost = ; //Cost.setText($ItemCost); //} //$ItemCost = ; //} //if($ItemID = ) //{ //$ItemCost = ; //Cost.setText($ItemCost); //} //$ItemCost = ; //} //if($ItemID = ) //{ //$ItemCost = ; //Cost.setText($ItemCost); //} //$ItemCost = ; //} //if($ItemID = ) //{ //$ItemCost = ; //Cost.setText($ItemCost); //} //$ItemCost = ; //} //if($ItemID = ) //{ //$ItemCost = ; //Cost.setText($ItemCost); //} //$ItemCost = ; //} //if($ItemID = ) //{ //$ItemCost = ; //Cost.setText($ItemCost); //} //$ItemCost = ; //} }It links to the following GUI���¢�¯�¿�½���¦
//--- OBJECT WRITE BEGIN --- %guiContent = new GuiBitmapCtrl(ItemShopGui) { canSaveDynamicFields = "0"; isContainer = "1"; Profile = "GuiDefaultProfile"; HorizSizing = "right"; VertSizing = "bottom"; Position = "0 0"; Extent = "1024 768"; MinExtent = "8 2"; canSave = "1"; Visible = "1"; hovertime = "1000"; bitmap = "~/data/images/GUI/Shop.png"; wrap = "0"; new GuiScrollCtrl() { canSaveDynamicFields = "0"; isContainer = "1"; Profile = "GuiScrollProfile"; HorizSizing = "relative"; VertSizing = "relative"; Position = "528 456"; Extent = "392 136"; MinExtent = "8 2"; canSave = "1"; Visible = "1"; hovertime = "1000"; willFirstRespond = "1"; hScrollBar = "alwaysOff"; vScrollBar = "alwaysOn"; constantThumbHeight = "0"; childMargin = "0 0"; new GuiMLTextCtrl(itemText) { canSaveDynamicFields = "0"; isContainer = "0"; Profile = "GuiMLTextProfile"; HorizSizing = "relative"; VertSizing = "relative"; Position = "2 2"; Extent = "360 13"; MinExtent = "8 2"; canSave = "1"; Visible = "1"; tooltipprofile = "GuiDefaultProfile"; hovertime = "1000"; lineSpacing = "2"; allowColorChars = "1"; maxChars = "-1"; text = "Nothing selected."; }; };