Problem with returning a Type
So I'm in the middle of a school project that I have to hand next month.
Anyway, I have created 3 Types- one for 3D modeling, one for the levels in the game and another one for the enemies(which are, creatively, called "Aliens")
Code:
Public Type m3Solid
nVerts As Integer
Verts() As m3Point
nFaces As Integer
Faces() As m3Face
End Type
Public Type m3Level
Num As Integer
noAliens As Integer
AA() As m3Alien
End Type
Public Type m3Alien
Body As m3Solid
Dome As m3Solid
aLoc As m3Location
End Type
As you can see, in m3Level there is an array of Aliens, and the aliens are build as m3Solid. (m3Location is another type which represents where the aliens will spawn)
I have two functions that create the alien, but my problem is that when I return the alien from one function to the other, it's like it was "reseted".
Here are my two functions:
Code:
Public Function SysInit(w As Double, ByVal h As Double) As m3Alien
Dim r As Double
Dim s As m3Alien
r = 30
s.Body = m3GetPrizm(r * 1.4, h / 5, 20)
s.Dome = m3GetHalfSphere(r, 20)
m3SolidApply s.Body, m3Translate(0, -h / 5, 0)
MsgBox s.Body.nVerts
SysInit = s
End Function
Public Function createAlien(ByRef loc As m3Location, ByRef obj As Object)
Dim i As Integer
Dim a As m3Alien
For i = 1 To getCurLevel.noAliens
getCurLevel.AA(i) = Alien.SysInit(30, 45)
MsgBox getCurLevel.AA(i).Body.nVerts
sendShips locs(getRandomInt), getCurLevel.AA(i), obj
'checkWhereAlien obj
Next i
setCurLevel lvls(getCurLevel.Num)
End Function
I've created two MsgBox to see what happens when the alien is being returned. In the first MsgBox the value is 40, in the second it's 0.
I don't know what is wrong because everything seems fine to me.
By the way, I hope you can understand this without all the code. If you want I can paste here the entire code.
Re: Problem with returning a Type
Code:
Public Function createAlien(ByRef loc As m3Location, ByRef obj As Object)
Dim i As Integer
Dim a As m3Alien
For i = 1 To getCurLevel.noAliens
getCurLevel.AA(i) = Alien.SysInit(30, 45)
MsgBox getCurLevel.AA(i).Body.nVerts
sendShips locs(getRandomInt), getCurLevel.AA(i), obj
'checkWhereAlien obj
Next i
setCurLevel lvls(getCurLevel.Num)
End Function
What kind of object is the Alien in that code?
Re: Problem with returning a Type
I think this is the ticket... Changes highlighted in blue
Code:
Public Function createAlien(ByRef loc As m3Location, ByRef obj As Object)
Dim i As Integer
Dim a As m3Alien
For i = 1 To getCurLevel.noAliens
a.SysInit(30, 45)
getCurLevel.AA(i) = a
MsgBox getCurLevel.AA(i).Body.nVerts
sendShips locs(getRandomInt), getCurLevel.AA(i), obj
'checkWhereAlien obj
Next i
setCurLevel lvls(getCurLevel.Num)
End Function
Re: Problem with returning a Type
From what I gather from the presented piece of code, Alien.SysInit doesn't exist (as SysInit is just a function)
If I understand it correctly, you should use:
Code:
Public Function createAlien(ByRef loc As m3Location, ByRef obj As Object)
Dim i As Integer
Dim a As m3Alien
For i = 1 To getCurLevel.noAliens
getCurLevel.AA(i) = SysInit(30, 45)
MsgBox getCurLevel.AA(i).Body.nVerts
sendShips locs(getRandomInt), getCurLevel.AA(i), obj
'checkWhereAlien obj
Next i
setCurLevel lvls(getCurLevel.Num)
End Function
Re: Problem with returning a Type
Alien is the module which contains the two functions and the m3Alien type declaration.
I wrote Alien.SysInit because I also have Ship.SysInit (Dumb, I know)
Re: Problem with returning a Type
If that's the case you may want to use classes.
I'll post an example soon.
Re: Problem with returning a Type
Quote:
Originally Posted by
TheBigB
If that's the case you may want to use classes.
I'll post an example soon.
I prefer not to use classes because we I never learned about them.
But if there is no other way then please explain how to use them.
Thanks.
Re: Problem with returning a Type
Ok so I think I found the problem, but I don't know how to fix it.
I changed the code to: (changes in red)
Code:
Public Function createAlien(ByRef obj As Object)
Dim i As Integer
Dim a As m3Alien
For i = 1 To getCurLevel.noAliens
a = Alien.SysInit(30, 45)
MsgBox a.Body.nVerts
getCurLevel.AA(i) = a
MsgBox getCurLevel.AA(i).Body.nVerts
sendShips locs(getRandomInt), getCurLevel.AA(i), obj
'checkWhereAlien obj
Next i
setCurLevel lvls(getCurLevel.Num)
End Function
Now I have 3 MsgBox, one in the SysInit and the other two here.
At the first two the value is 40, and in the third the value is 0.
So clearly the problem is with the array.