Global TYPEs
Blitz3D Forums/Blitz3D Programming/Global TYPEs
| ||
I have a TYPE defiened something like this TYPE Ship Field X Field Y END TYPE Is it possible to create a SHIP Object inside a funtion and change values inside another? Something like this: Function Create() Ship.Ship = New Ship End Function Function Change(Ship.Ship) Ship\X = 10 Ship\Y = 20 End Function |
| ||
Yep, that's entirely possible. Types are global, but to get to a specific type instance, you must use either it's handle or give it an ID, unless you want to mess about with the Object() and Handle() commands. Something like this should work: TYPE Ship Field id Field X Field Y END TYPE ImAShip = CreateShip() ImAnotherShip = CreateShip() MoveShip(ImAShip,5,10) MoveShip(ImAnotherShip,100,24006583) Function CreateShip() Ship.Ship = Last Ship If Ship = Null id = 1 Else id = Ship\id + 1 End If Ship.Ship = New Ship Ship\id = id Return id End Function Function MoveShip(id,movex,movey) For Ship.Ship = Each Ship If Ship\id = id Ship\x = Ship\x + movex Ship\y = Ship\y + movey End If Next End Function You can do the same with Handle and Object, but they are a little more tricky. Try searching the forums for them, to see how they are used. Fredborg |
| ||
afaik types are not global by default.... |
| ||
You don't even need ID values if you don't want. If you have a certain type that you want to be global throughout the program, just create a Global Type Pointer like the following re-write of FredBorg's code:TYPE Ship Field X Field Y END TYPE Global ImAShip.Ship = CreateShip() ;<------ GLOBAL Global ImAnotherShip.Ship = CreateShip(10,10) ;<------ GLOBAL MoveShip(ImAShip,5,10) MoveShip(ImAnotherShip,100,24006583) Function CreateShip.ship(x=0, y=0) ; Create a LOCAL Ship pointer and assign its ; values. Then return the value of the Ship type pointer ; To whatever called the function.. ThisShip.Ship = New Ship ThisShip\x = X ThisShip\y = Y Return ThisShip End Function Function MoveShip(thisship.ship, movex, movey) ThisShip\x = ThisShip\x + movex ThisShip\y = ThisShip\y + movey End Function You could also use an array of Ship pointers. Types are VERY flexible!! :D :D :D Wiebo: Well, the types themselves ARE global. (That is, the list of types themselves. And it's for this reason you can create "preserved redimensional arrays" of types...) But when you create a type POINTER, it is local by default. eg bad.baddy = New baddy <--- "bad" is a local pointer to a new baddy. The baddy is global until you delete it, but "bad" will lose the value of the baddy it's pointing to when you leave a function, unless you specify it as being a global pointer variable. |
| ||
[ double post -- I don't know how this got in here so delete it, mods, please ] |
| ||
Or the superior object/handle method:TYPE Ship Field X Field Y END TYPE Global ImAShip = CreateShip() Global ImAnotherShip = CreateShip(5,2) MoveShip(ImAShip,5,10) MoveShip(ImAnotherShip,100,24006583) Function CreateShip(x=0,y=0) Ship.Ship = New Ship Ship\x = x Ship\y = y Return Handle(Ship) End Function Function MoveShip(n,movex,movey) Ship.Ship = Object.Ship(n) If Ship = Null Then Return False Ship\x = Ship\x + movex Ship\y = Ship\y + movey End Function Fredborg |
| ||
Why is this supposed to be superiror in that case? I mean Object and Handle are useful, but the only thing you changed (compared to the one of cyberseth, which is the one maverick69 should use IMHO) in your version is to test if Ship is Null in MoveShip, and you clearly don't need Object and Handle for that: In Blitz when you delete an instance every reference to that instance becomes instantly Null (it's a so-called "weak reference"). Or am I missing something? Plus using Object/Handle seems to involve an additional memory hit (probably because Blitz needs to add a node in some internal strucutre to retireve the reference given the handle), so it's better to use it only when you have a real benefit. |