ClearWorld and Includes??

Blitz3D Forums/Blitz3D Programming/ClearWorld and Includes??

Naughty Alien(Posted 2008) [#1]
..well..its very straightforward what to do with this command..but Im wondering, how is it related to all includes within program..let say i have game consist of few levels...so take a look at this structure:

Include"Hot stuff1.bb"

Include"Hot stuff2.bb"

Include"Hot stuff3.bb"

Load everything, setup or whatever before main loop

'main loop magic
While not KeyDown(1)

do incredible stuff here
UpdateWorld
Renderworld
Flip
Wend

;Im wondering whats happening with included Hot stuff at begining, after next line

ClearWorld

;From here I would like to load next level, BUT im wondering, where are my includes from begining..they are still with me after Clearworld or not??



:)


Moraldi(Posted 2008) [#2]
After the ClearWorld statement all of your resources are lost. You need to reload all of your stuff again:
You need a 'global loop' before the 'main loop magic' to direct execution before the first include statement:

done%=false

'global loop
while not done

Include"Hot stuff1.bb"

Include"Hot stuff2.bb"

Include"Hot stuff3.bb"

Load everything, setup or whatever before main loop

'main loop magic
While not KeyDown(1)

do incredible stuff here
UpdateWorld
Renderworld
Flip
Wend

ClearWorld

'if user exits then done=true

wend 'goto global loop


D4NM4N(Posted 2008) [#3]
After the ClearWorld statement all of your resources are lost

I think clearworld only applies to the 3d stuff.? I think images and sounds are not effected (or so i seem to remember :/ )

Its always good programming practise to dispose of every resource when it is no longer required as a matter of protocol rather than relying on clearworld/endgraphics etc. It makes it more effecient when loading a new level etc. I usually load all my "source meshes" and hide them straight away. Then when i need them (like monsters etc) just use copyentity to create disposable instances of the originals in small datastructures.

Eg. What i usually do is create a type datastructure for all disposables as a rule of thumb, then i can then just call: (for example)

function cleanuplevel()
    ;clear level's SFX
    freesound g_leveltune : g_leveltune=0

    ;Dispose of main level mesh
    Freeentity g_levelmesh : g_levelmesh=0

    ;Clean copies of loaded "source" meshes
    For s.t_scenerycompoent=each t_scenerycomponent 
         freeentity s\handle : delete s 
    next
    For m.t_monsters=each t_monsters 
         freeentity m\handle: delete m 
    next

    etc...

end function

then for things that can stay loaded like source meshes, they dont get wiped out and have to be reloaded next time.


Naughty Alien(Posted 2008) [#4]
..well..thats not bad...as for me, Im wondering whats going to happen with my Includes(as well as Type definitions, DIM arrays, etc..)..they are not containing any image, entity or brush..just functions for dealing with loaded things...so, after ClearWorld, are they gone too??


D4NM4N(Posted 2008) [#5]
no, i think clearworld only clears the 3D "space" if i remember correctly. All variable values & non '3d' stuff should remain intact, which is not always a good thing.

The problem with clearworld is if you have a variable called "player" storing a mesh handle. After clearworld, the handle will still be set to say player=1238783 but the handle will be invalid as the media has been dumped.

Includes are simply (as far as the programmer is concerned) extensions of the main code at the exact point and order in which they are placed. Basically the compiler treats it as if the included code was all in one file.


Naughty Alien(Posted 2008) [#6]
but how to delete TYPEs, while holding their definition structure intact??


D4NM4N(Posted 2008) [#7]
you dont need to.

For eg.
sourcemonster=loadmesh ("mysourcemonster")

This would be loaded and never disposed of.
Then, when you want to add say a monster or a load of trees or something:

createmonster(sourcemonster,100,0,100)
function createmonster(sm,x,y,z)  ; 
   m.monster=new monster
   m\hand=copyentity(sm)
   m\startpositionX=x
   m\startpositionY=y
   m\startpositionZ=z
   etc...
end function
To delete a monster data item ie, has been killed:
freeentity m\hand:delete m
This will not only delete the 3d resource but the entire "record" (m) in the datastructure (assuming m.monster is locally valid)

To delete all monsters when changing level:
for m.monster=each monster 
   freeentity m\hand
   delete m
next

Obviously for large unique items like the main level mesh and music etc you use a global and can clean this as normal with a simple:
freeentity (G_mainlevelmesh):G_mainlevelmesh=0
Its always a good idea to reset the vars value to 0 as you can still use "if NOT myhandle then.... " to detect any non loaded resources (to avoid potential MAVs ).


John Blackledge(Posted 2008) [#8]
You should be aware that 'includes' are handled at compile time.
It doesn't make the slightest difference if you put them in the main loop.
Additionaly, in Blitz you can never have 'conditional includes' (at least without a pre-processor).

Think of your includes as blocks of code that are attached to the exe at compile time.


Dreamora(Posted 2008) [#9]
Not really.

Include is a copy - paste command. It takes the whole content of the "to be included file" and replaces the include line with it.
Thats it.


D4NM4N(Posted 2008) [#10]
Exactly ( i liked the way i put it better... although i did like the copy and paste analogy :P )


John Blackledge(Posted 2008) [#11]
Dur... yes, and then it's compiled.

But it's not conditional within the code, as when using an 'if' or 'case'.


D4NM4N(Posted 2008) [#12]
Ahh, but its not attached to the exe... o_O

Sorry john, had a beer its saturday night and im depressed because im not down the pub and have nothing better to do- :P

I know what you mean tho ;)


Dreamora(Posted 2008) [#13]
You can actually make it "conditional" due to a little powerfull feature in Blitz3D / BlitzPlus:

If with constants works like #ifdef #ifndef #endif in C. This emans the part of an IF, that is never reached due to the constant is not even included in the EXE.

That allows you to effectively switch functionality between debug and release build as well as between demo and full version builds.


John Blackledge(Posted 2008) [#14]
Dreamora, we were talking about includes, not variables or constants.

Also, it wouldn't matter whether you made a variable or constant conditional. An include, if specified in the code, is _always_ compiled into the exe. It doesn't matter if you wrap it in a condition, it still gets compiled.


PowerPC603(Posted 2008) [#15]
I just tested this conditional include, but that doesn't work:

main file:
Const test = True

If test = True Then
	Include "includefile.bb"
EndIf


file "includefile.bb":
Function Blabla()
	Print "Blabla"
End Function


If I want to compile the mainfile into an EXE, I got this error on the function-statement of the file "includefile.bb": 'Function' can only appear in main program.
If I remove the If-EndIF in the mainfile, it works perfectly.


Dreamora(Posted 2008) [#16]
Follow programmer logic number one: INCLUDES ARE ALWAYS AT THE TOP OR NOWHERE :-)
That saves you headache and other people a lot of time when trying to read the code.

What am I happy that BM removed that feature that can only be missused to make code worthless and unmaintainable, not used for good codedesign.


Wings(Posted 2008) [#17]
I agree with Dreamora
Specialy with blitz3d


D4NM4N(Posted 2008) [#18]
Ditto, conditional includes would be horrendous in general, with the very small possibility of some very exceptional exceptions where it *might* be 'useful' (if it were possible). but otherwise nono.
Basically if you *need* to do it then you have not structured your code properly.