Here's the situation. I have a uinteger pointer pointer that my program modifies all day long: "dim buildingAmountByPlayer as uinteger ptr ptr"
The use is:
Code: Select all
buildingAmountByPlayer[playerId][buildingId] = buildingAmount
I want to attach a way to modify my "building type count" every time that I modify the variable. (The "building type count" would be the amount of unique building types that are built... so, 5 schools would count as +1, and 3 bakeries as +1, but 0 stables would not count.)
This addition is fairly simple. Here are two cases, setting and adding/subtracting:
Code: Select all
'ORIGINAL (setting):
buildingAmountByPlayer[playerId][buildingId] = buildingAmount
'NEW (setting):
if buildingAmount > 0 then
if buildingAmountByPlayer[playerId][buildingId] = 0 then buildingCountByBuildingType[playerId] += 1
else
if buildingAmountByPlayer[playerId][buildingId] then buildingCountByBuildingType[playerId] -= 1
end if
'ORIGINAL (adding/subtracting):
buildingAmountByPlayer[playerId][buildingId] += buildingAmount
'NEW (adding/subtracting):
scope
dim as uinteger buildingAmountOldTemp = buildingAmountByPlayer[playerId][buildingId]
dim as uinteger buildingAmountNewTemp = buildingAmountOldTemp + buildingAmount
if buildingAmountNewTemp > 0 then
if buildingAmountOldTemp = 0 then buildingCountByBuildingType[playerId] += 1
else
if buildingAmountOldTemp then buildingCountByBuildingType[playerId] -= 1
end if
buildingAmountByPlayer[playerId][buildingId] += buildingAmount
end scope
Another way, which I believe exists in C++ and partly in Freebasic, is to create inline functions, or macros.
I can create two macros for setting and adding/subtracting and a macro for the getter. Thus, I won't have the speed hit that is associated with small functions.
Since there's not really that much extra code here and the amount of instances in the code (not the amount of times I'll be calling, which is much greater) is small, there's a negligible memory hit.
There is a huge issue with Freebasic macros that make them fairly unwieldy to use and dirty, however.
THERE IS NO SCOPE
The buildingAmountByPlayer and buildingCountByBuildingType variables are contained in a UDT. I want my macro similarly contained, but the macro namespace is global.
(A) This forces me to have a huge function name instead of a huge one.
(B) it prevents me from having a clean getter. Theoretically, I need to be able to get these:
getbuildingAmountByPlayer(p) ' A pointer to the building list for that player.
getbuildingAmountByPlayer(p, buildingId) ' Just an amount.
I can't do that with the current macros since I'll get a duplicate definition!
(C) The programmer can't set getbuildingAmountByPlayer to PRIVATE (it should be private!!) because the macro can only work on public variables. (since it's just a macro) Again, theoretically, proper scoping (allowing macros to be put in UDTs) should fix this.