def quest_function(player)
player.quests
end
def quest_function_without_player(quests)
quests
end
quest_function(player)
quest_function_without_player(player.quests)
$quests = Array.new()
def quest_fun
$quests
end
def quest_fun_2
$quests
end
$quests = arbitrary_query_function
quest_fun()
quest_fun_2()
#include <iostream>
// define a structure
struct X
{
int m;
};
void function(X arg) // a function with a structure as a parameter
{
std::cout << arg.m << '\n'; // prints 42
}
int main()
{
X obj = {42}; // create an instance of type 'X'
function(obj); // pass 'X' to 'function()'
return 0;
}
void function(X& arg) // pass by reference
{
// etc
}
void function(const X& arg) // pass by const reference
{
// etc
}
void function (X *arg) // by pointer parameter (or const X *arg if applicable)
{
// etc
}
X obj = {42};
function(&obj);
In order to work this out and display things to players, you (potentially) need to be looking up multiple quests at a time. Yes, you could finagle it such that you build up state one quest at a time, but now your non-DB logic has to be more complicated in order to deal with (easily avoidable) idiosyncrasies of the DB API.
You identified another problem yourself: you find it brittle, but you wrote it yourself so it's ok (for now). But what if you have to pass this code to somebody else? For that matter, what if you have to revisit it in a year?
You have avoided passing a parameter to quest functions by storing it in a global variable. But you have apparently felt the brittle nature of this solution already. Is having to think about it really worth not passing around that single extra parameter?
Very frankly I could not condone this as a good usage of a global, because it limits quest look-ups to one-at-a-time, fragile operations. It makes API users think about things that are irrelevant (am I accessing stuff in the right or wrong order here?). It makes for relearning every time you revisit the implementation. All of this for nothing more than not having to pass a parameter to 6 functions.
If we were talking about an order of magnitude speed-up in a performance bottleneck, maybe we would allow ourselves some complexity because the gains are truly significant. Complexity is not necessarily bad, but complexity without sufficient payout is not good. In your example, I'm seeing plenty of complexity (and you apparently experienced it yourself) but I'm not seeing gain beyond not having to pass a parameter. Were there other things you found to be positive as a result of having a global?