Everything on an FP table that does something (i.e. not a wall) needs to have a class built for it. To start with, you simply need a class declaration, which looks like this:
End Class
Of course this class doesn't actually do anything, we still have to add in the properties and methods that make it work. Since it is a simple class, I think it will actually only have one property:
Public State
End Class
To use this class, you have to "instantiate" the class, which is just a programming way of saying that you have to create an object of type "bumper". This will be done in the FuturePinball_BeginPlay() subroutine. We do this because we have to create all of our objects when the table first loads so that everything will work when the player starts the game. The way it is done is thusly:
set redBumper = bumper
I'm also going to create a blueBumper and yellowBumper object as well.
To change the value of the "State" property, you simply use the object name and the "." operator, like this:
Notice that I'm using text to tell the program if the bumper is off, lit, or flashing. The code itself doesn't care. You can use text or numbers equally well here. The reason I'm using text in this case is for readability. I can look at "lit" and know what that means, whereas if it was written as redBumper.State = 2, I might not know what that means.
So to pull it all together, here is the way that the bumper logic will work with classes. Since we have to rely on the built-in _Hit() functions, we will use that to call our own scoring function:
bumpHit(redBumper)
End Sub
And our function will look like this:
Dim numFlashing : numFlashing = 0
If (bump.State = "off") then
AddScore(1000)
Else
If (bump.State = "lit) then
AddScore(2000)
Else
Dim i
For i = 1 to 3
Select Case (i)
case 1: if (redBumper.State = "flashing") then
numFlashing = numFlashing + 1
end if
case 2: if (blueBumper.State = "flashing") then
numFlashing = numFlashing + 1
end if
case 3: if (yellowBumper.State = "flashing") then
numFlashing = numFlashing + 1
End Select
Next
If (numFlashing = 3) then
AddScore(5000)
Else
AddScore(3000)
End If
End If
End If
End Sub
I understand that the final code may look more complicated than the code in the previous post, and in some ways it is. The bumpers are actually the worst case scenario for classes. A lot of setup without much apparent benefit. But as things get more complicated, the benefits become more clear. To start off, we decreased the number of variables that we were using, and consolidated functions. Instead of having three seperate functions for each bumper, we call the same function for all three. This means that if we want to change the bumper behavior for some reason, we just change it in once place, not in three.
I do realize that the earlier post's function could be simplified in the same manner, but I wasn't after perfect code in that function, it was for illustrative purposes only, and thus was written with clarity in mind.
Edited by MeatBundle, 11 May 2010 - 09:35 PM.




Top















are all trademarks of VPFORUMS.