Question about Types (B3D)
Blitz3D Forums/Blitz3D Programming/Question about Types (B3D)
| ||
I had a brainwave about how to optimize my code, but then it fell flat. I had been doing the following:house.building = New building house\id = nextid: nextid = nextid+1 space.room = New room space\buildingID = house\id ... in order to cross reference rooms with the building they belong to. But then every time I wanted to know which building a room belonged to, I had to do this: space.room = First room house.building = First building While house\id <> space\buildingID house = After house Wend ... in order to iterate through the buildings until I found the right one. Then I read more about types and the type identifier and I *thought* I could do this: house.building = New building space.room = New room space\buildingID = house But I get an "Illegal Type Conversion" error. I think it's a shame I can't store the type identifier as a variable, for future use. Is this something that is legal in BlitzMax? Even better, is it possible in B3D? |
| ||
I just tried this too:Function FindBuilding(id) For b.building = Each building If b\id = id Then Return b Next End Function ... and that doesn't work either! I could swear I've seen that in some examples before... |
| ||
This should help you ... not a fully working demo .. Type Building Field x, y, z Field Otherstuff End Type Type Room Field ID.Building Field x, y, z Field Otherstuff End Type CHURCH.building = CREATEbuilding( 120 , 100 , 1800 ) room1.room = CREATEroom( CHURCH , 10, 10, 10 ) room2.room = CREATEroom( CHURCH, 150,10,80 ) ;access info from the building that the room is in Text 0,0,room1\ID\x Text 0,10,room1\ID\y Text 0,20,room1\ID\z Function CREATEbuilding.Building( x, y, z ) b.building = New building b\x = x b\y = y b\z = z Return b End Function Function CREATEroom.room ( b.building , x, y, z ) r.room = New room r\ID = b r\x = x r\y = y r\z = z return r End Function |
| ||
Your particular code gets an error... but wow - THANK YOU! Types within types: That's *exactly* what I was looking for and it's not only chopped my code in half and made it more succinct, it's made a lot more sense of types for me. BRILLIANT! |
| ||
Your illegal conversion error... Just use in your example above Return Handle(b) Function FindBuilding(id) For b.building = Each building If b\id = id Then Return Handle(b) Next End Function and recall it with house.building=Object.building(TheReturnedValue) Has the advantages: Code is easier to read and you can put FindBuilding in your decls file to have the Function highlighted what is afaik not possible if you use for example Function CreateBuilding.building(id) ... ... End Function Btw: There's a thread: only 1 day old http://www.blitzbasic.com/Community/posts.php?topic=45753 where you could have read more about it... Inclusive a working example |
| ||
Excellent! Thanks for that too - I wondered how Handle and Object were used. The more I find out about types the more excited I get about getting stuck back in to my game. In the other thread, there were Functions with Types... how does that work? ie: Function house.building(id) I couldn't tell from the thread whether that was legal code or not. |
| ||
Oops .. just coded staight into a text editor sorry ... didn't test with BB, the room function should have "return r" at the bottom but I guess you figures that one out. Glad it helped. Steven |
| ||
Aaaahhh... again thanks. I'm having a very good day learning wise. |
| ||
Oops again .. update the code above .. there was no .room type extension on the Room1 and Room2 create statements. Stevie |
| ||
Really getting to grips with it now. How about this - is this legal? Will try later on but I'm at work now:Type house Field x, y, z End Type Global hse.house Is the Global declaration okay? |
| ||
Yes. |
| ||
Wicked. Thanks again... |
| ||
If you want a function to return a handle to an object (custom type), do this:Function FindBuilding.building(id) For b.building = Each building If b\id = id Then Return b Next End Function ; Find the building with id = 5 ABuilding.Building = FindBuilding(5) The ".building" at the end of the function name states that the function will be returning a pointer (= handle) to a building-instance. It's the same as having a function that would return a string: Function GetFirstChar$(aString$) Local b$ = Mid$(aString$, 1, 1) Return b$ End Function Here the "$" states that the function will be returning a string. Really getting to grips with it now. How about this - is this legal? Will try later on but I'm at work now: Type house Field x, y, z End Type Global hse.house Is the Global declaration okay? This works, but you didn't create a house-instance yet. The line "Global hse.house" just creates a global variable that is able to hold a handle to a house-instance, but the variable is still empty. To create one, use this: Global hse.house = New house |
| ||
Thanks - yeah, figured the second bit but wondered if you had to have a building instance before declaring the global - seems not. |