having trouble with changing weaps (code)
Blitz3D Forums/Blitz3D Programming/having trouble with changing weaps (code)| 
 | ||
| Hello As the topic is self explanitory, how can I make the cycling of weapons properly? Im expirimenting with some commands but it doesn't work at all :( Here is my piece of code if anyone wants to even read it xd: ;keys for weap slots weap_0=KeyDown(2) weap_1=KeyDown(3) weap_2=KeyDown(4) weap_3=KeyDown(5) weap_4=KeyDown(6) weap_5=KeyDown(7) weap_6=KeyDown(8) weap_7=KeyDown(9) weap_8=KeyDown(10) ;weap slots ;shotgun If weap_0 Then ShowEntity weap_shotgun If weap_0 Then HideEntity weap_sshotgun If weap_0 Then HideEntity weap_smg If weap_0 Then HideEntity weap_ssmg If weap_0 Then HideEntity weap_rl If weap_0 Then HideEntity weap_gl If weap_0 Then HideEntity weap_light If weap_0 Then HideEntity weap_bfg And the same sequence is repeated to the other weapons, and when I launch a test build, and then I press one of the assigned keys, the models dont dissapear as they should and the model that I want DOES show up, but its in the mess of polygons xd and Id like to know if my mouse script is good enough,because I saw demos better than mines and I was thinking "With these low poly models, and it gets me 20-15 fps???? Thats not possible..." Here it starts: ;mouse look mxs# = mxs# + MouseXSpeed() mys# = mys# + MouseYSpeed() If mys# > 80 Then mys# = 80 If mys# < -80 Then mys# = -80 RotateEntity fps_cam,mys#,-mxs,0 MoveMouse 320,240 If anyone would kindly help me, I will thank you very very much :) | 
| 
 | ||
| About the selection of the weapon you could use a variable to store the model name (a string or a constant) and one variable to store the weapon reference/mesh : global HandJoint = findchild(CharacterMesh,"Hand") global WeaponMesh = createmesh() global WeaponModel% const CGun% = 1 const CRifle% = 2 const CShotgun% = 3 const CGrenade% = 4 WeaponModel = CGun ChangeWeapon() If( keyhit(20)=1 ) WeaponModel = WeaponModel - 1 If( WeaponModel < 1 ) WeaponModel = 1 endif ChangeWeapon() else if( keyhit(21)=1 ) WeaponModel = WeaponModel + 1 If( WeaponModel > 4 ) WeaponModel = 4 endif ChangeWeapon() endif function ChangeWeapon() freeentity(WeaponMesh) If( WeaponModel = CGun ) WeaponMesh = copyentity(GunMesh) else if( WeaponModel = CRifle ) WeaponMesh = copyentity(RifleMesh) else if( WeaponModel = CShotgun ) WeaponMesh = copyentity(ShotgunMesh) else if( WeaponModel = CGrenade ) WeaponMesh = copyentity(GrenadeMesh) endif PositionRotateEntityLikeOtherEntity(WeaponMesh,HandJoint) SetEntityAsChildOfOtherEntity(WeaponMesh,HandJoint) end function About the first person view and controls and turns moves, see : http://www.blitzbasic.com/codearcs/codearcs.php?code=2933 | 
| 
 | ||
| double post.. | 
| 
 | ||
| My Blitz3d syntax is a bit rusty but in pseudo code you could perhaps do something like this. Have another variable for weapon and use keyhit instead of keydown. in every game loop 
weaponchange =0
If KeyHit(1) Then weaponchange = 1
If KeyHit(2) Then weaponchange = 2
If KeyHit(3) Then weaponchange = 3
If KeyHit(4) Then weaponchange = 4
If KeyHit(5) Then weaponchange = 5
If KeyHit(6) Then weaponchange = 6
If weaponchange> 0 Then 
	HideEntity(weapon)
          Select weaponchange
            	Case 1
              		weapon = weap_shotgun
            	Case 2
             		weapon = weap_smg
            	Case 3
            		weapon = weap_ssmg
       	End Select
	ShowEntity(weapon)
EndIf
 | 
| 
 | ||
| Ok thanks for the suggestions :) Ill try it out now | 
| 
 | ||
| There's not enough of your code here to pull any conclusions, but perhaps for the weapons selection you need to use FlushKeys? And are perhaps use KeyHit instead of KeyDown? | 
| 
 | ||
| KeyDown + 2-second check using millisecs + holding down the same key = <3 (Your answer ) | 
| 
 | ||
| Ok the code in one of the suggestions work, but there's another problem :/ When I understood the code 'n stuff, I've noticed that in the weap change code, it hides one entity, and I've got 8 seperate entities for each weapon, and I was trying to somehow group them into one big entity with calls such as for eg. 0 would be the shotgun, 1 would be the double barrel shotgun and so on and so on, and so I've read the Command Reference and there's no function of that sorts :/ Well maybe there is but I just don't see it? So is there a way I can make the weaps into one group with each seperate call ? | 
| 
 | ||
| Oh, and I went for KronosUK's code, because to me it's much simpler, and I want the code of the engine to be short and easy understandable at once | 
| 
 | ||
| Maybe you can make something like this : 
; load weapons
Dim weapon(10)
weapon(1) = LoadMesh("handgun.b3d")
weapon(2) = LoadMesh("shotgun.b3d")
weapon(3) = LoadMesh("smg.b3d")
etc...
For i = 1 to 10
	HideEntity(weapon(i)) ; hide all weapons
Next
currentweapon = 1 ; handgun for a start
; in your loop
weaponchange = currentweapon
If KeyHit(1) Then weaponchange = 1
If KeyHit(2) Then weaponchange = 2
If KeyHit(3) Then weaponchange = 3
If KeyHit(4) Then weaponchange = 4
If KeyHit(5) Then weaponchange = 5
If KeyHit(6) Then weaponchange = 6
if weaponchange <> currentweapon
	currentweapon = weaponchange
	For i = 1 to 10
		HideEntity(weapon(i)) ; hide all weapons
	Next
	ShowEntity(weapon(currentweapon)) ; show current weapon
endif
that's the idea, you will have to tweak for your needs. | 
| 
 | ||
| In Kronos UK code it hides the currently showing entity and then changes the reference that variable points to and shows it. (Just as an explanation). I think you could benefit from some simple programming exercises if you wanted to get better at understanding how it all works. | 
| 
 | ||
| Well some lessons for me would be usefull, but I don't have that much time to take lessons about BB3D, because I got alot of work with high school goin on T,T but Flankers code seems to work out, I tweaked  in my prefrences, but the compiler gives me an error at HideEntity(weap(i)), hence before that line there's For i = 1 to 8 , and I've got 8 entities, and as I understand, i represents each and every weap entity, instead of 'i' .... Something's not right :/ | 
| 
 | ||
| Aww great ... now the compiler gives me an error about the weapons itself, omg I'm not getting this xd ok here's my code: Dim weap(8) weap(1)=LoadMesh("v_shotgun.3ds"):ScaleEntity weap(1),0.1,0.1,0.1:RotateEntity weap(1),-90,0,0 and the LoadMesh sequence repeats for the following 8 entities and it gives me an error with the db-shotgun .... | 
| 
 | ||
| I mean the first one loads fine but there's a conflict with the second entity :/ | 
| 
 | ||
| Okay let me think another way out for a while .. maybe I might come up with some kind of solution , but I doubt it :/ | 
| 
 | ||
| If you are in high school (as a student not a teacher) then you are at an age in life where you have the most time you will ever have to learn new skills..... |