different pointer same type
Blitz3D Forums/Blitz3D Programming/different pointer same type
| ||
what happens when you use a different pointer for the same type? e,g, if I create some types: s.ship= new ship s\hull=30 t.ship=new ship s\hull=30 then i do t.ship=each ship does it just go through the ones that I named t.ship or all of them? Is there any use for having different pointers? |
| ||
t.ship=each ship will go through each ship that you have created, regardless of what pointers they are assigned to. Any time you use something like (pointer) = new ship, its the 'new ship' part that is creating the type object. The type object exists independently of any pointer, even when it is not assigned to a pointer. One use for multiple pointers is to access the same data from different places (although that's probably not good programming practice). So for example, you might have a mothership object which contains a list of pointers for all the ships inside it, as well as a solar system object that has the same thing. Both would have a pointer to the same ship. |
| ||
can i have an example of accessing the data from different places? it sounds useful |
| ||
My method of choice for accessing and assigning data from type objects.Const SHIP_MAX%=32 ;Pre determined Limitation Dim shipID.ship(SHIP_MAX%) ;<-- array of types used as a pointer to specific object by primary key. Type ship Field id% ;<-- primary key Field hull% End Type Function shipNew.ship(id%,hull%) this.ship=New ship this\id%=id% this\hull%=hull% shipID(this\id%)=this ;<-- Pointer to specific ship by ID Return this End Function ;create some ships s.ship=shipNew(1,30) ;s.ship has id value of 1 t.ship=shipNew(2,40) ;t.ship has id value of 2 ;examples of accessing the data from different places ship.ship=shipID(1) ;assign s.ship to another pointer tshiphull%=shipID(2)\hull% ;access the hull value from t.ship ;display values Print ship\hull% Print tshiphull% |
| ||
thanks for the example, should tshiphull be t.shiphull? How does using different pointers help? I can see your use of an array of types but I dont understand why using different pointers makes any difference. |
| ||
When I get home, Slenkar - I'll try to post some snippets of code from my project. I need separate pointers, because I need a constant reference to a particular instance of a Type object, but at the same time, each loop, I am processing other commands to affect numerous instances of that same type. In effect, it's like: Type Tree Field Fruit$ Field Ripeness End Type .... For CheckFruit.Tree=Each Tree Temp=Rand(1,4) Temp=Temp + (CheckFruit\Ripeness) Checkfruit\Ripeness=Temp Next Text 10,300,SpecificFruit\Fruit$,0,1 Text 10,330,SpecificFruit\Ripeness,0,1 ------------EDITED COS MY SPEELING IS ATROCIOUS!------------ |
| ||
I think Im starting to get it, you are printing the temp of a single fruit (otherwise it would get messy on screen) using a unique pointer. How do you set which instance gets called by "specificfruit". |
| ||
should tshiphull be t.shiphull? No because tshiphull is a integer variable that stores the shipID(2)\hull% integer value. I could have labeled this var hullfortship% :) I can see your use of an array of types but I dont understand why using different pointers makes any difference. In the method I described you can specify pointers to define objects properties at design time from within an editor, script, etc. For example, In my RPG there are many Switches to trigger doors, platforms, and monsters. Using the system I described above allows me to assign specific switches to specific doors, platforms, etc within the level. Thus, SwitchID(2) open/close DoorID(4), SwitchID(1) activates Platform(3). Some game objects dont require you to identify them in any specified manner. Take Asteroids for instance. Usually, Asteroids move until they collide with the player's ship, exploding upon impact. The behavior is simple and the result is the same no matter what Asteroid collides. Pointers would not be necessary in this scenerio. However, I have found it to be good pratice to specify pointers for complete control. |
| ||
I think Im starting to get it, you are printing the temp of a single fruit (otherwise it would get messy on screen) using a unique pointer. How do you set which instance gets called by "specificfruit". ------------- Almost - I am increasing ALL the fruits' ripeness by a random amount each loop. SpecificFruit will have been set elsewhere, either by a larger For/Each loop with some sort of Field argument i.e. If SpecificFruit\Fruit$="Apple" .... or by defining a Global Type to point here (as explained to me here) You'll see my game has nothing to do with fruit or trees at all, I was just tryiong to find a simpler example! |
| ||
As promised (although a little overdue) here's my usage of the Global Type pointer. What I needed was the variable 'TargetEntity' to equal the instance of the RadarTargets Type when that instance was currently 'selected' as the target in-game. However, many other routines needed to check RadarTargets (for placing them all on my 3D radar, labelling them etc.) Also TargetEntity is used a lot because a special crosshair may be pinned to it, and an alternative radar camera was used to display it elsewhere on-screen. Global CurrentTarget.RadarTargets .............. RadarSorter.RadarTargets=First RadarTargets TargetEntity=RadarSorter\TargetEntityHandle ...... ;CHANGING RADAR PlaySound radar_blip Target_Sorter_First.RadarTargets=First radartargets Target_Sorter_Last.RadarTargets=Last radartargets If targetentity=0 targetentity=Target_sorter_First\TargetEntityhandle CurrentTarget=First RadarTargets EndIf For CountTargets.RadarTargets = Each RadarTargets If targetentity=CountTargets\TargetEntityHandle Then CurrentTarget=CountTargets Next If changetarget$="Forward" If Null <> After CurrentTarget CurrentTarget = After CurrentTarget Else CurrentTarget = First RadarTargets EndIf EndIf If changetarget$="Back" If Null <> Before CurrentTarget CurrentTarget = Before CurrentTarget Else CurrentTarget = Last RadarTargets EndIf EndIf targetentity=CurrentTarget\TargetEntityHandle .................................. For SelectTarget.RadarTargets=Each Radartargets If targetEntity=SelectTarget\TargetEntityHandle skip=0 EntityColor radar_pointer,SelectTarget\TargetColourRED,SelectTarget\TargetColourGREEN,SelectTarget\TargetColourBLUE Color SelectTarget\TargetColourRED,SelectTarget\TargetColourGREEN,SelectTarget\TargetColourBLUE Text radarX+(Radarlength/2),RadarY-(small_text_size*2.5),SelectTarget\TargetType$,1,0 Text radarX+(Radarlength/2),RadarY-(small_text_size),SelectTarget\Name$,1,0 If EntityDistance (targetentity,ship)>radar_range# Or targetentity=sunsprite Or targetentity=sunsphere Text radarX+(Radarlength/2),RadarY-(small_text_size*5),"OUT OF RANGE",1,0 skip=1 EndIf If EntityDistance (targetentity,ship)<=radar_range# And skip=0 Text radarX+(Radarlength/2),RadarY-(small_text_size*5),Int(EntityDistance(Ship,Targetentity)),1,0 EndIf EndIf Next |