Results 1 to 8 of 8

Thread: Problem with returning a Type

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    4

    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.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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?

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    4

    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.

  6. #6
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    4

    Re: Problem with returning a Type

    Quote Originally Posted by TheBigB View Post
    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.

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    4

    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
  •  



Click Here to Expand Forum to Full Width