Types and OOP?
BlitzMax Forums/BlitzMax Programming/Types and OOP?
| ||
I have a TStar type. I create objects using s:star = new star. When I check if a star is near the other I do this. Method CheckDistance:Double(from:star) Local XDistance:Double = from.XLoc-XLoc Local YDistance:Double = from.YLoc-YLoc Return Sqr(XDistance * XDistance + YDistance*YDistance) End Method Function UpdateCluster() For Local Body:Star = EachIn StarList Body.DrawCluster body.MoveStars For Local Body2:Star = EachIn StarList If (body = body2) Continue If (Body.CheckDistance(Body2)<1.0) DrawText "yay",0,0 End If Next 'Body.CheckDistance Next End Function What i want to know is, why does it work? The full source code is below. Antony helped me with it but I'm at a loss as to why a partiicular bit works. SuperStrict Graphics 800,600,16,60 Global StarList:TList = CreateList() Type Star Field XLoc:Double Field YLoc:Double Field Speed:Double Field Inertia:Double Field Gravity:Double Field CRed:Int Field CGreen:Int Field CBlue:Int Field Name:String Field Class:Int Field Angle:Int Field Direction:Int Global MaxStars:Int Global SType:Int Function CreateCluster:Int( MaxStars:Int , SType:Int ) For Local xiter:Int = 0 To MaxStars - 1 Local Body:Star = New Star Body.Class = Rand( 0 , SType) If body.Class = 0 Body.Name = "Small Planet" Body.Gravity = 0.01 Body.Inertia = 0.05 Body.Speed = Rnd( 0.05 , 0.1 ) Body.XLoc = Rand( 0 , 800 ) Body.YLoc = Rand( 0 , 600 ) Body.CRed = 10 Body.CGreen = 210 Body.CBlue = 25 Body.Angle = 0 Body.Direction = Rand( -180 , 179 ) End If ListAddFirst StarList , (Body) Next Return MaxStars End Function Method DrawCluster() SetColor( CRed , CGreen , CBlue ) Plot XLoc,YLoc End Method Method MoveStars() Xloc = Xloc + Cos(Angle) * Speed YLoc = Yloc + Sin(Angle) * Speed angle = Direction End Method Method DoPhysics() End Method Method CheckDistance:Double(from:star) Local XDistance:Double = from.XLoc-XLoc Local YDistance:Double = from.YLoc-YLoc Return Sqr(XDistance * XDistance + YDistance*YDistance) End Method Function UpdateCluster() For Local Body:Star = EachIn StarList Body.DrawCluster body.MoveStars For Local Body2:Star = EachIn StarList If (body = body2) Continue If (Body.CheckDistance(Body2)<1.0) DrawText "yay",0,0 End If Next 'Body.CheckDistance Next End Function End Type Star.CreateCluster(5,0) While Not KeyHit(KEY_ESCAPE) Cls Star.UpdateCluster 'Star.CheckDistance Flip WEnd If you notice, this bit below.. Function UpdateCluster() For Local Body:Star = EachIn StarList Body.DrawCluster body.MoveStars For Local Body2:Star = EachIn StarList If (body = body2) Continue If (Body.CheckDistance(Body2)<1.0) DrawText "yay",0,0 End If Next 'Body.CheckDistance Next End Function ... has a Body and Body2. What i am not grasping is why Body2 exists if it hasn't been created using "new". eg. Body2:Star = new star. I don't understand how it came to exist and it uses the same list. :/ Also, if you notice Antony's coding, he uses the word "From" as a parameter and the word "continue". I dunno, I'm lost. Any help would be appreciated. Also, please be gentle when explaining it to me. :) |
| ||
From is a just a variable of type Star. If it were me, I would probably call it FromStar, TargetStar or DestStar so that it's more obvious that it's a star. You're checking how far away the current star is FROM the fromstar. Body2 doesn't exist at first. It's using EachIn to iterate through all the stars you previously put in the list. Every time you create a star, you add it to the list ( this line in the create function ) ListAddFirst StarList , (Body) So EachIn just provides a convenient way to get all the stars you've created ( as long as you put them in the list, it won't happen automatically is it did with B3D. Oh, and continue just skips to the end of the loop it's being called in. So it skips the If..EndIf immediately following it, and reaches the Next instead. It then continues to loop until you've gone through the entire list as normal ( unless you already have in which case it exits the loop as normal too. ) He's using it to check if the stars you're comparing are in fact the same star. You don't want to know how far away a star is from itself, so if they are the same, it skips the condition and carries on with the remaining stars. |
| ||
why Body2 You need two stars referenced to check the distance between each one and all of the rest. That's why he Continues when body=body2... there's no point checking the distance between a star and itself. EDIT: Whoops... Gab pips me to it! |
| ||
Ok thanks. But how can Body2 exist without it being created using new? Where does body2 get all it's fields and parameters from? I thought they were all stored Body? Am I making sense? |
| ||
Body2 is just a refferance to each star in you star list. Each one has already been created. What you are doing is going throu each one and "Pretending" its called body2 |
| ||
It's a reference to an entry in the list rather than an actual entry in the list. EDIT: DAGNAMMIT!! :D |
| ||
Ahh, Thank H&K. That makes sense now. lol@Sledge :) Thanks again guys. :) |
| ||
Nothing is "stored" in body. It's just a variable, and as such it's volatile. That is to say, it loses what it used to contain every time you give it another value. Forget for a moment that Body is a type. Think of it as an Integer. If I say Global Body:Int=1 And then I say Body=2 Body no longer contains 1. The 1 was overwritten when I set it to 2. Well it's the same way with types. Every time you create a new type, the old one is gone. BlitzMax now has a garbage collector, so I mean that quite literally. Unless you've put it somewhere, it's gone, deleted, kaput. EG: Type Star Field X:Int,Y:Int End Type Global Body:Star=New Star Body.X=10 Body.Y=10 Body=New Star Body.X=20 Body.Y=20 When the second star is created, the first one is gone from the variable Body. Since I haven't stored it anywhere else, it's subsquently deleted by BMax's garbage collector. Now go back and look at your create function. Body is a local variable. As soon as that function is run once in it's entirety, Body goes out of scope. That is to say, it ceases to exist and the contents are gone. Next time you run the function, the variable will be created afresh, but it won't contain what it contained before. So if you didn't store that newly created star, it would be lost. The only reason you have any stars to work with at all is because you add them to the list. That's the only place they exist. So when you want to use any stars, you have to get them back from the list. There are many ways of doing this, but the easiest and probably the most common is to use For Local Blah:Star=EachIn StarList Now note again, it's YET ANOTHER local variable. It's only holding that one star at a time, and it only holds it temporarily. Once the function ends, it's gone again, and the only stars you have are.. you guessed it.. in your list. Was that explanation any better? |
| ||
@Gab (ie Amon ignore this) That's the only place they exist They exist as well in passing as self in the methods ;0 (Temporay Ghost existance as it is) |