Avoid using a particular type's name?

BlitzMax Forums/BlitzMax Programming/Avoid using a particular type's name?

sswift(Posted 2011) [#1]
I seem to recall there was a way to tell BlitzMax that a particular section of code was using a particular type, so any variable references without a type name in front would first be checked against the type's fields. Anyone know how this can be accomplished?

Context:

Some code I'm working on has a bunch of globals which it then sticks in a type before saving the data. I would like to know if it is possible to stick those globals in a type so the type need not be created and filled before saving, without having to change every single global variable reference in the code to point to the type.


Czar Flavius(Posted 2011) [#2]
If there is such a way, I don't know it and would be interested to find out.

I'm not entirely sure what you are trying to do, but why not put your globals into some kind of save data type? And then you pass a reference to the save data type you are interested in writing to. I'm not sure what you mean by "stick those globals in a type so the type need not be created and filled before saving".


sswift(Posted 2011) [#3]
I'm working on someone else's code. The codebase is very large. It uses lots of globals. There is a type though which duplicates all those globals and was added later. It's used only when saving. An instance of the type is created, and then all the globals are copied into the type before it's all saved with one of the bank saving functions.

The ideal thing to do would be to get rid of all the globals and use the type. But that would require renaming every single global variable in the code, which if I'm not careful could introduce bugs. A better option would be to just make all those globa names reference the fields in the type instead, without having to specify the object name before them. But I guess that's not possible.

Anyway it's not that important; I just wanted to clean up the save game code a bit. The way it is now you have to update it every time you add a global variable. By using the type to store all the variables you would not.


Kryzon(Posted 2011) [#4]
Did you mean Reflection?

EDIT: After actually reading what you asked for, you could change the Type's global's declarations so they act as pointers; the address they point to are the globals outside of the Type.
Global checkPoint:Int

Type TGameState
	Global checkPoint:Int Ptr

	Function Assign()
		TGameState.checkPoint = Varptr checkPoint
		'[...]
	EndFunction

	Function Save()
		'[...]

		WriteInt saveStream, TGameState.checkPoint[0]
	EndFunction
EndType
I don't know if the above is correct but in any case, you can get more information at -> BlitzMax IDE -> Language Reference -> Advanced Topics -> Pointers.

And this thread as well.

Last edited 2011


Czar Flavius(Posted 2011) [#5]
Pointers? Reflection? What for?

You can find and replace the name of the global, to add the name of a type you are now moving them into, quickly.


sswift(Posted 2011) [#6]
Not when there are thousands of lines of code which you are unfamiliar with, in many seperate source files, and where a particular variable's name might be contained within another variable and just doing an auto replace would cause bugs since the code isn't superstrict.


Paul "Taiphoz"(Posted 2011) [#7]
I had a similar issue . What I did was write some code to parse my 24 source files looking for a variable and the. Replace it with another exactly as you describe I moved all my globals into a single type.

With my code I made it check for anfew things like whitespace before the variable or a : or - or ( etc in a nut shell I set some rules that would exclude any locally declared variables of the same name.

You should do that, code took me a few hors but when it was done a converted my source globals to type in seconds I only had one bug and that was because I had a function with the name in it and I forgot to full functions out easy fix tho.