Jump to content

Ammo Bar


Thirsty Panther
 Share

Recommended Posts

Hi Guys.

I need some help with my PlayerAmmoBar script.

This script is almost identical to Aggrors PlayerHealthBar script from his excellent video tutorials. Ive made some changes to it so that it graphically represents the amount of Ammo a player has left.

 

The problem I have is that in Aggrors script he is able to get the health information from scripts belonging to the player by using this:

 

self.player = self.entity:GetParent()

 

From there he creates a healthFactor from self.player.script.health / self.player.script.maxHealth (these scripts belong to the Player entity.

 

However for Ammo the information comes from self.Weapon.script.ammo / self.Weapon.script.clipsize. These scripts belong to the Autopistol.pfb which is attached to the player.

 

Now I've tried GetParent (doesnt work because it not the parent) GetChild, Findchild didnt seem to work for me either.

To confuse matters more I was looking at the FPS player script and found this line

self.weapon.entity:SetParent(self.camera)

So does that mean that the weapon is a child of the camera!?

 

Any help would be greatly appreciated.

Link to comment
Share on other sites

i don't know Aggrors script so i need a bit more information.

 

Where do you attach the scripts to?

 

I don't understand yet where you want to attach the PlayerAmmoBar.lua to?

 

 

self.weapon.entity:SetParent(self.camera)

So does that mean that the weapon is a child of the camera!?

 

Yes since you hold the weapon in front of the camera like in all FPS games. ;)

 

 

maybe you can give a small example project so i could look at it when i am home and understand better what your issue is?

Link to comment
Share on other sites

i don't know Aggrors script so i need a bit more information.

 

Where do you attach the scripts to?

 

I don't understand yet where you want to attach the PlayerAmmoBar.lua to?

 

 

 

 

Yes since you hold the weapon in front of the camera like in all FPS games. wink.png

 

 

maybe you can give a small example project so i could look at it when i am home and understand better what your issue is?

 

Ok I've created a basic map. It has the Player prefab. Autopistol prefab is loaded to the player. I've created a pivot which is also attached to the player and my Ammo Bar script is attached to this.

 

In its current state it stops with a nil value error (line 44). In the script I've commented where I think the problem lies (lines 14 to 17).

 

To see how the script looks ( but doesn't function ) go to line 44, commented it out and activate line 47.

 

Thanks in advance.

Ammo Bar Test.zip

Link to comment
Share on other sites

Hello Thirsty Panther,

 

sorry for the long delay. I just got to check your map and script.

 

I guess your script is not yet finished.

 

To be true i think i would do this differently.

Maybe include the Script into the player script.

 

 

But for now i guess i found your errors. Maybe that helps a bit.

 

Line 14: change

function Script:Start()

self.Weapon = self.camera:GetChild()

 

into

 

function Script:Start()

self.player = self.entity:GetParent()

self.Weapon = self.entity:FindChild("Pivot Ammo Bar")

 

 

--------------------

change line 37 from

local AmmoFactor = self.Weapon.script.ammo / self.Weapon.script.clipsize

 

into

local pistolEntity = self.player.script.camera:FindChild("Autopistol")

local AmmoFactor = pistolEntity.script.ammo / pistolEntity.script.clipsize

 

 

at least that should get you your informations.

  • Upvote 1
Link to comment
Share on other sites

Brilliant , thanks Beo6. I still have some fine tuning to do but at least I can get the information I need.

 

Would you mind explaining how the above scripts work.

 

Self.player=self.entity:get parent () - I'm good with.

Self.Weapon= self.entity:FindChild("pivotammobar") - so does this search all children of all entities for a child named "pivotammobar"?

 

Local pistol.enity= self.player.script.camera:FindChild("Autopistol")- so this one uses the self.player we defined earlier

To use the Camera script which has a child called Atopistol where we will find our data yes?

 

Local Ammofactor= PistolEntity.scriptAmmo/ pistol.entity.script.clipsize - I've changed this slightly to PistolEntity.scriptclipammo/

 

pistol.entity.script.clipsize so that it's a ratio of ammo in the clip compared to the ammo the clip can hold.

 

Greatly appreciate the help. Will post the completed script once I sort out a couple of minor things.

 

Cheers.

Link to comment
Share on other sites

Hello,

 

yes FindChild() searches for a child with that name. Note that it is not the prefab filename but the name you gave the entity in the editor.

 

Please also note that the Camera is not technically a child of the player which is the reason you would need to get it from the player script and FindChild() would not work there.

the Autopistol is again a child of the camera.

 

The Autopistol then has your ammo information etc. that you needed.

 

Not sure what you mean with "pistol.entity.script.clipsize so that it's a ratio of ammo in the clip compared to the ammo the clip can hold." as far as i have seen clipsize only holds the value how big the clip is it does not change with the ammo or anything. So i guess unless you want to show the player how much shots in his clip are before he needs to reload you don't really need that value.

 

Will you have only one weapon in your game? Because as i told this is not very flexible at the moment how it is done.

When you have more weapons this would break fast and would require a lot of additional code.

 

Or you could also use a for loop with CountChildren() and GetChild() so you don't need to know the name of the weapon.

 

Or maybe you could give every weapon the same name? But not sure what problems that will give later.

Link to comment
Share on other sites

Thanks Beo6 very helpful.

 

I hadn't given any thought to different weapons. I'm just happy to get this to work. To be honest I was going to give up on this until you were able to help out.

 

I'm amazed at Rick and Aggror ability to be able write scripts on the fly ( in their videos). For me it's lots of trawling forums, reading docs and asking for help. The longest journey and all that.

 

The ammo bar will work as you describe. It will start full and reduce as shots are fired. It will fill again when you reload. I'm adding a numerical counter for your total ammo amount. I've run into a problem with font size ( see bug report). Once I get this sorted I will post the final script.

 

Once more I am greatly indebted for all your help.

 

Cheers.

Link to comment
Share on other sites

I'm amazed at Rick and Aggror ability to be able write scripts on the fly ( in their videos). For me it's lots of trawling forums, reading docs and asking for help. The longest journey and all that.

 

I still trawl the forums and read the docs plenty. Don't feel bad :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...