Solved Script_Count ammo fired (weapon), not lose weapon, have reload animation

Question that is answered or resolved.

Glitch

Member
Hi guys :)

I have my character with a handgun weapon (type none) in which he fires bullets. Bullets are not set as ammo- as upon full depletion weapon will drop, which is not what I want. Weapon file does not have ammo count nor is it even a pickup weapon, character always will have handgun available via ''weaponframe.

He draws weapon from freespecial with ''weaponframe'' at end of animation, the weapon idle then plays, another freespecial (holster) will then go back to non weapon.

It is in the counting of bullets fired I have a problem:

I require the bullets to be counted via script and set as 15 bullets, upon the total ammo being depleted it should not fire any more bullets rather it should play a misfire/no bullet, then reload-cock animation in which set ammo ammo variable is fulled up. The reload-cock should also be a accessed by a freespecial.

I checked the forum and found @O'Illusionistas script used in his ''Brand'' character, however it is much too complex for me and has many other options/capabilities, more than what I require. All I need is the count, misfire and reload-cock capability.

I am using inline script for now to test, if it works I was going to create script file (with help from someone as I do not know what type script it should be)

I tried using some of it but it does not work as I need, the character fires a shot but upon the next shot he reloads then fires...as if the ammo is 1 and not 15

Post below has images and script as used my me:

@O'Illusionista or others, I would appreciate any assistance.
 
DTatCfQ.gif


Idle anim from which fire handgun freespecial can be activated, usually has multiple freespecial cancels, now set to singular freespecial just to test:
Code:
anim	idle3
	range	-300 430
	rangez	-100 200
	loop	1
	delay	30
	offset	66 138
	bbox	60 56 25 82
	@cmd	changeentityproperty getlocalvar("self") "aiflag" "blocking" 0
	cancel	0 9 0 f a4 freespecial28
	cancel	0 0 0 a2 freespecial23
	cancel	1 1 0 a2 freespecial23
	cancel	2 2 0 a2 freespecial23
	cancel	3 3 0 a2 freespecial23
	cancel	4 4 0 a2 freespecial23
	cancel	5 5 0 a2 freespecial23
	cancel	6 6 0 a2 freespecial23
	cancel	7 7 0 a2 freespecial23
	cancel	8 8 0 a2 freespecial23
	cancel	9 9 0 a2 freespecial23
	frame	data/chars/3dean/pcidle2a.gif
	frame	data/chars/3dean/pcidle2.gif
	frame	data/chars/3dean/pcidle2b.gif
	frame	data/chars/3dean/pcidle2.gif
	frame	data/chars/3dean/pcidle2a.gif
	frame	data/chars/3dean/pcidle2.gif
	frame	data/chars/3dean/pcidle2b.gif
	frame	data/chars/3dean/pcidle2.gif
	frame	data/chars/3dean/pcidle2a.gif
	frame	data/chars/3dean/pcidle2.gif


Script in below anim to set ammo total max, when reloaded and script in spawn anim as well:
Code:
anim	freespecial19
	/	- copy reload and cock /

	loop	0
	delay	15
	offset	66 138
	bbox	52 58 22 81
	@script
	void vSelf = getlocalvar("self");
	int ammo = 15;
	setglobalvar("ammoControl", ammo);
	@end_script
	frame	data/chars/3dean/preload.gif
	delay	15
	frame	data/chars/3dean/preload1.gif
	frame	data/chars/3dean/preload1a.gif
	sound	data/sounds/drawwep.wav
	delay	20
	frame	data/chars/3dean/preload2.gif
	delay	10
	frame	data/chars/3dean/preload4.gif
	delay	15
	frame	data/chars/3dean/preload3.gif
	sound	data/sounds/reload.wav
	delay	20
	frame	data/chars/3dean/preload4a.gif
	delay	10
	frame	data/chars/3dean/preload3.gif
	delay	10
	frame	data/chars/3dean/preload5.gif


