Good Programming Habits
Blitz3D Forums/Blitz3D Programming/Good Programming Habits
| ||
I've written this little routine that creates a terrain mesh for me, but at the mo its got hardcoded variables.. I'd like to seperate it from my main program loop and make it into a function that I could call, whenever its needed. But, I'm not too sure how to go about it. Here's some snippets of the code: Global terrain_pivot=CreatePivot() Global mesh_land=CreateMesh(terrain_pivot) Global surf=CreateSurface(mesh_land) I need these, but I realize I cant put them into a function. Then comes the rest of the code.. ... AddVertex surf,x+cenx#,y+ceny#,0,0,0,0 0 AddVertex surf,x+cenx#,(y+1.0)+ceny#,0, 1,0, 0 ... As you can see I'm using the global handle surf, which is fine if I'm only calling the function once - but if I want to use it twice ?. Ideally, I'd just like to call the function, like this: global terrainpatch=make_terrain(terrainpatch) And that would be that !. But I'm just alittle puzzled how I can go about it. Any Help Guys ?...pretty please !. |
| ||
I'm not sure what you mean by terrainpatch in the function call, but I was thinking somthing like this:global terrainpatch=make_terrain(terrainpatch) function make_terrain() mesh_land=CreateMesh(terrain_pivot) surf=CreateSurface(mesh_land) ... AddVertex surf,x+cenx#,y+ceny#,0,0,0,0 0 AddVertex surf,x+cenx#,(y+1.0)+ceny#,0, 1,0, 0 ... return mesh_land end function ;then in the case that you wanted to get the surface again, surf=getsurface(mesh,1) |
| ||
You need to use types, and want to be doing this kind of thing (note that this code won't run on its own!):; Terrain 'object'... add whatever fields you require... Type Terrain Field terrain_pivot Field mesh_land Field surf Field number ; Example field to receive parameter passed in function below... End Type ; Function to create Terrain object -- note return type! Function CreateNewTerrain.Terrain (example) t.Terrain = New Terrain t\terrain_pivot = CreatePivot () t\mesh_land = CreateMesh (t\terrain_pivot) t\surf = CreateSurface (t\mesh_land) t\num = example ; Etc... Return t End Function ; D E M O . . . ; Get a pointer to the object that is created and returned... test.Terrain = CreateNewTerrain (1) Print test\num ; Would be '1'... ; Move the resulting terrain... MoveEntity test\terrain_pivot, 0, 10, 0 ; Etc... Let me know if any of that needs further explanation! |
| ||
Thanks Guys, just what I was after. James, I'm a reasonable C coder that can use structures - but for some reason I just cant get my head around blitz'es types !?. (all this '.' and '\' !) Cheers for the example code. ;) |
| ||
James, I'm a reasonable C coder that can use structures - but for some reason I just cant get my head around blitz'es types !?. (all this '.' and '\' !) There is a trick to it. Once you realize that (a) '.' isn't the member operator ('\' is), and (b) the type designation always comes AFTER the variable name, it gets a lot easier. e.g. int: a% float: a# string: a$ custom: a.custom <-- '.' is type declaration and then the neumonic for remembering \ is that \ is analogous to a directory. WHen you want to get at a field that "in" a certain type, use '\'. a\field1 <-- this type is already instantiated |