Stupid Q about Types
Blitz3D Forums/Blitz3D Beginners Area/Stupid Q about Types
| ||
I'm tired and feeling dumb and need assistance. Trying to add some meanies to my game. Type "meanie" all set up etc, but I'm stupidly stuck trying to actually create and render them. Here's my meanies.bb which is included from the main program: Global num_meanies%=0 Const MEANIE_BIGSPHERE = 1 Const MEANIE_MEDIUMSPHERE = 2 Const MEANIE_SMALLSPHERE = 3 Type meanie Field typeof% Field x# Field y# Field dx# Field dy# Field hue# Field hitpoints% End Type Function add_meanie.meanie(this.meanie,meanie%,x#=0,y#=0) If (x#=0 And y#=0) angle#=Rnd#(0,359.99) radius#=GraphicsHeight()/2 ; !! Need to change for wide displays. x#=(Sin(angle#)*radius#)+(GraphicsWidth()/2) y#=(Cos(angle#)*radius#)+(GraphicsHeight()/2) EndIf Select meanie% Case MEANIE_BIGSPHERE this.meanie = New meanie this\typeof% = MEANIE_BIGSPHERE this\x# = x# this\y# = y# this\dx# = Rnd#(-.5,.5) this\dy# = Rnd#(-.5,.5) this\hue# = 0 this\hitpoints% = 50 num_meanies% = num_meanies+1 End Select Return this End Function Function render_meanies(this.meanie) For this.meanie = Each meanie this\x#=this\x#+this\dx# this\y#=this\y#+this\dy# Oval this\x#,this\y#,128,128,True Next End Function Here's the relevant excerpt from the main program: While Not (KeyHit(KEY_ESC) Or KeyHit(KEY_3) Or KeyHit(KEY_NUM_3)) If Rand(0,49)=0 menu.meanie = add_meanie(MEANIE_BIGSPHERE) EndIf render_meanies(menu) render_menu(MENU_MAIN) Flip Cls Wend I get an illegal type conversion where the main program tried to create a meanie. I know, it's probably obvious. I'm just too tired to know what it is I need to change :) |
| ||
Function add_meanie.meanie(this.meanie,meanie%,x#=0,y#=0) should be Function add_meanie.meanie(meanie%,x#=0,y#=0) |
| ||
DUH! *embarrassed* Thanks Octo. **EDIT** Argh! Now it creates a meanie but then it immediately disappears. Sod it, I'm going to bed (well, update blog first). I've not been well you know %^( |
| ||
No worries. Hmm.. A couple things that won't cause bugs, but are a little weird: menu.meanie = add_meanie(MEANIE_BIGSPHERE) No need to capture the return value if you don't need it. Also, are you sure you want to use the variable name "menu"? render_meanies(menu) ;;; Function render_meanies(this.meanie) render_meanies() doesn't operate on a single meanie, so there's no point in passing it one. It shouldn't even take one as an argument: function render_meanies() Finally, you might consider moving common code outside of your Select Case block(s), but that's a style issue. It'll cut down on duplicated code later: this.meanie = New meanie this\typeof% = MEANIE_BIGSPHERE this\x# = x# this\y# = y# num_meanies% = num_meanies+1 Select meanie% Case MEANIE_BIGSPHERE this\dx# = Rnd#(-.5,.5) this\dy# = Rnd#(-.5,.5) this\hue# = 0 this\hitpoints% = 50 End Select |
| ||
Argh! Now it creates a meanie but then it immediately disappears. Hmm, the only thing I can think of with the code supplied is that your frame rate might be rediculously high and the meanie is moved off-screen by your position updating code before you get a chance to see it? Throw a Stop in your render_meanies() function and step through it, watching to make sure your values of \x and \y are reasonable. Or, if you need to wait until one's created first: if keydown(88) then stop ; debug on <f12> EDIT: Flip() is supposed to do a vwait, so that's probably not it after all... |
| ||
implementing your suggestions. I seem to have a basic misunderstanding of Blitz's object handling. Back to school for me... It is a menu.meanie because it is part of the attract sequence and not a game.meanie ;) How does render_meanies() know what meanies to render if I don't tell it? I'll either want it to render the meanies specific to attract mode (side effect of attract mode returning to the state it was prior to the player starting a new game) or meanies specific to in-game mode. I don't understand how it can loop through and render meanies if I do not give it an object to work with. The code in the select block will end up being like so: Select meanie% Case MEANIE_BIGSPHERE this\dx# = Rnd#(-.5,.5) this\dy# = Rnd#(-.5,.5) this\hue# = 0 this\hitpoints% = 50 Case MEANIE_SEEKER_SWARM Create a bunch of separate meanies Place them in a specific arrangement Case MEANIE_SWIRLER_CLUSTER add_meanie(MEANIE_SWIRLER) a bunch of times End Select This just means that instead of different functions to create different meanies, I can just put their unique startup code in the Select block. How they get updated will depend on meanie.typeof, which will trigger a Select block in render_meanies. |
| ||
You're bloody right though. I took out all references to menu.meanie, called render_meanies() with no arguments, add_meanie() returns nothing. Now it works. What the hell?! http://www.stefanholmes.com/Spheres-alpha-0.05.rar |
| ||
Blitz secretly maintains a linked list of all your objects for each type. When you use For..Each, you get all of them for that type. It is a menu.meanie because it is part of the attract sequence and not a game.meanie menu.meanie and game.meanie are single pointer variables, they only point to one object each. If you want to maintain separate collections of meanies there are many different ways to do this: loop over all with Each skipping the ones you don't want; store pointers in separate typed arrays; turn your objects into linked-list nodes; or use a container class to manage your Object(Handle())s. |
| ||
Grief. I'm going to read up on your container class. I'm obviously mired in a deep pit of ignorance and I need to climb out. Remind me again, Basic is supposed to be easy, right? ;) |
| ||
I doubt the pit is very deep. I suspect it's only one or two concepts that are eluding you, after which everything will fit into place. I'd glad to hear you're interested in my container classes. My tutorial starts off with a primer on how pointers and objects work, but I'm not sure how accessible it is. I'd appreciate any feedback on what you have trouble understanding so I can improve it. |