"global" function
This is VB, but I don''t believe it''s language specific..
I have two slightly different types (type1 and type2) .The other type has about 6 variables more than the other. Then there is a function that initializes objects (Function init(obj as type1)). How can I call this function with obj as type2?
I know it''s not possible directly, but I''d like keep my code short (and more flexible, I don''t want to have a different init procedure for all the different objects). Init does the same thing for all kinds of objects anyway (load texture, set basic color, set rect, start pos etc.).
Various Types are needed because there''ll be various objects (map objects, screen, hud, particles etc.).
It''s kind of difficult to explain this, hope you got it
If it''s of any help, here are the types:
Public Type type1
pos As POINTFLOAT
width As Integer
height As Integer
r As RECT
texture As Direct3DTexture8
color As D3DCOLORVALUE
End Type
Public Type type2
pos As POINTFLOAT
width As Integer
height As Integer
r As RECT
texture As Direct3DTexture8
color As D3DCOLORVALUE
rotation As Single
speed As Single
heading As Single
state as long ''flags: damaged, injured, destroyed and so on...
End Type
Well, I''d use inheirentence, but you''re using VB so thats not an option for you. If you want to write code like that, you might want to start learning C++.
To do something like that in VB, have type2 contain a type1, and then the six bonus data members. You still need an InitType2 function, but now it can call the InitType1 with the type1 property, and init its own 6 bonus properties.
To do something like that in VB, have type2 contain a type1, and then the six bonus data members. You still need an InitType2 function, but now it can call the InitType1 with the type1 property, and init its own 6 bonus properties.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
You can change the parameter of your procedure from "As type1" to "As Variant", if it''s not in an object module. If it''s in a normal code module, then that is an option for you.
Then, as long as you access ONLY the shared variables, from type to type, in the procedure, it should work. I haven''t tried before. I just do something like this...
Public Type Type1
pos As POINTFLOAT
width As Integer
height As Integer
r As RECT
texture As Direct3DTexture8
color As D3DCOLORVALUE
End Type
Public Type Type2
SharedData As Type1 ''Or whatever you want to call it...
rotation As Single
speed As Single
heading As Single
state as long ''flags: damaged, injured, destroyed and so on...
End Type
Hope this helps!
Then, as long as you access ONLY the shared variables, from type to type, in the procedure, it should work. I haven''t tried before. I just do something like this...
Public Type Type1
pos As POINTFLOAT
width As Integer
height As Integer
r As RECT
texture As Direct3DTexture8
color As D3DCOLORVALUE
End Type
Public Type Type2
SharedData As Type1 ''Or whatever you want to call it...
rotation As Single
speed As Single
heading As Single
state as long ''flags: damaged, injured, destroyed and so on...
End Type
Hope this helps!
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
As a person learning Visual Basic, I am obliged to ask:
Is there not a way to clear a certain amount of memory, akin to the C++ code:
Maybe there''s some kind of Windows API function that does this?
Doubt it, but hey, I''m obliged to ask .
Otherwise, take a serious look at C++.
The_Minister
1C3-D3M0N Interactive
Is there not a way to clear a certain amount of memory, akin to the C++ code:
ZeroMemory(Variable, sizeof(Variable))
Maybe there''s some kind of Windows API function that does this?
Doubt it, but hey, I''m obliged to ask .
Otherwise, take a serious look at C++.
The_Minister
1C3-D3M0N Interactive
[email=mwronen@mweb.co.za" onmouseOver="window.status='Mail The_Minister'; return true" onmouseOut="window.status=' '; return true]The_Minister[/email]1C3-D3M0N Interactive
try something like this:
function Init(obj as Variant)''Initialise common membersobj.pos = whateverobj.width = whateverobj.height = whateveretc etc etc...''Now do the specificsif typename(obj) = "type2" then obj.rotation = whatever obj.speed = whatever ''etcend if''do one of the above if-blocks for each sub-type. if you''re''careful with your inheritance, it will minimise the number''of such blocks that you needend function
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement