Changing mesh by button press

Blitz3D Forums/Blitz3D Beginners Area/Changing mesh by button press

Chad(Posted 2006) [#1]
I've got a quick question..

How can I quickly change a model to another model by pressing a button? I would like to think it would be simple like:

LoadMesh("yadadada.b3d")

While Not KeyHit(1)
If MouseHit(1) Then LoadMesh("blahblah.b3d")
EndIf


But it's not, so that's where I'm stuck.. Is there an example in B3D that I missed?

Thanks,
Chad


mindstorms(Posted 2006) [#2]
I would load both meshes at the beginning of the program and then hide one of them. Then I would have 3 variables for position and when the mousehit is activated, show the hidden mesh and hide the shown mesh and then place the newly shown mesh at those coordinates.


Chad(Posted 2006) [#3]
Uhhuh.. So if I understand you then this is what the code should look right correct??

yadda = LoadMesh("yadda.b3d")
blah = LoadMesh("blah.b3d")
SetAlpha yada,100
SetAlpha blah, 0

While Not KeyHit(1)
If MouseHit(1) Then SetAlpha,blah,100,Then SetAlpha,yada,0


On a side note, I'm trying this out on Mike's TPS 3rd person engine, so about the variables for position I don't know what to do about that.. If that's not the correct code, could someone help me with the correct code? I like to code what I think it is first and then learn by people fixing or pointing at what's wrong.

Thanks,
Chad


Stevie G(Posted 2006) [#4]
Is setalpha not a max command ... it's cetainly not a b3d one I know of?

yadda = LoadMesh("yadda.b3d")
blah = LoadMesh("blah.b3d")
hideentity blah

While Not KeyHit(1)
  If MouseHit(1) Then 
    hideentity yadda
    showentity blah
  endif
  



n8r2k(Posted 2006) [#5]
Should be:
yadda = LoadMesh("yadda.b3d")
blah = LoadMesh("blah.b3d")
EntityAlpha yada,1
EntityAlpha blah, 0

While Not KeyHit(1)
If MouseHit(1) 
      EntityAlpha,blah,1
      EntityAlpha,yada,0
EndIf
Wend 

its not setalpha its EntityAlpha, and the range is 0 to 1, not 0 to 100, you also need wend at the end, and rearrange your if...then statement

[edit] you could also use hideentity, I am not sure really which one is faster/better. There was a big arguement a while ago about this, i think it depends on what your doing. You should look for this thread I am speaking of.

[edit]
look here: http://66.249.93.104/search?q=cache:M2eEYhYXhGQJ:www.blitzbasic.com/archive/posts.php%3Ftopic%3D17199+hideentity+entityalpha+faster+blitzbasic&hl=en&gl=uk&ct=clnk&cd=1 and here


GfK(Posted 2006) [#6]
EntityAlpha might not be the best solution for you. Take a look at HideEntity/ShowEntity.

The difference between EntityAlpha and Hide/ShowEntity - with EntityAlpha, you can still do collisions with invisible objects. If you hide an entity instead, then collisions for that object will be disabled.

Which you use depends on what you want to achieve.


Chad(Posted 2006) [#7]
Thanks guys, this works :)


Chad(Posted 2006) [#8]
One last thing, if I want to switch it back to the original model again by pressing the same button, how do I do that? Just rewrite the thing but switch the entity's alpha command?


mindstorms(Posted 2006) [#9]
Use a variable that determines which model to show. If the key is pressed, toggle it between 1 and 0. Then check the variable, if it is one set the entity to this. If it is two than set the entity to that.


Chad(Posted 2006) [#10]
I don't exactly know how to use variables.. but here's my example code..

yadda = LoadMesh("yadda.b3d")
blah = LoadMesh("blah.b3d")
EntityAlpha yada.yada1,1
EntityAlpha blah.blah1 0

While Not KeyHit(1)
If MouseHit(1) 
      EntityAlpha,blah.blah1,1
      EntityAlpha,yada.yada1,0
EndIf
Wend 


I just don't understand how to use variables :-\ Could you give me an example please?

Thanks,
Chad


mindstorms(Posted 2006) [#11]
which_one = 1
yada = LoadMesh("yadda.b3d")
blah = LoadMesh("blah.b3d")
EntityAlpha yada,1
EntityAlpha blah 0

repeat

if mousehit(1) then 
	which_one = which_one+1
	select (which_one mod 2)
		case 1:
			EntityAlpha yada,1
			EntityAlpha blah 0
		case 0:
			EntityAlpha yada,0
			EntityAlpha blah,1
	End Select
endif
until keydown(1)


..You should look at the docs for some help on variables and other important BASIC ideals.


Chad(Posted 2006) [#12]
This conflicts with my function of updatecamera for some reason.. I don't know exactly where I messed up so I could show the code if you think you guys should look at it and see?

In the first post I did ask if there were examples or something I could take a look at myself, but I guess there is nothing but the docs for Blitz3d.

;*************
Type Timer
	Field start
	Field timeOut
	Field vel_start#;=90
	Field vel_down#;=-2
End Type


;System
Global AppFPS =45
Global screen_width% = 1024
Global screen_height% = 768
Global screen_halfwidth% = screen_width/2
Global screen_halfheight% = screen_height/2
Global camHeight#=4.5  ; default height of the camera behind player
Global camDist#=-7 ; default distance of the camera behind player
Global dest_xang# 
Global dest_yang# 
Global pl_gravity#=-.5
Global pl_jump=0
Global Jump_Move#=0
Global Move_For_Back#
Global Move_Left_Right#
Global Move_Slow#=.1
;CONST
Const C_PLAYER=1, C_LEVEL=2

AppTitle "mo"
HidePointer

;Screen
Graphics3D screen_width,screen_height,32,1
SetBuffer BackBuffer()

CreateWorld()

;CAMERA
camera =CreateCamera()

;Player Pivot
plpivot=CreatePivot()
EntityType plpivot, C_PLAYER
;EntityRadius plpivot,1.8,3
PositionEntity plpivot, 0, 3,0

;Player, now a cube but you can change for a nice model
;Player=CreateCube(plpivot)
which_one = 1

player=LoadMesh("C:\Documents and Settings\Chad\Desktop\asay\man.b3d",plpivot)
ScaleEntity player,.08,.08,.08
MoveEntity player,0,1,0

blah=LoadMesh("C:\Documents and Settings\Chad\Desktop\asay\blah.b3d",plpivot)
ScaleEntity blah,.08,.08,.08
MoveEntity blah,0,1,0

	EntityAlpha player,1
	EntityAlpha blah,0

Repeat

If MouseHit(1) Then
	Which_one = which_one+1
	Select (whichone Mod 2)
		Case 1:
			EntityAlpha player,1
			EntityAlpha blah,0
		Case 2:
			EntityAlpha blah,1
		EntityAlpha player,0

	End Select
EndIf
Until KeyDown(1)

;I'm using 2 pivots for camera controlling
;Pivot for the camera controlling
;One Front of the player,
FrontPivot=CreatePivot(plpivot)
PositionEntity Frontpivot,0,4,10

;One behind player
;This pivot controlls the collisions and "finds" the right position of the camera behind player
Backpivot=CreatePivot(plpivot)
EntityRadius Backpivot,1
EntityType BackPivot, C_PLAYER


Collisions C_PLAYER, C_LEVEL ,2,2


; -------------
; Main loop
; -------------
UpdateWorld
RenderWorld

FramePeriod = 1000 / AppFPS
FrameTime = MilliSecs () - FramePeriod

While Not KeyHit(1)
 
; --------------------------
	; Frame rate limiting
	; --------------------------
	Repeat
		FrameElapsed = MilliSecs () - FrameTime
	Until FrameElapsed

	FrameTicks = FrameElapsed / FramePeriod
	FrameTween# = Float (FrameElapsed Mod FramePeriod) / Float (FramePeriod)

	; -----------------------
	; Update tweening
	; -----------------------
	For FrameLimit = 1 To FrameTicks
		If FrameLimit = FrameTicks Then
			CaptureWorld
		EndIf
		FrameTime = FrameTime + FramePeriod
		
              UpdateCamera(plpivot,MouseXSpeed(),MouseYSpeed(), Frontpivot, Backpivot, Camera)
              UpdatePosition(plpivot)
              UpdateWorld

      Next
     
  RenderWorld FrameTween
  ; Crosshair :)
  Text screen_halfwidth%, screen_halfheight%, "+"
  
  Flip 0

End


Function updatecamera(ent,mxs#, mys#, pivot1, pivothatso, camera)
;ent : Player pivot
;mxs: MouseXmove
;mys: MouseYmove
;pivot1: pivot before player
;pivothatso: pivot behind player
;Camera: 

    MoveMouse screen_halfwidth%, screen_halfheight%
  
    ; Limit of the "look up", "look down"  
    If dest_xang + mys > 120 Or dest_xang + mys < -45 Then ;
        mys=0 
    EndIf


      dest_xang# = dest_xang + mys

      ; When the player looks up, the front pivot moves UP, when looks down the back pivot moves down
      TranslateEntity pivot1,0,-MYS#*.15,0
 
     angle#=  Sin(EntityPitch(camera))*6
      tempkkammag#=camheight + (angle*.8)
      tempkkamtav#=   camdist  + Abs( angle) 

      dest_yang# = dest_yang - (mxs*.5)
 
       RotateEntity ent, 0, dest_yang,0
     
        dx#=(EntityX(pivothatso,True)-EntityX(camera,True)) *.1
	dy#=(EntityY(pivothatso,True)-EntityY(camera,True)) *.9
	dz#=(EntityZ(pivothatso,True)-EntityZ(camera,True)) *.1
	
	TranslateEntity camera,dx,dy,dz
        PointEntity camera,pivot1
	PositionEntity pivothatso,0,0,0
	ResetEntity pivothatso

       	PositionEntity pivothatso,0,tempkkammag,tempkkamtav
End Function


Function UpdatePosition(ent)
;MOVEMENT
If pl_jump=0
  If KeyDown(200) Or KeyDown(17) Then 
	Move_For_Back=.3
  End If
  If KeyDown (208) Or KeyDown(31) Then
	Move_For_Back=-.2
  End If
  If KeyDown(203) Or KeyDown(30) Then 
      Move_Left_Right=-.2
  End If
  If KeyDown (205) Or KeyDown(32) Then
       Move_Left_Right=.2
  End If
EndIf

If ( MouseHit(2)  Or KeyHit(57))  And pl_jump=0
     t.Timer = JumpTimer (1000)
    ;When jumps slides in the air doesn't stop quickly
     Move_Slow#=.999
     pl_jump=1
EndIf

If jump_timeout()=1
    pl_jump=0
   ;when goes stops quickly
    Move_Slow#=.1
EndIf

   MoveEntity ent, Move_Left_Right, Jump_Move, Move_For_Back
  TranslateEntity ent,0,pl_gravity,0

  ;Smooth movement
  move_left_right = move_left_right * move_slow
  move_for_back = move_for_back  * move_slow

End Function

Function JumpTimer.timer (timeOut)
	t.Timer = New Timer
	t\start   = MilliSecs ()
	t\timeOut = t\start + timeOut
        t\vel_start#=90
        t\vel_down#=-2
	Return t
End Function

; Check for timeout:
Function Jump_TimeOut ()
	For test.timer= Each timer
		If test\timeOut < MilliSecs ()
 		       Jump_move=0
			Delete test
			Return 1
	      Else
	           Jump_Move=Sin(test\vel_start#)
	            test\vel_start=test\vel_start#- test\vel_down#
        	   Return 0
       	EndIf
     Next		
End Function


Function CreateWorld()
;Light
light=CreateLight(1)
PositionEntity light ,0,50,0

;Plan
plan=CreatePlane()
EntityColor plan,255,0,0
EntityType plan, C_LEVEL




mindstorms(Posted 2006) [#13]
You need to put my code in the main loop, and get rid of the repeat and until commands. I would also recremend declaring which_one to be a global...Can't see much else that's wrong....didn't look that hard.


Chad(Posted 2006) [#14]
I still can't seem to get it.. I get an error somewhere or another..

for the global it'd be
global = which_one=1


and then this is in the main loop

yada = LoadMesh("yadda.b3d")
blah = LoadMesh("blah.b3d")
EntityAlpha yada,1
EntityAlpha blah 0


and this is right after Function UpdatePosition(ent) ;refer to code above for placement
if mousehit(1) then 
	which_one = which_one+1
	select (which_one mod 2)
		case 1:
			EntityAlpha yada,1
			EntityAlpha blah 0
		case 2:
			EntityAlpha yada,0
			EntityAlpha blah,1
	End Select
endif


*bangs head on desk* nothing seems to be doing any good


Chad(Posted 2006) [#15]
On the last one part of the code that I showed you right above and where I placed it,

if mousehit(1) then 
	which_one = which_one+1
	select (which_one mod 2)
		case 1:
			EntityAlpha yada,1
			EntityAlpha blah 0
		case 2:
			EntityAlpha yada,0
			EntityAlpha blah,1
	End Select
endif


It wants me to put a ")" after select (which_one )mod 2) where I just added it.. and then it says it needs an end select or something like that.. So this might be the problem.


mindstorms(Posted 2006) [#16]
select (which_one mod 2), not select (which_one) mod 2). It should work, unless there is another if that you put it in or if you call mousehit(1) somwhere else in the program (mouseHit(1) only works once per loop)


big10p(Posted 2006) [#17]
Case statements aren't terminated with colons. Remove them.


Chad(Posted 2006) [#18]
Ok, all of it is working now except for when I run the program, and I use the mouse button, I press it once, nothing happens, when I press it again, it crashes and says entity does not exist, and is pointing to EntityAlpha yada,1 when that is what I use to start the program.

Any suggestions on why it wouldn't be changing first of all, and then why it's crashing on the second time I press it?


n8r2k(Posted 2006) [#19]
Check to make sure your paths to the models are correct, then to see if you are consistent with your variable names, then you might make yada global. If all that doesnt work, you may try to be more descriptive of whats happening and/or post your code in its entirety (only instead of using the [ code ] tag, use [ codebox ], it takes up less room for longer scripts.


Chad(Posted 2006) [#20]
Here is the entire code.. Someone who can look at another piece of code and find an error in it is very intelligent. Especially since I can't even code simple stuff like this..




mindstorms(Posted 2006) [#21]
player is declared global, but not player attack. Since the code that checks player attack is in a function, playerattack has to be global.

@big10p: I have never had trouble with using colons for the cases. They make it look right to me, but are not needed.


Chad(Posted 2006) [#22]
Ok, that way it doesn't crash, but it doesn't swap the meshes, looking at it I don't see why they don't swap, do you?


Chad(Posted 2006) [#23]
Ok, both are loaded at the same time and the attack one is initially not entityalpha


Chad(Posted 2006) [#24]
Ok, if I comment out

Entityalpha player,1
entityalpha playerattack,0


then both load at the same time and when I left click then the attack disapeers, however, left clicking again it doesn't come back. It stays gone.


mindstorms(Posted 2006) [#25]
Set which_one at the beginning to 0 or 2 and put those two lines back in.


Chad(Posted 2006) [#26]
Nothing.. I tried making the global which_one 0, 1, and 2 with no sucess. And then I added which_one = 0 and then 1 and then 2 with nothing..

Then I tried each combination of the two to try to get it to work, however now they don't both appear at the same time so it is a little better, but it doesn't switch.

Thanks though,
Chad


mindstorms(Posted 2006) [#27]
Sorry I didn't catch this earlier, but when you do the select which_one mod 2 you need to have the 2 cases be 1 and 0.


big10p(Posted 2006) [#28]
@big10p: I have never had trouble with using colons for the cases. They make it look right to me, but are not needed.
Really? OK. I just saw them and assumed that they were causing the syntax error.


mindstorms(Posted 2006) [#29]
They cause problems in blitz max, but not in blitz 3d...It is a personal preference.


Chad(Posted 2006) [#30]
Thank you!

I know this was a bit of a hassle for all of you, but I got it working now..

Thanks everyone :-D
Chad