problem with my practice program
Blitz3D Forums/Blitz3D Beginners Area/problem with my practice program
| ||
i try to run it and it says one of my entities doesnt exist but i have checked it and i dont think anything's wrong can u see whats wrong? Graphics3D 800, 600, 0, 1 SetBuffer BackBuffer() Const player_col= 1 Const level_col = 2 Const enemy_col = 3 Const bullet_col= 4 Type bullettype Field entityhandle End Type Type badguytype Field entityhandle Field state End Type light = CreateLight() RotateEntity light, 30, 30, 0 camera_pivot= CreatePivot() camera= CreateCamera(camera_pivot) CameraRange camera,1,1000 level = CreateCube():ScaleEntity level,50,50,50:FlipMesh level:MoveEntity level,0,50,0:EntityColor level,20,20,200 levelfloor=CreatePlane():MoveEntity levelfloor,0,.002,0:EntityColor levelfloor,0,100,0 If KeyDown(2) Then weapon1= LoadMesh("SWORD.3ds", camera) If KeyDown(3) Then weapon2= LoadMesh("pdpistol.3dd", camera) bulletmesh= CreateCone() : RotateMesh bulletmesh,90,0,0 : ScaleEntity bulletmesh,.1,.1,.1 EntityType bulletmesh, bullet_col EntityRadius bulletmesh,.01 enemymesh = LoadMesh("F15E.3ds") EntityType enemymesh, enemy_col HideEntity bulletmesh HideEntity enemymesh For iter= 1 To 10 badguy.badguyType = New badguyType badguy\entityhandle= CopyEntity(enemymesh) badguy\state = Rnd(1, 2) Next MoveEntity camera,0,.9,0 MoveEntity weapon1 Or weapon2, .1,-.15,.1 MoveEntity player, 20, 1, -20 For badguy = Each badguyType PositionEntity badguy\entityhandle, Rnd(100)-50,.95, Rnd(100)-50 Next EntityType camera_pivot, player_col EntityRadius camera_pivot,.3,.95 EntityType level, level_col Collisions player_col, level_col,5, 5 Collisions player_col, enemy_col, 1,2 Collisions enemy_col, level_col,10, 5 Collisions bullet_col, level_col, 100, 1 While Not KeyHit(1) wkey = KeyDown(17) ;collect user input skey = KeyDown(31) ;It's a good practice to collect these inputs only once akey = KeyDown(30) ;per loop. This will prevent odd behaviors from happening, dkey = KeyDown(32) ;for instance if the state of a key changes between multiple mouse1 = MouseHit(1) ;checks of that key while still in the same loop. If wkey Then MoveEntity camera_pivot, 0, 0, .1 ;Forward - w key If skey Then MoveEntity camera_pivot, 0, 0, -.1 ;Back - s key If akey Then MoveEntity camera_pivot, -.1, 0, 0 ;Left - a key If dkey Then MoveEntity camera_pivot, .1, 0, 0 ;Right - d key TurnEntity camera_pivot, 0, -MouseXSpeed()/5.0, 0 ;rotate player Pivot according to mouse X movement TurnEntity camera, MouseYSpeed()/5.0, 0, 0 ;rotate camera up/down according to mouse Y movement If EntityPitch(camera) < -45 ;don't allow camera to look below -45 degrees RotateEntity camera, -45, EntityYaw(camera), EntityRoll(camera) EndIf If EntityPitch(camera) > 45 ;don't allow camera to look above 45 degrees RotateEntity camera, 45, EntityYaw(camera), EntityRoll(camera) EndIf MoveMouse GraphicsWidth()/2, GraphicsHeight()/2 ;reset mouse position to middle of screen TranslateEntity camera_pivot, 0, -.1, 0 ;simple gravity If mouse1 ;check if left mouse button was pressed bullet.bullettype = New bullettype ;create a bullet bullet\entityhandle = CopyEntity(bulletmesh) ;create the bullet mesh PositionEntity bullet\entityhandle, EntityX(weapon1, 1), EntityY(weapon1, 1), EntityZ(weapon1, 1) ;place the bullet at the guns position RotateEntity bullet\entityhandle, EntityPitch(weapon1, 1), EntityYaw(weapon1, 1), EntityRoll(weapon1, 1);orientate the bullet with the gun ResetEntity bullet\entityhandle ;otherwise bullet could hit enemy while moving from 0,0,0 to current position EndIf For thisbullet.bullettype = Each bullettype ;iterate through all of the bullets MoveEntity thisbullet\entityhandle, 0, 0, 2 ;move the bullet forward along the bullets Z axis If Abs(EntityX(thisbullet\entityhandle, 1)) > 10000 ;check if the bullet is way out of bounds FreeEntity thisbullet\entityhandle ;delete the bullet mesh Delete thisbullet ;delete the bullet ElseIf Abs(EntityY(thisbullet\entityhandle, 1)) > 10000 ;check if the bullet is way out of bounds FreeEntity thisbullet\entityhandle ;delete the bullet mesh Delete thisbullet ;delete the bullet ElseIf Abs(EntityZ(thisbullet\entityhandle, 1)) > 10000 ;check if the bullet is way out of bounds FreeEntity thisbullet\entityhandle ;delete the bullet mesh Delete thisbullet EndIf Next UpdateWorld ;figures out collisions For thisbullet = Each bullettype ;iterate through all of the bullets If CountCollisions(thisbullet\entityhandle) > 0 ;check if bullet collided with something enemyhit = EntityCollided(thisbullet\entityhandle, 3) ;note which enemy bullet collided with (if any) If enemyhit > 0 ;enemyhit contains entity handle of enemy that was hit For thisbadguy.badguytype = Each badguytype ;iterate through all of the badguys If enemyhit = thisbadguy\entityhandle ;check if the enemy hit = this badguy If thisbadguy\state <> 5 ;check if badguy is alive thisbadguy\state = 5 ;dead ;make him dead RotateEntity thisbadguy\entityhandle, 90, 0, 0 ;make him horizontal EntityType thisbadguy\entityhandle, 0 ;turn off collisions for this badguy TranslateEntity thisbadguy\entityhandle, 0, -.9, 0;set him on the ground Exit ;exits the badguy For-Next loop (no need to check rest of badguys) EndIf EndIf Next EndIf FreeEntity thisbullet\entityhandle ;delete the bullet mesh Delete thisbullet ;delete the bullet EndIf Next RenderWorld ;draws the 3d scene Flip ;displays the scene to the screen Wend End |
| ||
enemymesh = LoadMesh("F15E.3ds") Check your modelpath. weapon2= LoadMesh("pdpistol.3dd", camera) Have You mistyped the file ending .3dd ? www.moonworx.de |
| ||
Is this right?If KeyDown(3) Then weapon2= LoadMesh("pdpistol.3dd", camera) |
| ||
thats right aswell but the part entitytype enemymesh, enemy_col it says the entity does not exist |
| ||
I looked some further and found you haven't created a player entity at all. Your player (cam ) falls trough the ground. For a first test you could just create some cubes instead of the real models. www.moonworx.de |
| ||
FYI, What are the forum codes? Here's your problem: If KeyDown(2) Then weapon1= LoadMesh("SWORD.3ds", camera) If KeyDown(3) Then weapon2= LoadMesh("pdpistol.3dd", camera)This is a bad coding technique. Your weapons should be loaded at the beginning of your program, NOT if you happen to be pressing the 1 or 2 key. Remember, this code is only executed at the time your program starts, it is never executed again, so unless you happened to be holding down the 1 or 2 key at the time the program started, neither weapon would have loaded. Your weapons should be loaded like in my example: weapon1 = LoadMesh("sword.3ds", camera) weapon2 = LoadMesh("bdpistol.3ds", camera)Then, IN THE GAME LOOP (inside of the While Not KeyHit(1)-Wend loop), add your code for switching weapons like so: If KeyDown(2) ShowEntity weapon1 HideEntity weapon2 Endif If KeyDown(3) ShowEntity weapon2 HideEntity weapon1 Endif |
| ||
thnx ron, how do i make like a sword slash if u press a key? by the way im quite new. |
| ||
Making the sword slash can be done in a couple of ways. You could animate the sword slashing in a modelling program, then play that back in blitz, or, rotate the sword when a key is pressed. Not exactly an easy one. You should get some more of the basics done before you venture into that i think. Like structuring your code for starters. It helps alot when searching for errors and seeing how the code is executed. |
| ||
ross can u advise me on what i can do to get better cos im only 15 |
| ||
Sure thing :o) Firstly, say you have a loop. with some IF statements in there. While Not KeyHit(1) If KeyDown(203) then var=1 End If If KeyDown(205) then var=0 End If UpdateWorld RenderWorld Flip Wend It doesn't look very good. Just looks like a big chunk of code. But if you tab in everytime there's an IF or the beginning of a loop, and tab back again at the end, you can see easier the start and end of loops, and if statements, and generally bits of code that do a specific function. While Not KeyHit(1) If KeyDown(203) Then var=1 End If If KeyDown(205) Then var=0 End If UpdateWorld RenderWorld Flip Wend That looks alot neater and you can see what happens if the IF statement is true. Try and group things together as well. Like, set up everything before entering the main loop of your program. Try not to load in meshes, sounds etc, in the middle of the main game loop. I'll take your code you posted at the top. See how much clearer it looks when tabbed, and spaced out? Graphics3D 800, 600, 0, 1 SetBuffer BackBuffer() Const player_col= 1 Const level_col = 2 Const enemy_col = 3 Const bullet_col= 4 Type bullettype Field entityhandle End Type Type badguytype Field entityhandle Field state End Type light = CreateLight() RotateEntity light, 30, 30, 0 camera_pivot= CreatePivot() camera= CreateCamera(camera_pivot) CameraRange camera,1,1000 level = CreateCube():ScaleEntity level,50,50,50:FlipMesh level:MoveEntity level,0,50,0:EntityColor level,20,20,200 levelfloor=CreatePlane():MoveEntity levelfloor,0,.002,0:EntityColor levelfloor,0,100,0 If KeyDown(2) Then weapon1= LoadMesh("SWORD.3ds", camera) If KeyDown(3) Then weapon2= LoadMesh("pdpistol.3dd", camera) bulletmesh= CreateCone() : RotateMesh bulletmesh,90,0,0 : ScaleEntity bulletmesh,.1,.1,.1 EntityType bulletmesh, bullet_col EntityRadius bulletmesh,.01 enemymesh = LoadMesh("F15E.3ds") EntityType enemymesh, enemy_col HideEntity bulletmesh HideEntity enemymesh For iter= 1 To 10 badguy.badguyType = New badguyType badguy\entityhandle= CopyEntity(enemymesh) badguy\state = Rnd(1, 2) Next MoveEntity camera,0,.9,0 MoveEntity weapon1 Or weapon2, .1,-.15,.1 MoveEntity player, 20, 1, -20 For badguy = Each badguyType PositionEntity badguy\entityhandle, Rnd(100)-50,.95, Rnd(100)-50 Next EntityType camera_pivot, player_col EntityRadius camera_pivot,.3,.95 EntityType level, level_col Collisions player_col, level_col,5, 5 Collisions player_col, enemy_col, 1,2 Collisions enemy_col, level_col,10, 5 Collisions bullet_col, level_col, 100, 1 While Not KeyHit(1) wkey = KeyDown(17) ;collect user input skey = KeyDown(31) ;It's a good practice to collect these inputs only once akey = KeyDown(30) ;per loop. This will prevent odd behaviors from happening, dkey = KeyDown(32) ;for instance if the state of a key changes between multiple mouse1 = MouseHit(1) ;checks of that key while still in the same loop. If wkey Then MoveEntity camera_pivot, 0, 0, .1 ;Forward - w key If skey Then MoveEntity camera_pivot, 0, 0, -.1 ;Back - s key If akey Then MoveEntity camera_pivot, -.1, 0, 0 ;Left - a key If dkey Then MoveEntity camera_pivot, .1, 0, 0 ;Right - d key TurnEntity camera_pivot, 0, -MouseXSpeed()/5.0, 0 ;rotate player Pivot according to mouse X movement TurnEntity camera, MouseYSpeed()/5.0, 0, 0 ;rotate camera up/down according to mouse Y movement If EntityPitch(camera) < -45 ;don't allow camera to look below -45 degrees RotateEntity camera, -45, EntityYaw(camera), EntityRoll(camera) EndIf If EntityPitch(camera) > 45 ;don't allow camera to look above 45 degrees RotateEntity camera, 45, EntityYaw(camera), EntityRoll(camera) EndIf MoveMouse GraphicsWidth()/2, GraphicsHeight()/2 ;reset mouse position to middle of screen TranslateEntity camera_pivot, 0, -.1, 0 ;simple gravity If mouse1 ;check if left mouse button was pressed bullet.bullettype = New bullettype ;create a bullet bullet\entityhandle = CopyEntity(bulletmesh) ;create the bullet mesh PositionEntity bullet\entityhandle, EntityX(weapon1, 1), EntityY(weapon1, 1), EntityZ(weapon1, 1) ;place the bullet at the guns position RotateEntity bullet\entityhandle, EntityPitch(weapon1, 1), EntityYaw(weapon1, 1), EntityRoll(weapon1, 1);orientate the bullet with the gun ResetEntity bullet\entityhandle ;otherwise bullet could hit enemy while moving from 0,0,0 to current position EndIf For thisbullet.bullettype = Each bullettype ;iterate through all of the bullets MoveEntity thisbullet\entityhandle, 0, 0, 2 ;move the bullet forward along the bullets Z axis If Abs(EntityX(thisbullet\entityhandle, 1)) > 10000 ;check if the bullet is way out of bounds FreeEntity thisbullet\entityhandle ;delete the bullet mesh Delete thisbullet ;delete the bullet ElseIf Abs(EntityY(thisbullet\entityhandle, 1)) > 10000 ;check if the bullet is way out of bounds FreeEntity thisbullet\entityhandle ;delete the bullet mesh Delete thisbullet ;delete the bullet ElseIf Abs(EntityZ(thisbullet\entityhandle, 1)) > 10000 ;check if the bullet is way out of bounds FreeEntity thisbullet\entityhandle ;delete the bullet mesh Delete thisbullet EndIf Next UpdateWorld ;figures out collisions For thisbullet = Each bullettype ;iterate through all of the bullets If CountCollisions(thisbullet\entityhandle) > 0 ;check if bullet collided with something enemyhit = EntityCollided(thisbullet\entityhandle, 3) ;note which enemy bullet collided with (if any) If enemyhit > 0 ;enemyhit contains entity handle of enemy that was hit For thisbadguy.badguytype = Each badguytype ;iterate through all of the badguys If enemyhit = thisbadguy\entityhandle ;check if the enemy hit = this badguy If thisbadguy\state <> 5 ;check if badguy is alive thisbadguy\state = 5 ;dead ;make him dead RotateEntity thisbadguy\entityhandle, 90, 0, 0 ;make him horizontal EntityType thisbadguy\entityhandle, 0 ;turn off collisions for this badguy TranslateEntity thisbadguy\entityhandle, 0, -.9, 0;set him on the ground Exit ;exits the badguy For-Next loop (no need to check rest of badguys) EndIf EndIf Next EndIf FreeEntity thisbullet\entityhandle ;delete the bullet mesh Delete thisbullet ;delete the bullet EndIf Next RenderWorld ;draws the 3d scene Flip ;displays the scene to the screen Wend End You can see where the loops start, the IF's start and what commands are executed if the IF statement is true. Also, try and use functions. You have code to check the collisions with the bullets, but it's in your main loop. You main loop really should consist of function calls, nothing to major, mainly to keep it clean and modular. If something goes wrong in your code, you can narrow it down to a specific function producing an error, rather than searching thru your main loop. You should try and start off with something a little simlier. No offense here, but the code looks as if it was pasted together with different code samples, with a bit of coding done by yourself. I'm not too sure what to suggest really for starting simple, as i dived in a bit when i first started :o) Simple things like moving the camera around. Learning how to structure your code though does help alot. Oh, and if your going to use function and you have variables in your main loop then make them GLOBAL, or else you risk losing them when you enter a function :o) |