Type Functionality

BlitzPlus Forums/BlitzPlus Programming/Type Functionality

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Say, that's pretty good. Organized and logical.

Anyway, (ah!) I understand your issue now. No, you do not have to cycle through the Type list each time you want to find a specific type. Remember, you can make yourself a Type pointer (a variable that represents one of the Type instances), like this, for example:
Global CurrentUnit.unit = Null

This is just a pointer that is pointing to nothing. It is ready to be assigned to any new instance that you create. Of course you can also do something like this:
Local Enemy.unit = New unit

This creates a new Type instance, automatically inserts it at the end of the internal Type list (as you know), and assigns the variable 'Enemy' to that single instance.

Remember that once you define a custom type, you can declare it and use it just like you would any other variable type, e.g. VariableName[type], where [type] is '%', '#', '$', or '.unit'.

So now, in your Clicked() function, where you are selecting the unit, you might say this:
	UnitSelected = True
	For Soldier.unit = Each unit
		If Soldier\x = CursorX And Soldier\y = CursorY Then
			Soldier\Selected = True
			CurrentUnit = Soldier ; <-- new line
			Movement(Soldier\x,Soldier\y,Soldier\move) ;remove later
		End If
	Next


...and in your Movement routine, you won't have to cycle through each Unit instance. You just say Soldier = CurrentUnit.

As a side effect, you don't need UnitSelected. You can test that like this: if (CurrentUnit <> Null) then ...

And if you really wanted, when you create each Unit instance, you could put each one in an indexed array, just like this:

Dim Units.unit(NUMUNITS)
For i = 0 to NUMUNITS
     Units(i) = new unit
     Units(i)\x = ...
     Units(i)\y = ...
Next


Then you can access any one you want, directly.

Anyway, I hope this helps. Ask if you have any questions.