Script in below anim to set ammo total max (in non weapon file)
Code:
anim	spawn
	loop	0
	delay	26
	offset	66 138
	bbox	54 58 28 81
	@script
	void vSelf = getlocalvar("self");
	int ammo = 15;
	setglobalvar("ammoControl", ammo);
	@end_script
	frame	data/chars/3dean/idle2b.gif


Script in below anim to check ammo count, if has no ammo then go to reload anim, else go to fire bullet anim:
Code:
anim	freespecial23
	/	a2 in idle3 - weak aim shot leftwards /

	loop	0
	delay	3
	offset	66 138
	bbox	49 55 36 84
	@script
   	void vSelf = getlocalvar("self");
       	if(getglobalvar("ammoControl") < 1){
	void self = getlocalvar("self");
	performattack(self, openborconstant("ANI_FREESPECIAL19"));
	} else {
	void self = getlocalvar("self");
	performattack(self, openborconstant("ANI_FREESPECIAL5"));
	}
	@end_script
	custknife	BulletP1l
	throwframe	1 16
	cancel	9 11 0 a freespecial25
	sound	data/sounds/gunshot.wav
	delay	3
	frame	data/chars/3dean/phfire1.gif


Script in below anim to deduct 1 bullet from total when fired:
Code:
anim	freespecial5
/	a2 - weak aim straight shot /
	
	loop	0
	delay	3
	offset	66 138
	bbox	49 55 36 84
	custknife	BulletP
	throwframe	1 16
	cancel	9 11 0 freespecial23
	sound	data/sounds/gunshot.wav
	frame	data/chars/3dean/phfire1.gif
    	@script
	void vSelf = getlocalvar("self");
        int ammoControl = getglobalvar("ammoControl");
        setglobalvar("ammoControl", ammoControl-1);
	@end_script
	delay	3
	frame	data/chars/3dean/phrecoil1.gif
	frame	data/chars/3dean/phfiren.gif
	frame	data/chars/3dean/phfire2.gif
	frame	data/chars/3dean/phfiren.gif
	frame	data/chars/3dean/phfire3.gif
	frame	data/chars/3dean/phfiren.gif
	frame	data/chars/3dean/phfire4.gif
	frame	data/chars/3dean/phfiren.gif
	frame	data/chars/3dean/phfire5.gif
	frame	data/chars/3dean/phIDLE2.gif
 
I have messed around with the script more, I changed the if else statement in the firing weapon animations to:

@script
  void vSelf = getlocalvar("self");
      if(getglobalvar("ammoControl") < 1){
void self = getlocalvar("self");
performattack(self, openborconstant("ANI_FREESPECIAL29"));
} else {
void vSelf = getlocalvar("self");
        int ammoControl = getglobalvar("ammoControl");
        setglobalvar("ammoControl", ammoControl-1);
        }
@end_script

(ANI_FREESPECIAL29 plays the misfire/ammo empty animation)

And then in the idle animations a cancel to below reload/cock animation which has script to full ammo to its value back again:

Code:
anim	freespecial19
	/	a4 in idles	- reload /
	loop	0
	delay	3
	offset	66 138
	bbox	52 58 22 81
	frame	data/chars/3dean/pcidle2b.gif
	delay	15
	frame	data/chars/3dean/preloada.gif
	delay	15
	frame	data/chars/3dean/preload1.gif
	frame	data/chars/3dean/preload1a.gif
	sound	data/sounds/drawwep.wav
	delay	20
	frame	data/chars/3dean/preload2.gif
	delay	10
	frame	data/chars/3dean/preload4.gif
	delay	15
	frame	data/chars/3dean/preload3.gif
	sound	data/sounds/reload.wav
	delay	20
	frame	data/chars/3dean/preload4a.gif
	delay	10
	frame	data/chars/3dean/preload3.gif
	@script
	void vSelf = getlocalvar("self");
	int ammo = 120;
	setglobalvar("ammoControl", ammo);
	@end_script
	delay	10
	frame	data/chars/3dean/preload5.gif

