Forsooth! My Missile Won't Work

Blitz3D Forums/Blitz3D Beginners Area/Forsooth! My Missile Won't Work

Ace Killjoy(Posted 2008) [#1]
Yup. I have another problem.
I have a code that I got from someone on this sight and I have implemented it into a project of mine.
Here is the code:

When I press the space bar, it points to "Dx# = EntityX( foe\shape ) - EntityX( missile )" and says "Entity Does Not Exist".
I think it knows the missile is there so I'm guessing it's the foe\shape (I could be wrong).

So what am I to do?


Warner(Posted 2008) [#2]
Well, the code you've posted seems to run fine, if I add CreateCamera, remove EntityTexture, and use MoveEntity missile, 0, 0, 5. So I think the trouble must be in another part of the code.
Since the error is 'Entity does not exist', 'foe' should still exist, only the mesh that is placed in \shape seems to have dissapeared somewhere. I find it most likely that you somewhere assign another value to the 'shape' field.
Try a search in your code for 'shape' or perhaps 'FreeEntity'.


Ace Killjoy(Posted 2008) [#3]
That is very possible. I only cut & pasted the parts of my code that I thought had something to do with it.
I'll look through my code, but were you hinting at a search engine inside Blitz3D? Because if you were, I would much like to learn of it.


Ace Killjoy(Posted 2008) [#4]
Yup, it's definately not detecting foe\shape.
I did not find anything unusual in my code so it's probably some Type thing I have not learned yet.


kfprimm(Posted 2008) [#5]
Wouldn't PointEntity accomplish what you need to do?


Ace Killjoy(Posted 2008) [#6]
Possibly, but it would not look very good.
When a missile is launched it takes a time to aim itself in the right direction. If I were to use PointEntity, I could work, but it would instantly go in the direction of the target.

Also, I tried changing "foe\shape" to a normal identifier that was not a Type and it worked fine. I still need it to seek the "foe\shape, however.


Ace Killjoy(Posted 2008) [#7]
Still can't find anything to explain my problem.


Stevie G(Posted 2008) [#8]
If you need help you need to post more code. As some have suggested you are probably doing something elsewhere in your program which is affecting this. Are you freeing the foe\shape somewhere?

As it stands, when you press space, "foe" is the last instance of target that was created so it should work fine.

If you add this line of code after you press space

for t.target = each target
debuglog str$( t )
next

What information is displayed in the debuglog?


Ace Killjoy(Posted 2008) [#9]
Well, the debuglog turned up this:
[25,28297848,3]
[25,28298528,3]
[25,34764816,3]
[25,34765496,3]
[25,34750472,3]
[25,34751152,3]
[25,28295176,3]
[25,28295856,3]
[25,34607112,3]
[25,34607792,3]

I couldn't make heads or tails of it.

Also, here is more of my code that I dug up that might, just might, have something to do with it:


If you can find anything to blame in there feel free to point it out.
And thanks in advance.


Stevie G(Posted 2008) [#10]
Your code is a bit of a mess but I see the problem.

As you are using the 'foe' variable to iterate through the type list to apply gravity, 'foe' will be set to null at the end of the loop.

See example :

For foe.target=Each target
  TranslateEntity foe\shape,0,-2,0
  debuglog str$(foe)
Next
debuglog str$(foe)  ; <---- foe will be [NULL]


Don't use the same variable for the missiles target, use a global variable:

global MissileTarget.Target


and set it to one of the existing targets, once they are created :


MissileTarget = GetNearestTarget( Cam )

function GetNearestTarget.Target( Source )

  local Found.target = null

  MinDistance# = 100000

  for t.target = each target
     
    Distance = entitydistance( Source , t\Shape )

    if Distance < MinDistance
       MinDistance = Distance
       Found = t
    endif

  next

  return found

end function


Then, instead of using foe\shape for the missile targetting, use MissileTarget\Shape.

Stevie


Ace Killjoy(Posted 2008) [#11]
Darn, and I thought I was being smart with the gravity code. Aw well.
Thanks for the advice, I'll see what I can do with it.
I'm still curious as to how you can figure anything out from that DeBugLog code.


Ace Killjoy(Posted 2008) [#12]
Having a little trouble figuring out where to put certain pieces of code.

I tried putting "global MissileTarget.Target" in a few different places in the target creating part of my code and it either said "Illegal Type Conversion" or "Global Can Only Appear-" anyway I can't figure that part out.

I also put "MissileTarget = GetNearestTarget( Cam )" right under the missile firing part of my code and got an "Illegal Type Conversion".

I'm obviously not very good at this.
When you said "Variable" did you mean Identifier, I got a little confused at that part.


Stevie G(Posted 2008) [#13]
I got nothing from the debuglog other than the fact that the \shape exists for them all.

No offence Ace but I think you should start with something simpler, or at least learn a bit more about how to use types properly.

I did mean variable, but you can only declare a global variable once in your code. I see nowhere in your code which handles missiles once space is pressed? At the moment you have to continually hold space to align and move the missile. Surely this isn't how you intend for this to work?

Anyway, this is what I meant from my previous post :



If you need any further help can I suggest you make a version of your code which does not require external media. You are more likely to get help that way.

Stevie


Ace Killjoy(Posted 2008) [#14]
In response to the previous comment of my code being a mess: Tell me about it.

I started out with the KeyDown command for the missile because it was simpler and I was planning to make an update code for it later once I got it working.

Also, yeah I probably shouldn't shoot for the sky quite yet. If I see something I really like I just get the irresistible urge to dive right into it.
And yes, I'll work on a non-external media version of my code.

Thanks again for all the help. I'll try this out and see how it works, and try to find time to check out some of the sweet tutorials I've been hearing about.


Ace Killjoy(Posted 2008) [#15]
Well, I still get an "Illegal Type Conversion" in reference to the "MissileTarget = GetNearestTarget( Cam )" line, but that's ok.

I think I'm going to come back to this later on when I am more better skilled at this.

Thanks again for all your help.


Mikel(Posted 2008) [#16]
Try using:

MissileTarget.Target = GetNearestTarget( Cam )

after you set the variable as a global pointer like Stevie showed you.

That function is returning a pointer to a Type and may be why you're getting the "Illegal Type Conversion" error message.

Don't give up!


Stevie G(Posted 2008) [#17]
Clearly your not using the code I posted above as it includes "global MissileTarget.Target" just below the type definition. If you do this then then line "MissileTarget = GetNearestTarget( cam )" will not give an illegal Type conversion error.

If it's still an issue post the code.


Ace Killjoy(Posted 2008) [#18]
@ Mikel:
Don't worry, I'm not giving up. I'm mearly taking a break to look into the details, as all this talk of variables and types is confusing me.

Also, the problem presists.
Here is where I put the code:

Likely and oversimplified example. If I needed to put anything else in there just let me know.

I'm sure you are being very patient with my noobishness Stevie G and I am greatful for it.


Stevie G(Posted 2008) [#19]
You need to add a .target at the end of the function name as it indicates what type of variable you are returning from the function. By default this is an integer which is why you get the error.

Function getnearesttarget( cam ) ; default  / returns integer

Function getnearesttarget%( cam ) ;returns integer

Function getnearesttarget#( cam ) ;returns a float

Function getnearesttarget.target( cam ) ; returns a .target


Stevie


Mikel(Posted 2008) [#20]
Yep, just do what Stevie suggested with your Function name and also, I think when you call that function you should use the pointer missiletarget.target

Try it with those changes and let us know how it goes.


Stevie G(Posted 2008) [#21]
If you declare the missiletarget.target to be global then blitz already knows what type of variable missiletarget is so this works fine:

missiletarget = getnearesttarget( cam )


Ace Killjoy(Posted 2008) [#22]
Wow, I did not know that.
It solved my "Illegal Type Conversion" problem.

But now it points to this line:
distance#=EntityDistance(source,t\shape)

and says "Entity Does Not Exist" for as long as I hold down the space bar (I assume that's because of the KeyDown command).
Come to think of it, I didn't see "Source" anywhere else in the code. Was that supposed to mean something else?


Stevie G(Posted 2008) [#23]
Ace, if you'd copied the code I posted then you wouldn't have had any of these issues!!

See the original function below as I posted it, note the 'Source' instead of 'cam' as a function parameter. When you call the function use 'cam'.

Function GetNearestTarget.Target( Source )

  Local Found.target

  MinDistance# = 100000

  For t.target = Each target
     
    Distance# = EntityDistance( Source , t\Shape )

    If Distance < MinDistance
       MinDistance = Distance
       Found = t
    EndIf

  Next

  Return found

End Function



Ace Killjoy(Posted 2008) [#24]
I can see that you are frustrated with me, quite understandable as I am noobish when it comes to Types and not much better when it comes to Functions and tend to occasionally over look an instruction. For that, I apologize.

I did get it to work. Thank you for your help and patience.

I also found a neat little tutorial that should help me with this type thing.

Thanks again.