|
-
Apr 2nd, 2010, 12:34 PM
#1
Thread Starter
New Member
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.
-
Apr 2nd, 2010, 03:38 PM
#2
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?
-
Apr 2nd, 2010, 03:57 PM
#3
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
-
Apr 2nd, 2010, 05:01 PM
#4
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
Delete it. They just clutter threads anyway.
-
Apr 3rd, 2010, 02:15 AM
#5
Thread Starter
New Member
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)
Last edited by Lotem; Apr 3rd, 2010 at 02:20 AM.
-
Apr 3rd, 2010, 03:32 AM
#6
Re: Problem with returning a Type
If that's the case you may want to use classes.
I'll post an example soon.
Delete it. They just clutter threads anyway.
-
Apr 3rd, 2010, 04:26 AM
#7
Thread Starter
New Member
Re: Problem with returning a Type
 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.
-
Apr 3rd, 2010, 04:46 AM
#8
Thread Starter
New Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|