You would notice the high ammo value of 120, this is due to some strange behavior I cannot understand (most likely caused by something wrong I did :-X).... when I tried ammo value 5, it fired bullet once but upon second time firing it reloaded then fired a bullet  ??? ...
Upon increasing value to 25 it fired bullets 3 times then reloaded, value to 60 caused 6 times fire bullets then reload, until I put ammo value 120 as of now in which it fires 10 bullets then plays misfire/ammo empty anim (misfire anim added recently) and will refill ammo in the reload animation.

Working 99% perfectly, apart from the strange math calculation it does (120=10) :o which I  suspect is an error on my side :-X

edit: I will not mark topic as solved although it is working, just hoping someone can help me understand the unusual variable count, if not will mark as solved
 
I was going to suggest you to check the code I did for Brand. For sure it has more than you need, but it works like a charm.

Also, I see that you are using a SPAWN animation with code for a Weapon model, right?
This won't work because when you change from a model to another, the spawn animation is never played.
This is why I had to use "onmodelcopyscript" and is why your ammo count never reaches 15.
 
O Ilusionista said:
I was going to suggest you to check the code I did for Brand. For sure it has more than you need, but it works like a charm.


Your script for Brand character is great and saying it has more than I need is an understatement 😉

Thanks for sharing it Ilu.

I see that you are using a SPAWN animation with code for a Weapon model, right?
This won't work because when you change from a model to another, the spawn animation is never played.
This is why I had to use "onmodelcopyscript" and is why your ammo count never reaches 15.

I have the declaration of ammo value in the spawn anim of non weapon player character sheet, Dean is Player, dean1 is none
Script to declare ammo value is in Dean spawn anim, not in dean1

Odd though it seems to work kind of
 
Thanks for the assistance @O'Illusionista  :).
It is working after the changes made above, character fires 10 bullets only  and then the magazine is empty. only upon doing the cancel to reload and cock gun animation does the bullet count go back to 10.

However I'm still confused as to how the ammo is 10 despite being declared as 120 in reload and cock animation (still in weapon file) :o. (I want it to be 10 bullets, lol seems 120=10 in this script)

I will mark the topic as solved as it is working as intended, but should anyone have a correction or change that will explain/rectify the 10=120 ammo count I will welcome it.
 
I dunno if the frame wher you declare you inlinescript has any influence, but I always use inline script like that before the first frame.
You can add this script in your ondrawscript so it will print on the screen the ammo count:

Code:
drawstring(190,60,0,"Ammo:  "+getglobalvar("ammoControl"));

I have the declaration of ammo value in the spawn anim of non weapon player character sheet, Dean is Player, dean1 is none
I use onmodelcopy to make this, because if you include this code on the non-weapon player SPAWN anim, it will be executed only one time, because when you switch models using weapon, the spawn animation is ignored.

Doesn't makes much sense to declare the ammo value on a model which won't use it, if you think about it. This is what onmodelcopy works for.
 
You can add this script in your ondrawscript so it will print on the screen the ammo count:

