I think this is one of those learning moments that Im about to understand something huge about how macros and functions work.
Essentially Im creating a huge map dictionary with over 5000 structures inside. Its fast and Im able to get infomation quickly. To set the keys and filter I use macros. It works very well
But If I copy and paste My macro into a function and put it in the same place, It doesnt work at all. Infact only about 1000 of the 5000 actually get entered into the map.
Im absolutly baffled on why this would be the case?
Anyone got any links or a fun way to understand this enigma
Or maybe a reason I might use a macro or function rather than just a gut feeling I should be using one or the other.
Macros are collection of events. Event use the engine task queue system, that’s how we can get things like delay nodes and/or latent async actions.
A function however is a straight block of code. 1, 2 ,3 etc on the cpu. Guaranteed to run. This allows us to declare temp variables on the stack the get released once the function is complete.
Is there a metaphor to describe them and when I might use them?
Lego for example?
a Function is basically a play by play on how to build something? like the little lego books that lead start to end. Unreal will read the book I lay out for it and do it page for page.
A macro allows delays and timelines etc. almost like just sections of the instruction booklet I can slap anywhere. But when would this be useful?
rarely i’ve found a situation where i actually need a macro. when my teammates used them, they always ran into issues time later.
is not because macros are bad, but because sometimes one might confuse their use.
i don’t think i’ll manage to succinctly explain what i know about them but:
in cpp macros are usually used to generate code in a generic way. just snippets. on a higher level than the actual language. and so they are not constrained by things like “type”.
in bp they work a bit similar in that you can “reuse” chunks of code. it’s more like a copy/paste than a function. the code is executed locally. if you can use a function instead, i’d rather do that.
if it has to be a “static global function” you can use a bp function library.
irrelevant thought:
i think the for loop uses it because it is type agnostic, and have multiple execution pins. but i don’t understand why epic decided to implement it in than fashion instead of a k2node, specially since a for loop is such a basic part of a language and it’s super related to performance.
a caveat is that a for loop will evaluate the input on every loop, so if you use a pure function (which is very likely) that function gets evaluated on each loop, so you might want to cache the value first (afaik).
as for a metaphor?
macros (at least in cpp) behave like a Find/Replace.
it will find all the occurences of the macro, and then paste the code in between, before the compiler compiles it (afaik).
unfortunately ue’s docs is lacking on explaining how they work and why.