Weapon::onUse
by Jeremy Jenkins · in Torque Game Engine · 08/01/2007 (6:57 pm) · 20 replies
I have recently been trying to add dual wielding into my game. I am using one of the dual wielding resources Here
There's nothing wrong with the resource, but I think I am doing something wrong with the weapon::onUse method in weapon.cs
If you look in the weapon::onPickup method you will see a call to weapon::onUse like this, %shape.use(%this)
(I know most of you probably knew that but I thought it couldn't hurt)
My problem is that I can't figure out why .use is called instead of .onUse
Also if I create a similar weapon method with a different name(I even copied the onUse method once and renamed it onSecondaryUse like in the resource)the engine can't find it. It doesn't matter if I call .SecondaryUse or .onSecondaryUse
If anyone knows why this is happening I would really appreciate an answer. Thanks in advance
P.S. I know this post was probably confusing, sorry about that :)
There's nothing wrong with the resource, but I think I am doing something wrong with the weapon::onUse method in weapon.cs
If you look in the weapon::onPickup method you will see a call to weapon::onUse like this, %shape.use(%this)
(I know most of you probably knew that but I thought it couldn't hurt)
My problem is that I can't figure out why .use is called instead of .onUse
Also if I create a similar weapon method with a different name(I even copied the onUse method once and renamed it onSecondaryUse like in the resource)the engine can't find it. It doesn't matter if I call .SecondaryUse or .onSecondaryUse
If anyone knows why this is happening I would really appreciate an answer. Thanks in advance
P.S. I know this post was probably confusing, sorry about that :)
About the author
#2
Creating own callbacks won't have much sense unless you implement them in the code.
The resource you mention for example, implements onUseSecondary and therefor UseSecondary, so you just tried to use the wrong commands.
08/02/2007 (6:30 am)
OnXY is a callback. You never call that one yourself, the engine will call them for you normally.Creating own callbacks won't have much sense unless you implement them in the code.
The resource you mention for example, implements onUseSecondary and therefor UseSecondary, so you just tried to use the wrong commands.
#3
edit: now that I looked at the resource I see that the resource calls it: %shape.onUse(%this); and not %shape.use(%this); like you specified.
08/02/2007 (6:33 am)
.use(%weapon) makes you change weapons to whatever %weapon is specified it is not used to shoot the weapon.edit: now that I looked at the resource I see that the resource calls it: %shape.onUse(%this); and not %shape.use(%this); like you specified.
#4
Marc Schaerer: Thanks for the answer. I still can't get what I want to work though. I was using a heavily modified form of that resource so I decided to start over with a fresh stock 1.5.1 engine. The only changes I have made to the whole thing is in weapon.cs
I renamed weapon::onUse to weapon::onSecondaryUse and I changed shape.use(%this) to shape.secondaryuse(%this) but it won't work
Is that what you mean by using the wrong commands?
08/02/2007 (7:45 am)
Mb: Thanks for the reply. I understand that .use(%this) is meant to change weapons, I just didn't understand why .use was called instead of .onUse before.Marc Schaerer: Thanks for the answer. I still can't get what I want to work though. I was using a heavily modified form of that resource so I decided to start over with a fresh stock 1.5.1 engine. The only changes I have made to the whole thing is in weapon.cs
I renamed weapon::onUse to weapon::onSecondaryUse and I changed shape.use(%this) to shape.secondaryuse(%this) but it won't work
Is that what you mean by using the wrong commands?
#5
And calls %shape.onUse(%this); which is a function below that the author created:
You also changed onUseSecondary to usesecondary. You might want to review how functions work.
TorqueScriptFunctions
08/02/2007 (8:43 am)
You should probably copy/paste the resource and modify it after you get it working. Copy the code and paste it into 'wordpad' (if you have windows), and then copy paste that into your .cs file. The reason I say this is because the function looks like this:function Weapon::onPickup(%this, %obj, %shape, %amount,%secondary)
{
// The parent Item method performs the actual pickup.
// For player's we automatically use the weapon if the
// player does not already have one in hand.
%secondaryimage = %shape.getMountedImage($dualWeaponSlot);
%image = %shape.getMountedImage($WeaponSlot);
if (Parent::onPickup(%this, %obj, %shape, %amount)) {
serverPlay3D(WeaponPickupSound,%obj.getTransform());
if (%secondary == 0){
if (%shape.getClassName() $= "Player") {
if (%image == 0) {
%amount = %shape.getInventory(%image.ammo);
%shape.throw(%image.ammo,%amount);
%shape.throw(%image.item);
}
%shape.onUse(%this);
}
}
else{
if (%shape.getClassName() $= "Player"){
if (%secondaryimage == 0) {
%amount = %shape.getInventory(%secondaryimage.ammo);
%shape.throw(%secondaryimage.ammo,%amount);
%shape.throw(%secondaryimage.item);
}
%shape.onUseSecondary(%this);
}
}
}
}And calls %shape.onUse(%this); which is a function below that the author created:
function Weapon::onUse(%data,%obj)
You also changed onUseSecondary to usesecondary. You might want to review how functions work.
TorqueScriptFunctions
#6
I haven't added the changes for the resource in these scripts yet. Now %shape.use(%this) works and %shape.onUse doesn't work correctly even though it is defined as weapon::onUse
(I suggest you look at it or try it yourself)(I would also read Marc's post)
My problem is when I create a new function(its actually the onUse function name onSecondaryUse like in the resource)it won't work
It doesn't matter if I use shape.usesecondary or shape.onusesecondary
Do you see my problem?
Thanks again
08/02/2007 (11:16 am)
Mb: Thank you for continuing to try to help me. With my fresh copy of torque, the code in the weapon.cs looks like this:function Weapon::onUse(%data,%obj)
{
// Default behavoir for all weapons is to mount it into the
// this object's weapon slot, which is currently assumed
// to be slot 0
if (%obj.getMountedImage($WeaponSlot) != %data.image.getId()) {
serverPlay3D(WeaponUseSound,%obj.getTransform());
%obj.mountImage(%data.image, $WeaponSlot);
if (%obj.client)
messageClient(%obj.client, 'MsgWeaponUsed', '\c0Weapon selected');
}
}
function Weapon::onPickup(%this, %obj, %shape, %amount)
{
// The parent Item method performs the actual pickup.
// For player's we automatically use the weapon if the
// player does not already have one in hand.
if (Parent::onPickup(%this, %obj, %shape, %amount)) {
serverPlay3D(WeaponPickupSound,%obj.getTransform());
if (%shape.getClassName() $= "Player" &&
%shape.getMountedImage($WeaponSlot) == 0) {
%shape.Use(%this);
}
}
}This code is what comes with torque and it works greatI haven't added the changes for the resource in these scripts yet. Now %shape.use(%this) works and %shape.onUse doesn't work correctly even though it is defined as weapon::onUse
(I suggest you look at it or try it yourself)(I would also read Marc's post)
My problem is when I create a new function(its actually the onUse function name onSecondaryUse like in the resource)it won't work
It doesn't matter if I use shape.usesecondary or shape.onusesecondary
Do you see my problem?
Thanks again
#7
"%shape.onSecondaryUse(...)". You have to call it what it's named. That's the only thing I see wrong in your post.
EDIT:
In other words if I defined this function:
function myFunction()
{
echo("HI");
}
To call that function I would type:
myFunction();
and it would write to the console: HI
I'll do the resource when I get home from work and see if it works.
08/02/2007 (11:57 am)
Actually no unfortunately I don't see your problem, except that you're saying that you are calling a function named: "onSecondaryUse" with "usesecondary" or "onusesecondary. The problem is that you should be calling that function (which is named onSecondaryUse) like so: "%shape.onSecondaryUse(...)". You have to call it what it's named. That's the only thing I see wrong in your post.
EDIT:
In other words if I defined this function:
function myFunction()
{
echo("HI");
}
To call that function I would type:
myFunction();
and it would write to the console: HI
I'll do the resource when I get home from work and see if it works.
#8
Thank you again
P.S. I would hate for you to waste your time puting in a resource that you won't use, but I would really appreciate it if you tried changing the name of the weapon::onUse function and the shape.use(%shape) in weapon.cs if you had the chance.
08/02/2007 (12:38 pm)
I wish lower case names were my problem but I put onSecondaryUse or SecondaryUse in my scripts, I just posted usesecondary and onusecondary like that because I'm lazy. :) (sorry)Thank you again
P.S. I would hate for you to waste your time puting in a resource that you won't use, but I would really appreciate it if you tried changing the name of the weapon::onUse function and the shape.use(%shape) in weapon.cs if you had the chance.
#9
08/02/2007 (1:08 pm)
I don't mind. Which version of tge are you using? I'll use the same version as long as it's 1.4 or higher... I don't have 1.3.
#11
08/03/2007 (3:07 pm)
Anything?
#12
08/04/2007 (5:07 pm)
Last bumb before i give up, mb are you out there?
#13
08/04/2007 (7:27 pm)
Haven't had time to try it out yet sorry.
#14
08/05/2007 (12:30 pm)
Thats ok, I just thought you might have forgotten.
#15
08/07/2007 (2:22 pm)
What are the errors you are getting in your console file, it would even be nice if you could email me the entire log. I should be able to tell what the problem is from it.
#16
Sorry for the wait. Problems with my internet, so if you don't get my email soon please tell me.
08/08/2007 (5:32 pm)
Email sent! :)Sorry for the wait. Problems with my internet, so if you don't get my email soon please tell me.
#17
08/09/2007 (12:45 pm)
Did you get it?
#18
we can see this in this line
Unable to find object: '' attempting to call function 'getName'
the problem isn't the function name, but the variable that is preceding the function.
ex. %obj.func();
if %obj is invalid it will say that it can't find that function because %obj is invalid and therefore has no member functions if you can follow that.
I'll see if I can find where one of these variables is becoming invalid.
08/09/2007 (10:03 pm)
Ah yes I've been quite busy lately but I think I'll be able to fix this. One of the variables is wrong.we can see this in this line
Unable to find object: '' attempting to call function 'getName'
the problem isn't the function name, but the variable that is preceding the function.
ex. %obj.func();
if %obj is invalid it will say that it can't find that function because %obj is invalid and therefore has no member functions if you can follow that.
I'll see if I can find where one of these variables is becoming invalid.
#19
08/09/2007 (10:34 pm)
I put it into a flat copy of Torque and it worked fairly well. I had some trouble firing the other gun but I got that worked out. Why don't you try to put the full thing in over again and if it still doesn't work either I'll send you my files or you can send me your's and we'll get this worked out.
#20
08/10/2007 (7:57 am)
Ok I'll give it another shot, thank you again.
Torque Owner Jeremy Jenkins