var/const/some_string = "something"
#define some_string "something"
What's the difference between the two?
Code:
var/const/some_string = "something" What's the difference between the two? |
@Stephen; What could be some good examples to use #define with? I've never really understood it either, but once before I saw a friend make a demo out of only using # (the green text). Ever since then, I've always wondered about it.
|
Mmmm. One obvious example would be for extra debugging code, or say ... an new algorithm would want to introduce. That debug checkbox you have in DreamMaker just puts a definition in the DME file.
#define TRACE_DEBUG Alternatively, F_Damage uses it within the Cache.dm to basically avoid proc call overhead within that file. It's not recommended to do in general, but within that file it was okay because that's a contained piece of code, that needs to operate quickly. #define F_DAMAGE_CACHE_TICKER 10 |
Incidentally, that has highlighted three things:
Poor use of macros. F_DAMAGE_CACHE_HIT and F_DAMAGE_CACHE_MISS are used once, there's really no need for them to be macros. Un-necessary delete of the icon. F_DAMAGE_CACHE_TICKER cannot be overridden by library users. |
For a more simple-minded alternative, if you for example wanted all of your procedures to follow one naming convention but are annoyed by the built-in procedure's naming convention, you can do this:
#define move(newloc, dir, step_x, step_y) Move(newloc, dir, step_x, step_y)
You can also do it for formulas. I find it very convenient (Although maybe not necessary) for simplifying formulas. // for example |
In response to Albro1
|
|
Thanks, that helped me understand it a little better. Stephen is too smart for me, lol. I got lost in his example.
|
The const variable represents a variable you'd have in the world at run-time. It obeys the normal rules of variables, so for example you cannot do:
Generally it's a good way to provide values that are scoped, have path names etc, in libraries and games.
The macro definition allows for a literal replacement within the source code, just before it's compiled. So there's no actual variable in the world at runtime. It applies from that point in the code onwards (or until an equivalent #undef) and can replace /any/ part of the code that matches.
Meaning, this could cause issue: