Swap Type.

Blitz3D Forums/Blitz3D Programming/Swap Type.

Paul "Taiphoz"(Posted 2004) [#1]
Type Gems
	Field x
	Field y
	Field z
	Field model
	Field gem_type
	Field selected
	Field points
	Field life
	Field name$
	Field move%
End Type
global gem.gems = new gems : delete gem


Thats my type. Iv made it global to the full code.

I cant make it an array of types. as this wont fit what im doing.

I need a way to take two of these type instances and swap them and all of their values.

Ta for any suggestions.


Michael Reitzenstein(Posted 2004) [#2]
Swap the pointers.


Michael Reitzenstein(Posted 2004) [#3]
And what exactly does this line do!?!
global gem.gems = new gems : delete gem



Paul "Taiphoz"(Posted 2004) [#4]
I tried to swap them and couldnt get it to work hence me asking here.

So if you can write a function id like to see it.


EOF(Posted 2004) [#5]
Heres a SWAP function to try.
It needs expanding to cover all of your fields:

Type Gems
 Field name$
 Field worth
End Type

Global gem.Gems
Global swap.Gems

gem=New Gems
gem\name="silver" : gem\worth=120
gem=New Gems
gem\name="red" : gem\worth=250
gem=New Gems
gem\name="gold" : gem\worth=170
gem=New Gems
gem\name="ruby" : gem\worth=135

ListGems
Print "Swapping 'red' and 'ruby'"
SwapGems "red","ruby"
ListGems

WaitKey
End

Function ListGems()
 Print ""
 Print "------[START]------"
 For g.Gems=Each Gems
  Print "Name = "+g\name+"       Worth = "+g\worth
 Next
 Print "-------[END]-------"
 Print ""
End Function

; swap 2 gems by name
Function SwapGems(n1$,n2$)
 Local flag=False
 For g1.Gems=Each Gems
  If g1\name=n1$
   For g2.Gems=Each Gems
    If g2\name=n2$
     ; swap the variables of the 2 types
     swap.Gems=New Gems
     swap\name=g1\name : g1\name=g2\name : g2\name=swap\name
     swap\worth=g1\worth : g1\worth=g2\worth : g2\worth=swap\worth
     Delete swap
     flag=True : Exit
    EndIf
   Next
  EndIf
  If flag=True Exit
 Next
End Function



DL(Posted 2004) [#6]
Assuming you have two types to swap, red.Gems and blue.Gems...

temp.Gems = red

red = blue
blue = temp

delete temp


Gabriel(Posted 2004) [#7]
What DL said, but don't delete temp. They're just pointers so if you delete temp, you're in fact deleting blue.


Yan(Posted 2004) [#8]
Or...red even...;op

Sorry, but my pedantic bone is just tooooo big for me to resist ;o)


YAN


Gabriel(Posted 2004) [#9]
You may be pedantic, but you're also wrong ;)

It's red before the swap. After the swap, red has become blue and vice versa ;)


Yan(Posted 2004) [#10]
Yeah, but...

Type col
	Field s$
End Type

red.col = New col
red\s$ = "I am red"

blue.col = New col
blue\s$ = "I am blue"

temp.col = red

red = blue
blue = temp

Print "'red' = " + red\s$

Print "'blue' = " + blue\s$

Print "'temp' = " + temp\s$

WaitKey()

End
See...It's still red. It's just that blue is now pointing to it! ;o)


YAN


Paul "Taiphoz"(Posted 2004) [#11]
Yav your way is better and is the way its being done now. the thing is I had it working right from the start. Seems that aftera fresh pair of eyes had a look at it, that the bug was being caused by some other code.

Something I should have seen myself but due to little sleep I just overlooked it.


DL(Posted 2004) [#12]
...er..um..yeah The delete was a brain fart, sorry.