Author Topic: Heed's learning to script!  (Read 1470 times)

I am learning to script, and I think I am do ok so far. I am trying to make a reload script since I thought it would be easy. Here is what I have so far.

Code: [Select]
function gunImage::onFire(%this,%obj,%slot)
{
%ammo + 1;
{
if(%ammo >= 8)
{
echo("lol")
%ammo = 8;
}
else
{
return;
}
}
};
Errors in console, What did I do wrong?

Dont know but it's better than anything i could make.

You forgot a semicolon (;) on the echo("lol");

The statement "%ammo + 1;" doesn't make sense. It's not a variable assignment, it's not setting an object variable so it'll be deleted at the end of the function.

Functions shouldn't end with a semicolon on the last bracket.

The spacing is horrible and makes it hard to read.

I seriously have a problem when I try to correct beginner scripts.

Also, it doesn't call the default firing function so your gun will do nothing at all.

Your brackets should be corrected. See other codes.

Study basic C/C++

Your brackets should be corrected. See other codes.
All spacing should be corrected. It doesn't work too.

Your brackets should be corrected.
His brackets are fine if you're allowed to create/remove scopes without an if/else/while/etc. statement in Torque.

Sorry for being new to scripting. How would I get this to work? I do not want a whole ammo mod, I just want it to echo that after it fires 8 times.

Basic Example.
Code: [Select]
package AmmoMod
{
function GunImage::onFire(%this,%obj,%slot)
{
%obj.client.Ammo += 1;
if(%obj.client.Ammo >= 8)
{
echo("Ammo is above or equal to 8.");
%obj.client.ammo = 0;
parent::onFire(%this,%obj,%slot);
}
else
{
echo(%obj.client.Ammo);
//Add parent::onFire(%this,%obj,%slot); here if you want them to still be able to shoot.
return;
}
}
};
activatePackage(AmmoMod);
« Last Edit: August 10, 2009, 03:17:46 PM by Kunit »

%obj.client.Ammo += 1; can be substituted with %obj.client.Ammo++; .

@Kunit
I can't see what you're doing in that script. It looks like it'll echo 1 to 7 for the first 7 times you shoot the weapon, without firing a bullet, then on the 8th go it'll fire a bullet and reset the ammo to 0? Then it'll not fire anything for another 7 shots and so on... Seems like something went wrong there.

@OP
There's also the problem that all guns would use the same ammo, but I guess that's something to think about once you've got the basics down.

Perhaps an ammo mod might not be the best place to start for your first script? It's quite advanced.
« Last Edit: August 10, 2009, 04:11:47 PM by Ephialtes »

@Kunit
I can't see what you're doing in that script. It looks like it'll echo 1 to 7 for the first 7 times you shoot the weapon, without firing a bullet, then on the 8th go it'll fire a bullet and reset the ammo to 0? Then it'll not fire anything for another 7 shots and so on... Seems like something went wrong there.

@OP
There's also the problem that all guns would use the same ammo, but I guess that's something to think about once you've got the basics down.

Perhaps an ammo mod might not be the best place to start for your first script? It's quite advanced.
I wasn't making a full ammo mod, just something to see what I could do with weapons, as I make alot of those.

-Snip-
Oh, you are right. I did not notice that. Just replace %obj.client.ammo >= 8  with %obj.client.ammo > 8.