Include syntax..?

Blitz3D Forums/Blitz3D Programming/Include syntax..?

mrbombermillzy(Posted 2007) [#1]
Hi all.Actually still using BlitzBasic (2D!) but Im sure the actual non-3D syntax is the same as B3D, so..
Cant seem to find any documentation that would tell me either way, but can I do the following:

Global Character ; INT containing 0-10
...
...
...
Include "Character number:":Character:".bb";

Ive tried all the permutations I can think of too, like:

Char_no$=Str Character

before the include statement line, also changing the colons for "+", and changing the order.
e.g. Include Character:"Character Number.bb"

Doesnt look like I can do any of this. Anyone know for definite?
Thanks for any help.


Curtastic(Posted 2007) [#2]
no you can't use a variable in the filename.


Matty(Posted 2007) [#3]
Something was asked like this the other day in this forum - basically you cannot use variables in the 'include' statements.


bytecode77(Posted 2007) [#4]
the include command is a pre-proccessor command. so before blitz compiles, your include files will be replaced by the include line!
understood?


mrbombermillzy(Posted 2007) [#5]
Thanks for the verification guys. Guess I will just have to make that bit of code a bit less streamlined!
But while we are on the subject, I may aswell ask another question.(Will save having to post a new topic anyway!)
I have alot of include files that I now have to labouriously 'include' manually lol
They have lots of data that needs to be read in. The problem is, I need to change/write the data into the file.
Do I have to do a string search/compare to find the correct location of the data, then 'print to file' over it or something?

Thanks again guys for any pointers.


Stevie G(Posted 2007) [#6]
I have alot of include files that I now have to labouriously 'include' manually lol
They have lots of data that needs to be read in. The problem is, I need to change/write the data into the file.
Do I have to do a string search/compare to find the correct location of the data, then 'print to file' over it or something?


By this question I don't think you fully understand the include command.

As Devil suggests, all that it does is effectively copy and paste all the code from the include .bb file where you call the include command. When you create a final .exe all the included files will be compiled together into one single file.

The files you are writing to should not be .bb files for this reason.

What is it you're doing and someone can come up with a better solution for you?

Stevie


jfk EO-11110(Posted 2007) [#7]
Include was never ment to load data. You may however write your own Include function that does the job, something like:

function MyInclude#(filename$)
if filetype(filename$)<>1 then return 0 ; error, file not found
re=readfile(filename$)
value#=readfloat(re)
closefile re
return value
end function

you may then simply use it like this:

character#=MyInclude#("character.xyz")

(using floats in this example). you need to write the value to the file like this:

wr=writefile(filename$)
writefloat wr, somevalue#
closefile wr

you may however use WriteLine or ReadLine instead, blitz will automaticly convert it to the required variable type.

If you want to access multiple values in one file then you can use the SEEK command (if the exact postion is known), or as you said, string-compare (maybe use descriptors, eg:
WIDTH=1.34
HEIGHT=4.77
)

Personally I prevere to write data line by line (writeline), using description strings for each parameter, then load them into an String Array (DIM) and comfortly parse the array using FOR loops.


mrbombermillzy(Posted 2007) [#8]
Sorry for any confusion.I am fully aware of how the 'Include' command just pastes the included file into the code.I was just trying to achieve a 'selective' inclusion.
For example, I may have 8 different character data files which may not all have to be loaded.The player should be able to choose which one to play as, and therefore this characters data should be the only one required.
The second post was only marginally related, and was more of a 'data manipulation' query.
I already have a very concise 'character data template' which includes data statements containing values for 'Energy' 'Speed' etc. If I was starting off, I may have used the method jfk has shown, but Im too far down the road with this imported data template style.
It looks like with the lack of a memory mapped data 'heap' which I can manually 'Peek/Poke', I will have to use 'Seek', 'FilePos' and maybe a bit of string compare.
Maybe its not the most elegant method, but its probably the most suited to the problem at hand without changing the templates.


Zethrax(Posted 2007) [#9]
One thing you can do is use constant expressions with some program flow commands to determine what does and doesn't end up in the compiled file. Blitz will optimize the code by collapsing it down to its simplest form, where possible. I've tested this in the past using checksums on the compiled exe.

The program flow commands need to follow the standard syntax rules for that command, though, so you can selectively include a function call or the contents of a function, but not an entire function declaration, for example.

Example input code:-
Const C_MODE = 1

If C_MODE = 1

	Include "INCLUDE1.bb"

ElseIf C_MODE = 2

	Include "INCLUDE2.bb"

EndIf

WaitKey

End


The actual code sent to the compiler will be:-
Const C_MODE = 1

Include "INCLUDE1.bb"

WaitKey

End



mrbombermillzy(Posted 2007) [#10]
Thanks Bill.Thats pretty much what I will end up doing I think.


Stevie G(Posted 2007) [#11]
Sure, the conditional include would be fine for testing but the final exe will be for a single character only. I don't get the logic here?

Maybe I've misunderstood you but do you really intend on having 8 different .exe's , one for each character?

If that's the case, your method of character selection is flawed and you should seriously think about implementing a system which loads or reads the data for the character selected in a similar way to what jfk suggested.

Do you have an example of what data you're holding in an include file for one of the characters?

Stevie


kerrill(Posted 2007) [#12]
I've tried to do some conditional Includes and have found that you can't have any Constant declarations in your Include file if you call it from within an if...then...else loop (or any loop?) Just tried calling an Include file that contains Data statements, but it didn't work.

Message: " 'Data' can only appear in main program."

A while back I finally figured out that it's important to think of just typing the Include file's code right in place of the Include "yourfilename.bb" statement. If you can't put your entire file code there (by syntax rules), then the Include call can't go there either. (It's easy to forget though.) Are you using Data statements or something else that I don't know about?


mrbombermillzy(Posted 2007) [#13]
@Stevie G: This all boils down to the 'Include' statement being pre-processed.It is, so thats pretty much the end of the story for what I was trying to do.Its as fundamental as that.

To explain, I was using the Include statement much as you would, for example the 'Print' or 'Text' statements.

I could therefore do the following:
Get_Character=GetKey(); gets the character number

If Get_Character>7 then Print "Number too high":End

Print "Character selected is"+Get_Character;

But I cannot 'Include' Get_Character'+".bb" (assuming it was an integer or even a string between 0-7, sorry for the simplified code).I would have to include them all as the processing would have to include every option before runtime anyway.

The game would ideally have just included the data for that character, as I dont need the data for any other redundant characters.I cant use the include keyword like this though. Hope that explains it all.

What Im going to end up doing, is writing all character data to a file in the form of data statements. Tricky to write, but dead easy to read from.

To write, I will have to do a string compare function, much like the 'Restore' keyword which points to the start of a data list to be read.
The data would then be written as below example:

Data "fred",100,2, etc

From a table which stores characters name,characters energy,weapon number used,etc, to corellate with the above data statement.

The data would then be read into a multi dimensioned array, which stores all the character data for each character.

At the end of the day, with the includes, I can just paste it all in anyway. I just wanted to keep the size of my final code down, ideally.
As for the read/write issue, it wont be quite as simple as there are no real commands for writing data into data tables, so I will go with the above mentioned ideas.


Subirenihil(Posted 2007) [#14]
It sounds to me that you need some form of loading function, something that loads only the data needed for that particular character. The Include statement is not what you need. Like everyone else has been trying to tell you, Include is a copy/paste operation from another file into your sourcecode. Basically, what you're trying to do is compile your program so that when it runs it gets compiled with only the info for the one character.

It's like trying to build a one-piece screwdriver that can unscrew slotted, hex, phillips, and any other screw you have - and have only one bit. It doesn't work. You build it to work with one screw and it won't work with other kinds. What you need is replaceable bits for your screwdriver depending on the screw you're using.

In other words, you need data files that you load depending on what character is needed:



Sample Files:
;"Sample File 1.dat"
.Character1
Data "Me, Myself, and I", 456, 21, 1

.Character2
Data "The Little Nobody",12,2,14

.Character3
Data "Sleepy Zzzzzz",321,458,3
;"Sample File 2.dat"
.Character1
Data "Once again", 456, 21, 1

.Character2
Data "Twice now",12,2,14

.Character3
Data "The Third Guy",321,458,3
;"Sample File 3.dat"
.Character1
Data "Me First", 456, 21, 1

.Character2
Data "The First Loser",12,2,14

.Character3
Data "The test",321,458,3


Sample loading function to load this data:

Global Name$, health, mana, weapon

Print "Press a key to choose a file:"
Print "    1)   File 1"
Print "    2)   File 2"
Print "    3)   File 3"
Repeat
	If Keyhit(2)
		user=1
		Exit
	EndIf
	If Keyhit(3)
		user=1
		Exit
	EndIf
	If Keyhit(4)
		user=1
		Exit
	EndIf
Forever
Print
Print

Print "Press a key to choose a Character:"
Print "    1)   Character 1"
Print "    2)   Character 2"
Print "    3)   Character 3"
Repeat
	If Keyhit(2)
		LoadData(user,1)
		Exit
	EndIf
	If Keyhit(3)
		LoadData(user,2)
		Exit
	EndIf
	If Keyhit(4)
		LoadData(user,3)
		Exit
	EndIf
Forever
Print
Print

Print Chr$(34)+name$+Chr$(34)+":"
Print "    Health: "+Str$(health)
Print "    Mana: "+Str$(mana)
Print "    Weapon: "+Str$(weapon)
WaitKey
End

Function LoadData(user,player)
	file=OpenFile("Sample File "+user+".dat")
	Repeat
		;This loop removes blank line from the parsing process
		Repeat
			ln$=Trim$(ReadLine$(file))
		Until Eof(file) or ln$<>""
		Select ln$
			Case ".Character":
				ln$=Trim$(Right$(ln$+String$(" ",Len(".Character")),Len(ln$)))
				person=Int(ln$)
			Case "Data ":
					ln$=Trim$(Right$(ln$+String$(" ",Len("Data")),Len(ln$)))
				If person=player
					a1=Instr(ln$,Chr$(34))
					a2=Instr(ln$,Chr$(34),a1+1)
					a3=Instr(ln$,",",a2+1)
					a4=Instr(ln$,",",a3+1)
					a5=Instr(ln$,",",a4+1)
					name$=Mid$(ln$,a1+1,a2-a1-1)
					health=Int(Mid$(ln$,a3+1,a4-a3-1))
					mana=Int(Mid$(ln$,a4+1,a5-a4-1))
					weapon=Int(Right$(ln$,Len(ln$)-a5))
				EndIf
		End Select
	Until Eof(file)
	CloseFile file
End Function



mrbombermillzy(Posted 2007) [#15]
Ok..Thanyou very much Subirenihil, Bill and JFK for your help, and to everyone else for the comments.
Just to let you know, Ive looked at all the commands available, and which methods to use.
The route Ive decided to take is to open the file and then write to it.
The reading in is the easy part, and its not really an issue to include all the characters data really, so it would work something like this:

File=Openfile ("Character0_data.bb"); opens data file

WriteLine (File,"Data "+Spawn_point(Character,0)+","+Spawn_point(Character,1)+","...etc,etc

where Spawn_point is an array for the placement of the character, character is the character number, and the 0,1,etc is the actual screen.
I can write to all my data statements like this, and they can be read in this format very easily.