.qc (dot qc) - the group for quake c coders of all denominations. If you make quake one mods and write code in quake c, join our group! I'll try to answer any quake c questions - please post your inquiry in the forums...

Forum Thread
  Posts  
qc++ #define precompiler macro function (Groups : qc : Forum : vault() : qc++ #define precompiler macro function) Locked
Thread Options
numbersix
numbersix quake-c coder++
Dec 7 2014 Anchor

What is a #define macro function?

From the fteqcc wiki: Fteqw.com

Syntax: #define {defname}[({macroparams})] [{defvalue}]
Defines a precompiler define or macro.

* {defname} contains the define name
* {macroparams} contains a list of parameters, seperated by commas, to use with a macro (optional)
* {defvalue} contains the define value (optional)

Working example:

Moddb.com

What this does is define a macro that substitutes the macroparams into the defvalue code fragment and compiles as code where it is used.
It appears like a function call, but compiles code.

Why may you want to take this route vs. an actual function call?

* limit global variable use - every function defined and its parameters are globals, the #define params are not
* limit stack usage - in heavy loops and repeating code, you may want to minimize stack use
* parm substitution can be more flexible than function variables - see "excode;" in the working example, which expands into _any_ code you need!
* stringify and concatenate parms (where a = that) #a = "that" and this_##a = this_that

Limits:

* {macroparams} - there is a maximum of eight and they can NOT contain numerals [0-9]
* {defvalue} - has a length limit of 256 characters, including spaces (you can use multiple levels of define substitution here...)
* when {defvalue} expands, you can NOT always use // style comments or /* comment */ certain code fragments
* macros are _not_ functions and may not behave the same way as the same code in a function call

From the working example:

framer($walk2, $walkeoc, shal_walk1, shal_walk1, , 0.1,

//framer($walk2, $walkeoc, shal_walk1, frd1, 0.1,

frame_arb($walk2,if (random() < 0.2) sound(self, CHAN_VOICE, "shalrath/idle.wav", 1, ATTN_IDLE));

);

Will not compile:

compiling shalrath.qc
at global scope
shalrath.qc:51: error: Too many parameters in macro call
shalrath.qc:61: error: "*" is not a type
in function frd1 (line 122)
shalrath.qc:123: error: Unknown value "shal_walk1".
in function monster_shalrath (line 257)
shalrath.qc:283: error: Unknown value "shal_walk1".
shalrath.qc:283: error: type mismatch for = (pointer and __variant)

************ ERROR ************
Errors have occured

I tried

/* framer($walk2, $walkeoc, shal_walk1, frd1, 0.1, */

and that also fails to compile!

If you want the comment - move it above (or outside) the macro usage.

Edited by: numbersix

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.