Code: [Select]
drawstring(190,60,0,"Ammo:  "+getglobalvar("ammoControl

That's an excellent suggestion printing the variable so it can be seen. So I should have an ondrawscript I gather, okay will make one, do I add that line only to the file?... Or do I write @script inline ?

Whenever I created a script file I called the function with @cmd, how do I use drawstring script?

Doesn't makes much sense to declare the ammo value on a model which won't use it, if you think about it. This is what onmodelcopy works for.

Hmm so weapon model is not dependant in any way on the player model...makes sense, will look into this onmodelcopy script.
 
Aren't you using an ondrawscript already?

You can save this code and put it as ondrawscript to your character:

Code:
void main(){
drawstring(190,60,0,"Ammo:  "+getglobalvar("ammoControl"));
}

Or use it in your updated.c file, under the "in_level" openborvariant, so it will be displayed only during levels.
Code:
void main()
{

	if(openborvariant("in_level"))
	{
		drawstring(190,60,0,"Ammo:  "+getglobalvar("ammoControl"));
	}
}

You need to use an actively updated script (like updated. or ondrawscript) to print the message on the screen, since using drawstring lasts just ony cycke (or tick, if its gets easier to understand) and will be erased on the next one.

But using textojets, you won't need to use any updated script, just a textobject with an time on it:
msmalik681 said:
I have never written anything like this before so please correct anything that does not look right. This should cover everything I know about text objects.

settextobj(index, x, y, font, z, text, time)

index - integer value if multiple text objects have the same index they overwrite each other the order they are called.
x - integer value relative to the screen not the stage.
y - integer value also relative to screen.
font - integer to pick the font to display.
z - integer value to set the text layer.
text - string value the text to display should be in quotes like "this". you can also call script text variables to see what value they hold just remember to add ""+ before the variable if it is a integer or float value.
time - integer value that is optional to set how long the text displays if not defined the text will stay on screen.

example 1:
Code:
settextobj(0, 120,  80, 4, 999999999, "msmalik681"); // this one is permanent.
settextobj(1, 120,  180, 5, 999999999, "is cool", 1000); //this one is displayed for 10 seconds.
settextobj(2, 120,  280, 5, 999999999, getentityproperty(getplayerproperty(0, "entity"), "name")); //this will display player one's name.
settextobj(2, 120,  380, 5, 999999999, ""+getentityproperty(getplayerproperty(0, "entity"), "mp")); //this will display player one's mp.

gettextobjproperty(index, property)
changetextobjproperty(index, property, value)

list of properties:
"font"
"text" *warning this will crash the engine if there is no valid text object first check one of the other values to confirm there is a valid text object.
"time"
"x"
"y"
"z"

example 2:
Code:
//these will modify the text objects from example 1
changetextobjproperty(0, "text", "Malik"); //change the displayed text in text object 0.
changetextobjproperty(1, "font", gettextobjproperty(0, "font")); //get the font from text object 0 and apply to text object 1.
 
Aren't you using an ondrawscript already?

You can save this code and put it as ondrawscript to your character:

Code: [Select]
void main(){
drawstring(190,60,0,"Ammo:  "+getglobalvar("ammoControl"));
}

Or use it in your updated.c file, under the "in_level" openborvariant, so it will be displayed only during levels.
Code: [Select]
void main()
{

if(openborvariant("in_level"))
{
drawstring(190,60,0,"Ammo:  "+getglobalvar("ammoControl"));
}
}

You need to use an actively updated script (like updated. or ondrawscript) to print the message on the screen, since using drawstring lasts just ony cycke (or tick, if its gets easier to understand) and will be erased on the next one.

Nope, the only script I'm using for character is animation script which has your runstop script.
I will research the other scripts and learn what their usages are, thanks for telling me about updatescript and ondrawscript :)

I will take an updatescript from another mod and add the in level drawstring part.

But it seems I will not be showing the ammo count in game (I have inserted the drawstring) as it shows 120 (full magazine) and upon firing it subtracts 12 bullets, instead of 10 (full magazine) and subtracting 1 bullet upon firing.

Or I have another idea to make use of the current ammo count situation, What if I keep the 120 ammo but instead of subtracting 1 upon firing I will instead subtract 1 upon reloading, this will mean that 120 is the total ammo and each reload takes 12 bullets away... The count to get to 12 bullets and empty animation (signifies you need to reload) will be another variable that I will create that checks if 12 bullets have been fired..

If that does not correct the ammo count then I will not show ammo count at all (it is only there to add realism and a part of ensuring the handgun is not spammed, besides counting the bullets fired is what you would have to do in reality as there is no HUD in real life ;D)


You are most helpful @O'Illusionista, I will be using the information you have provided me with to use other preexisting scripts (so many out there to study)

 
Back
Top Bottom