Doubble Functions?
Blitz3D Forums/Blitz3D Beginners Area/Doubble Functions?
| ||
Hi once agian I ran into a real noobie problem, i was wondering how in the world do you get two functions to work at the same time. I need a timer to run while you are playing the game, When I put the Timer() function before the function Ball1Main() the Timer() function is the only one that will run. But when they are switched the Ball1Main() function works while the Timer() function dosen't. An example: If a=0 setbuffer backbuffer() While Not Keydown(1) Timer() ;<\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ;They need to work together. Ball1Main() ;<//////////////////////////// Wend EndIf Also I have tryed to put the two functions into one, and agian it wont work as one, it works as two. One of the scrips takes over and the other one dosent even show. |
| ||
You can't (at least in Blitz, which doesn't support threads). What people typically do is called it game loop. It goes something like this: Repeat Do_a_little_bit_of_this() And_that() Redraw_everything_in_its_new_position Forever So you need to restructure your code. If you post more we might be able to be more specific. |
| ||
do not put While or Repeat commands inside the function timer() or Ball1Main() |
| ||
@Ryan, as the other posts already spotted, you can't run two functions in the same time. To to that, you need thread support, which is not implemented in B3D. That means that the statement you write when you code, are later executed sequentially. The trick is, they are executed so fast that it seems that they runs at the same time. You may find out, using a sort of pseudo code, what your program should do, and divide each part of it in separated sections; for example, in a game like pong where a ball is moved and bounced, you may have these functions in your main loop: ;Main loop while not keydown(1) ;while esc is pressed ; Cls ;clear the screen player_control() move_and_bounce_ball() draw_scenario() draw_ball() ;... and more Flip wend end The two functions above will be executed sequentially, that is, the ball is first 'moved' - that is, you update the ball coordinates, then the ball is drawn on the screen. You have already good knowledge about flow control and backbuffer, as I see above; but you need to sort out first some ABC of programming, so my suggestion is to read some tutorial here and @blitzcoder. Have fun, Sergio. |
| ||
Well its nice to know im good at one thing, flow and control :p. And why can't I have While or Repeat commands in my functions? Because I do have a few While commands but I see no harm done. |
| ||
And why can't I have While or Repeat commands in my functions? Because I do have a few While commands but I see no harm done. Because they're stopping program execution going back to the main while loop which calls both functions, and is causing the program to not do both things. |
| ||
If you have a While or Repeat inside a function, it will be stuck inside that function until the While or Repeat conditions are met. That's why you only get one thing running. I'm not sure what Timer() does in your program, I'm assuming something like this:Function Timer() time = Millisecs() While Millisecs() < time + 30000 ;wait 30 seconds Text 10,10,(Millisecs - time) Flip Wend Text 10,10,"Time's up!!!" End Function problem here is that the program remains in Timer() until the complete 30 seconds is up, not executing anything else. If you use something like this, it'll work better: ...some setup code Global time Global timeup = False ;main program time = Millisecs() While Not timeup Timer() ... Do other stuff Flip Wend Text 10,10,"Time's Up!!!!!!" ...Some other code Function Timer() If Millisec >= timer + 30000 Then timeup = True End Function My code is missing some elements like setting graphics mode and actually doing anything while the timer runs, just showing you the basic flow here. |
| ||
well, what i have works fine with the while functions in it, and my timer is just a 30min count down. |
| ||
Turkey you can use while and repeat inside your functionsRepeat a=a+1 Until a=100 what they mean is that you can't do things like this... While not Keydown(1) Wend ;or Repeat Forever If you can post the code for your Timer() function, we might be able to help. |
| ||
Ok I'm still having trouble getting this to work. Here are parts of my scrips so you might be able to help me get this timer going. ;This is part of the main program. I want Timer() and Ball1Main() to work as one. While Not KeyDown(1) If KeyDown(48) Then ;If B is pressed then... Cls Timer() Dim ball1image(500) Ball1Main() Flip ;-------------------------------------------------------------- Function Ball1Main() ;IMAGES ball1 = LoadImage("ball 1.bmp") ;Load Ball 1 image Cls ;Clear the screen SetBuffer BackBuffer() ;Set buffer to back buffer TFormFilter 1 For a = 0 To 360 rr = CopyImage(ball1) ball1image(a) = rr MidHandle ball1image(a) Next While Not KeyDown(1) Cls If MouseDown (1) r.ball1 = New ball1 r\x = Rand(1,800) r\y = Rand(1,600) r\angle = 0 r\speed = Rand (1,10) r\turnrate = Rand (1,10) End If For h.ball1 = Each ball1 h\iangle = ATan2((MouseY()-h\y),((MouseX()-h\x))) xmov# = Cos(h\angle) * h\speed ymov# = Sin(h\angle) * h\speed h\x = h\x + xmov# h\y = h\y + ymov# realangle_current = h\angle realangle_intend = h\iangle If h\angle < 1 realangle_current = 180+(180 - Abs(h\angle)) End If If h\iangle < 1 realangle_intend = 180+(180 - Abs(h\iangle)) EndIf If realangle_intend > realangle_current And h\iangle > 0 Then h\angle = h\angle + h\turnrate If realangle_intend > realangle_current And h\iangle < 1 Then h\angle = h\angle + h\turnrate If realangle_intend < realangle_current And h\iangle > 0 Then h\angle = h\angle - h\turnrate If realangle_intend < realangle_current And h\iangle < 1 Then h\angle = h\angle - h\turnrate If realangle_current> 360 Then realangle_current = 1 If realangle_current< 1 Then realangle_current = 360 DrawImage ball1image(realangle_current),h\x,h\y ;Draw Ball1Image Next Flip Wend End Function ;--------------------------------------------------------------------- Function Timer() Start = MilliSecs () Finish = Start + HowLong ; Start Finish ; | ----> | ; v v ; , _,___,___,___,___,___,___,___,___,___,_ , ; | | | | | | | | | | | | | | | | | | | ; | | ; |<----------------->| ; 75000 ms SetBuffer FrontBuffer() Repeat minutes = Floor((Finish - MilliSecs ())/60000) seconds = Ceil(((Finish - MilliSecs ()) - minutes*60000)/1000) If seconds < 10 Then Text 0 , 0 , minutes + ":0" + seconds Else Text 0 , 0 , minutes + ":" + seconds End If Flip Cls If KeyHit(1) Then Exit Until MilliSecs () >= Finish End End Function ;------------------------------------------------------------------ If you could tell me how to fix this I'd be the happiest man in the world. Also how can I get my scrips to show up in that black background with green text? |
| ||
http://www.blitzbasic.com/faq/faq_entry.php?id=2 It's not the same code you posted, but it demonstrates the steps necessary to get it functioning correctly. |