cycling weapons
by Jeff Howard · in Torque Game Engine Advanced · 07/07/2009 (1:48 pm) · 7 replies
I've looked at and implemented a couple of resources on cycling weapons, but I'm running into an error message whose source I can't find. When I try to switch to another weapon, I can't change weapons, and I get this error.
scriptsAndAssets/server/scripts/inventory.cs (55): Unknown command onUse.
Object Cup(5745) Item -> ShapeBase -> GameBase -> SceneObject -> NetObject -> SimObject
I've traced the error to this function.
The line "return %data.onUse(%this);" appears to refer to a later function in inventory.cs.
I've looked closely at inventory.cs as well as the script for the cup weapon that I'm trying to switch to. Strangely, I can equip this weapon and others like it without problems, but when I try to switch to this weapon while holding another one I get the Unknown command::onUse error.
Can anybody see how I might resolve this error? I'd greatly appreciate the help in getting weapon cycling to work for me. I can also post any of my other scripts if they would help to fix the problem.
scriptsAndAssets/server/scripts/inventory.cs (55): Unknown command onUse.
Object Cup(5745) Item -> ShapeBase -> GameBase -> SceneObject -> NetObject -> SimObject
I've traced the error to this function.
function ShapeBase::use(%this,%data)
{
// Use an object in the inventory.
if (%this.getInventory(%data) > 0)
return %data.onUse(%this);
return false;
}The line "return %data.onUse(%this);" appears to refer to a later function in inventory.cs.
function ShapeBaseData::onUse(%this,%user)
{
// Invoked when the object uses this datablock, should return
// true if the item was used.
return false;
}I've looked closely at inventory.cs as well as the script for the cup weapon that I'm trying to switch to. Strangely, I can equip this weapon and others like it without problems, but when I try to switch to this weapon while holding another one I get the Unknown command::onUse error.
Can anybody see how I might resolve this error? I'd greatly appreciate the help in getting weapon cycling to work for me. I can also post any of my other scripts if they would help to fix the problem.
About the author
#2
For reference, here is the full set of functions for weapon cycling in inventory.cs.
07/07/2009 (2:07 pm)
Thank you for the reply. I am using Julian Ridley's weapon cycling resource. I think that the call to use() occurs at the end of the function in the linesif ( %newSlot != -1 )
%this.use( $weaponInSlot[%newSlot] );For reference, here is the full set of functions for weapon cycling in inventory.cs.
function ShapeBase::hasInventory(%this, %data)
{
return (%this.inv[%data] > 0);
}
function serverCmdCycleWeapon( %client, %data )
{
%client.getControlObject().cycleWeapon( %data );
}
function ShapeBase::cycleWeapon( %this, %data )
{
%slot = -1;
if ( %this.getMountedImage($WeaponSlot) != 0 ) {
%curWeapon = %this.getMountedImage($WeaponSlot).item.getName();
if(%curWeapon $= "Crossbow")
%slot = 0;
else if(%curWeapon $= "UberCrossbow")
%slot = 1;
else if(%curWeapon $= "RocketLauncher")
%slot = 2;
else if(%curWeapon $= "FragMine")
%slot = 3;
}
if ( %data $= "prev" ) {
// Previous weapon...
if ( %slot == 0 || %slot == -1 ) {
%i = $maxWeaponSlot;
%slot = 0;
}
else
%i = %slot - 1;
}
else {
// Next weapon...
if ( %slot == $maxWeaponSlot || %slot == -1 ) {
%i = 0;
%slot = $maxWeaponSlot;
}
else
%i = %slot + 1;
}
%newSlot = -1;
while ( %i != %slot ) {
if ( $weaponInSlot[%i] !$= ""
&& %this.hasInventory( $weaponInSlot[%i] )) {
// player has this weapon and it has ammo or doesn't need ammo
%newSlot = %i;
break;
}
if ( %data $= "prev" ) {
if ( %i == 0 )
%i = $maxWeaponSlot;
else
%i--;
}
else {
if ( %i == $maxWeaponSlot )
%i = 0;
else
%i++;
}
}
if ( %newSlot != -1 )
%this.use( $weaponInSlot[%newSlot] );
}
#3
It's a common error location that messes up a lot of people. There's a way of simplifying that whole section of code in one of the replies (#33 by Nick "Steve" DeChiara) for the resource, but requires writing another accompanying array to make it work. I ended up writing a helper function to make that even easier by creating both arrays for you. You can find that code plus a few other extras at replies #37 and #38
07/07/2009 (2:25 pm)
Ok, I use that resource -- double check your $weaponInSlot array $weaponInSlot[0] = "Crossbow"; $weaponInSlot[1] = "UberCrossbow"; $weaponInSlot[2] = "RocketLauncher"; $weaponInSlot[3] = "FragMine"; $maxWeaponSlot = 3;and make sure the names of your weapons match up (but with your weapon item names of course) with the lines that read
%curWeapon = %this.getMountedImage($WeaponSlot).item.getName(); if(%curWeapon $= "Crossbow") %slot = 0; else if(%curWeapon $= "UberCrossbow") %slot = 1; else if(%curWeapon $= "RocketLauncher") %slot = 2; else if(%curWeapon $= "FragMine") %slot = 3;I bet that what's happening is that you've misspelled a name in either the array or one of the strings in that group of if/else's. That or your array order is different from the slot#'s being passed by those same if/else's.
It's a common error location that messes up a lot of people. There's a way of simplifying that whole section of code in one of the replies (#33 by Nick "Steve" DeChiara) for the resource, but requires writing another accompanying array to make it work. I ended up writing a helper function to make that even easier by creating both arrays for you. You can find that code plus a few other extras at replies #37 and #38
#4
And here are the two errors that I get when I cycle to the next weapon and previous weapon.
scriptsAndAssets/server/scripts/inventory.cs (50): Unknown command onUse.
Object Cup(5744) Item -> ShapeBase -> GameBase -> SceneObject -> NetObject -> SimObject
scriptsAndAssets/server/scripts/inventory.cs (50): Unknown command onUse.
Object AnkhStaff(5747) Item -> ShapeBase -> GameBase -> SceneObject -> NetObject -> SimObject
What puzzles me is that the errors give the correct weapons from the right slots, i.e. the next weapon is the Cup and the previous weapon is the AnkhStaff because I cycle back from slot 0 to 2.
So I'm wondering why OnUse is listed as an unknown command when these objects call it. I may go back and try to simplify the function using Nick DeChiara's code and your helper functions.
Thanks very much for your help, and please do let me know if you see anything else that might be causing the error. I'm probably making a simple error but haven't yet found it.
07/07/2009 (3:44 pm)
Thanks very much for the detailed reply with all of the code improvements. I'm still trying to de-bug the arrays and not having much luck. I did find a few inconsistencies in the ways that I had named items, but the array and the if/then series appear to line up. Just in case I'm missing something from having looked too long at the array and if/then series, I'll include them here.$weaponInSlot[0] = "StormbringerSword"; $weaponInSlot[1] = "Cup"; $weaponInSlot[2] = "AnkhStaff"; $maxWeaponSlot = 2;
if ( %this.getMountedImage($WeaponSlot) != 0 ) {
%curWeapon = %this.getMountedImage($WeaponSlot).item.getName();
if(%curWeapon $= "StormbringerSword")
%slot = 0;
else if(%curWeapon $= "Cup")
%slot = 1;
else if(%curWeapon $= "AnkhStaff")
%slot = 2;
}And here are the two errors that I get when I cycle to the next weapon and previous weapon.
scriptsAndAssets/server/scripts/inventory.cs (50): Unknown command onUse.
Object Cup(5744) Item -> ShapeBase -> GameBase -> SceneObject -> NetObject -> SimObject
scriptsAndAssets/server/scripts/inventory.cs (50): Unknown command onUse.
Object AnkhStaff(5747) Item -> ShapeBase -> GameBase -> SceneObject -> NetObject -> SimObject
What puzzles me is that the errors give the correct weapons from the right slots, i.e. the next weapon is the Cup and the previous weapon is the AnkhStaff because I cycle back from slot 0 to 2.
So I'm wondering why OnUse is listed as an unknown command when these objects call it. I may go back and try to simplify the function using Nick DeChiara's code and your helper functions.
Thanks very much for your help, and please do let me know if you see anything else that might be causing the error. I'm probably making a simple error but haven't yet found it.
#5
Hmm, other than that I can't think of anything else except to make sure that you're calling the weapon Item name and not the weaponImage name.
07/08/2009 (12:00 pm)
I don't see any problems there. Check your item datablock names too.Hmm, other than that I can't think of anything else except to make sure that you're calling the weapon Item name and not the weaponImage name.
Quote:In that case I would be suspicious about slot 1 -- which is the "Cup" isn't it?
because I cycle back from slot 0 to 2
#6
@Michael: Thank you for all your advice, which helped me to de-bug the script so that I could eventually find my own error.
07/09/2009 (8:18 pm)
I figured it out. Just in case anyone runs into the same problem, here's what happened. I had given two instances of two weapons the same names as the datablocks that defined these weapons. In other words, I had created an instance of the "Cup" weapon in the mission named "Cup," and I had created an instance of the "AnkhStaff" weapon in the mission named "AnkhStaff." These naming errors on my part resulted in a linking problem which showed up in the console as an error that began "Namespace::unlinkClass - cannot unlink namespace parent linkage." Once I renamed the instances of these items to keep them from conflicting with the datablock names, I was able to cycle between weapons without problems.@Michael: Thank you for all your advice, which helped me to de-bug the script so that I could eventually find my own error.
#7
07/09/2009 (9:48 pm)
Great, glad you got it figured out.
Associate Michael Hall
Distracted...
It could be passing the wrong data name into it, or you may have the args out of order -- at a first guess.