Quote: "STOP IT!!!
"
I disagree, I think it's OK to have an array and a variable share the same name when they belong together. This is an extract from one of my programs, hopefully it demonstrates to you why they share the same name:
rem ---------------------------------------------------------------------------
rem Click object - Effect when mouse clicks terrain
rem ---------------------------------------------------------------------------
rem ---------------------------------------------------------------------------
rem Constants
rem ---------------------------------------------------------------------------
rem ---------------------------------------------------------------------------
rem User Defined Types
rem ---------------------------------------------------------------------------
type ClickObjVT
logging as integer
CurrentMax as integer
endtype
type ClickObjAT
Active as integer
ConnectionActive as integer
pos as vec3
Life as integer
Obj as word
ConnectObj as word
Obj1 as word
Obj2 as word
color as dword
endtype
rem ---------------------------------------------------------------------------
rem Initialise
rem ---------------------------------------------------------------------------
function InitClickObj()
rem debug
DebugOutput( 0 , "Initialising click object" )
rem ---------------------------------------------------------------------------
rem Global Variables
rem ---------------------------------------------------------------------------
global ClickObj as ClickObjVT
rem ---------------------------------------------------------------------------
rem Global Arrays
rem ---------------------------------------------------------------------------
global dim ClickObj() as ClickObjAT
rem initial values
ClickObj.CurrentMax = -1
endfunction
function CreateClickObj( x# , y# , z# , Color , Life , ForceCreation )
rem local variables
local n as integer
local s as integer
rem logging
if ClickObj.logging = 1 then AddTextToConsole( "CreateClickObj " + str$(x#) + "," + str$(y#) + "," + str$(z#) + "," + str$(Color) + "," + str$(Life) + "," + str$(FrameStart) + "," + str$(FrameEnd) + "," + str$(FrameSpeed#) , 0xFF00FFFF , 1 , 0 )
rem find a click object already there
if ForceCreation = 0
for n = 0 to ClickObj.CurrentMax
if ClickObj( n ).Active = 2
if ClickObj( n ).pos.x# = x# and ClickObj( n ).pos.z# = z#
if ClickObj( n ).Life = Life then exitfunction n
if ClickObj( n ).Life <> 0 and Life <> 0 then exitfunction n
endif
endif
next n
endif
rem find free slot
for n = 0 to ClickObj.CurrentMax
if ClickObj( n ).Active < 2 then exit
next n
rem no free slot, create
if n = ClickObj.CurrentMax + 1
array insert at bottom ClickObj()
inc ClickObj.CurrentMax
endif
rem load object
if ClickObj( n ).Active = 0
ClickObj( n ).Obj = find free object()
load object "media\objects\clickobj\clickobj.x" , ClickObj( n ).Obj
ConvertObjectFVFTo338( ClickObj( n ).Obj )
set object transparency ClickObj( n ).Obj , 3
set object cull ClickObj( n ).Obj , 0
disable object zwrite ClickObj( n ).Obj
set object light ClickObj( n ).Obj , 0
endif
rem set object properties
show object ClickObj( n ).Obj
SetObjectDiffuse( ClickObj( n ).Obj , Color )
position object ClickObj( n ).Obj , x# , y# , z#
PlayObject( ClickObj( n ).Obj , 0 , 64 , 3 )
rem set parameters
ClickObj( n ).Active = 2
if Life <> 0 then ClickObj( n ).Life = Life + timer() else ClickObj( n ).Life = 0
ClickObj( n ).pos.x# = x#
ClickObj( n ).pos.y# = y#
ClickObj( n ).pos.z# = z#
ClickObj( n ).Color = color
rem if creation was forced, hide if another one is already at it's position
if ForceCreation = 1
for s = 0 to ClickObj.CurrentMax
if s <> n
if ClickObj( s ).Active > 1
if ClickObj( s ).pos.x# = x# and ClickObj( s ).pos.z# = z# and ClickObj( s ).Life = 0
HideClickObj( n )
exit
endif
endif
endif
next s
endif
endfunction n
function DestroyClickObj( n )
rem logging
if ClickObj.logging = 1 then AddTextToConsole( "DestroyClickObj " + str$(n) , 0xFF00FFFF , 1 , 0 )
rem make sure active
if ClickObj( n ).Active < 2
if ClickObj.logging = 1 then AddTextToConsole( "Not active!" , 0xFFFF0000 , 2 , 0 )
exitfunction -1
endif
rem destroy
hide object ClickObj( n ).Obj
StopObject( ClickObj( n ).Obj )
ClickObj( n ).Active = 1
if ClickObj( n ).ConnectObj > 0 then hide object ClickObj( n ).ConnectObj
ClickObj( n ).ConnectionActive = 0
endfunction n
function HideClickObj( n )
rem logging
if ClickObj.logging = 1 then AddTextToConsole( "HideClickObj " + str$(n) , 0xFF00FFFF , 1 , 0 )
rem make sure active
if ClickObj( n ).Active < 2
if ClickObj.logging = 1 then AddTextToConsole( "Not active!" , 0xFFFF0000 , 2 , 0 )
exitfunction -1
endif
rem hide
hide object ClickObj( n ).Obj
ClickObj( n ).Active = 3
endfunction n
function HideClickObjConnection( n )
rem logging
if ClickObj.logging = 1 then AddTextToConsole( "HideClickObjConnection " + str$(n) , 0xFF00FFFF , 1 , 0 )
rem make sure active
if ClickObj( n ).Active < 2
if ClickObj.logging = 1 then AddTextToConsole( "Not active!" , 0xFFFF0000 , 2 , 0 )
exitfunction -1
endif
rem hide
if ClickObj( n ).ConnectObj > 0 then hide object ClickObj( n ).ConnectObj
endfunction n
function ShowClickObj( n )
rem local variables
local s as integer
rem logging
if ClickObj.logging = 1 then AddTextToConsole( "ShowClickObj " + str$(n) , 0xFF00FFFF , 1 , 0 )
rem make sure active
if ClickObj( n ).Active < 2
if ClickObj.logging = 1 then AddTextToConsole( "Not active!" , 0xFFFF0000 , 2 , 0 )
exitfunction -1
endif
rem check if any other click objects are at the same position
for s = 0 to ClickObj.CurrentMax
if s <> n
if ClickObj( s ).Active = 2
if ClickObj( s ).pos.x# = ClickObj( n ).pos.x# and ClickObj( s ).pos.z# = ClickObj( n ).pos.z#
exitfunction -1
endif
endif
endif
next s
rem show
show object ClickObj( n ).Obj
if ClickObj( n ).ConnectionActive = 1 then show object ClickObj( n ).ConnectObj
ClickObj( n ).Active = 2
endfunction n
function ShowClickObjConnection( n )
rem logging
if ClickObj.logging = 1 then AddTextToConsole( "ShowClickObjConnection" + str$(n) , 0xFF00FFFF , 1 , 0 )
rem make sure active
if ClickObj( n ).Active < 2
if ClickObj.logging = 1 then AddTextToConsole( "Not active!" , 0xFFFF0000 , 2 , 0 )
exitfunction -1
endif
rem show
if ClickObj( n ).ConnectObj > 0 then show object ClickObj( n ).ConnectObj
endfunction n
function CreateClickObjConnection( n , Obj1 , Obj2 )
rem local variables
local pos1 as vec3
local pos2 as vec3
local angle# as float
rem logging
if ClickObj.logging = 1 then AddTextToConsole( "CreateClickObjConnection " + str$(n) + "," + str$(Obj1) + "," + str$(Obj2) , 0xFF00FFFF , 1 , 0 )
rem make sure active
if ClickObj( n ).Active < 2
if ClickObj.logging = 1 then AddTextToConsole( "Not active!" , 0xFFFF0000 , 2 , 0 )
exitfunction -1
endif
rem create object if necessary
if ClickObj( n ).ConnectObj = 0
rem create object
ClickObj( n ).ConnectObj = find free object()
make object plain ClickObj( n ).ConnectObj , 1 , 1
ConvertObjectFVFTo338( ClickObj( n ).ConnectObj )
position object ClickObj( n ).ConnectObj , 0 , 0 , 0
texture object ClickObj( n ).ConnectObj , IMGClickObjConnection
set object transparency ClickObj( n ).ConnectObj , 1
disable object zdepth ClickObj( n ).ConnectObj
set object light ClickObj( n ).ConnectObj , 0
set object cull ClickObj( n ).ConnectObj , 0
set object radius ClickObj( n ).ConnectObj , -1
rem edit diffuse data
lock vertexdata for limb ClickObj( n ).ConnectObj , 0
set vertexdata diffuse 0 , 0x00FFFFFF
set vertexdata diffuse 1 , 0x00FFFFFF
set vertexdata diffuse 2 , 0xFFFFFFFF
set vertexdata diffuse 3 , 0x00FFFFFF
set vertexdata diffuse 4 , 0xFFFFFFFF
set vertexdata diffuse 5 , 0xFFFFFFFF
unlock vertexdata
endif
rem object properties
SetObjectDiffuse( ClickObj( n ).ConnectObj , ClickObj( n ).Color )
show object ClickObj( n ).ConnectObj
rem set active
ClickObj( n ).ConnectionActive = 1
ClickObj( n ).Obj1 = Obj1
ClickObj( n ).Obj2 = Obj2
rem position correctly
ControlClickObj()
endfunction n
function DestroyClickObjConnection( n )
rem logging
if ClickObj.logging = 1 then AddTextToConsole( "DestroyClickObjConnection " + str$(n) , 0xFF00FFFF , 1 , 0 )
rem make sure active
if ClickObj( n ).Active < 2
if ClickObj.logging = 1 then AddTextToConsole( "Not active!" , 0xFFFF0000 , 2 , 0 )
exitfunction -1
endif
rem destroy
if ClickObj( n ).ConnectObj > 0 then hide object ClickObj( n ).ConnectObj
ClickObj( n ).ConnectionActive = 0
endfunction n
function ControlClickObj()
rem local variables
local n as integer
local pos1 as vec3
local pos2 as vec3
local angle# as float
rem loop through all active
for n = 0 to ClickObj.CurrentMax
if ClickObj( n ).Active = 2
rem animation
if ObjectPlaying( ClickObj( n ).Obj ) = 0 then PlayObject( ClickObj( n ).Obj , 0 , 64 , 3 )
rem angle
TiltObjectToTerrain( ClickObj( n ).Obj )
rem life has run out
if ClickObj( n ).Life <> 0
if ClickObj( n ).Life - timer() < 0 then DestroyClickObj( n )
endif
endif
rem connection
if ClickObj( n ).Active > 1
if ClickObj( n ).ConnectionActive = 1
rem get object positions
pos1.x# = object position x( ClickObj( n ).Obj1 )
pos1.y# = object position y( ClickObj( n ).Obj1 )
pos1.z# = object position z( ClickObj( n ).Obj1 )
pos2.x# = object position x( ClickObj( n ).Obj2 )
pos2.y# = object position y( ClickObj( n ).Obj2 )
pos2.z# = object position z( ClickObj( n ).Obj2 )
rem calculate angle
angle# = wrapvalue( 270 - atanfull( pos1.z# - pos2.z# , pos1.x# - pos2.x# ) )
rem object properties
lock vertexdata for limb ClickObj( n ).ConnectObj , 0
set vertexdata position 0 , 0-newxvalue( pos1.x# , angle# + 90 , 0.1 ) , pos1.y# , 0-newzvalue( pos1.z# , angle# + 90 , 0.1 )
set vertexdata position 1 , 0-newxvalue( pos1.x# , angle# - 90 , 0.1 ) , pos1.y# , 0-newzvalue( pos1.z# , angle# - 90 , 0.1 )
set vertexdata position 3 , 0-newxvalue( pos1.x# , angle# - 90 , 0.1 ) , pos1.y# , 0-newzvalue( pos1.z# , angle# - 90 , 0.1 )
set vertexdata position 2 , 0-newxvalue( pos2.x# , angle# + 90 , 0.1 ) , pos2.y# , 0-newzvalue( pos2.z# , angle# + 90 , 0.1 )
set vertexdata position 4 , 0-newxvalue( pos2.x# , angle# - 90 , 0.1 ) , pos2.y# , 0-newzvalue( pos2.z# , angle# - 90 , 0.1 )
set vertexdata position 5 , 0-newxvalue( pos2.x# , angle# + 90 , 0.1 ) , pos2.y# , 0-newzvalue( pos2.z# , angle# + 90 , 0.1 )
unlock vertexdata
endif
endif
next n
endfunction
What else should I have called them? It makes sense to me in the above code to name them the same.
TheComet