![]() |
|
[Tutorial] Using variables as functions. (Function Pointer) - Printable Version +- ItsMods (https://www.itsmods.com) +-- Forum: Archive (https://www.itsmods.com/Forum-Archive.html) +--- Forum: Archive (https://www.itsmods.com/Forum-Archive--64.html) +--- Thread: [Tutorial] Using variables as functions. (Function Pointer) (/Thread-Tutorial-Using-variables-as-functions-Function-Pointer.html) |
Using variables as functions. (Function Pointer) - master131 - 01-09-2011 Well have you ever seen code like this and thought wtf? Code: [[level.callbackStartGameType]]();That is was I call a function pointer. It runs a function that is assigned to a variable. If you search through the game's files you will find this: Code: level.callbackStartGameType = maps\mp\gametypes\_globallogic::Callback_StartGameType;What that means is the code in the first box runs the function that is assigned in the second box. Now that we have found out this. We can use variables as functions. Say, inside my _rank.gsc file I had these two sub-procedures (tehehe, VB): Code: onPlayerSpawned()
{
self endon("disconnect");
for(;;)
{
self waittill("spawned_player");
}
}
doSuicide()
{
self suicide();
}How do I run doSuicide() as a function? Well first you need to declare your function. Lets name it byeCruelWorld. To link it to a function that's in the same file and then run it, you do this: Code: byeCruelWorld = ::doSuicide;
self [[byeCruelWorld]]();Now my onPlayerSpawned looks like this: Code: onPlayerSpawned()
{
self endon("disconnect");
for(;;)
{
self waittill("spawned_player");
byeCruelWorld = ::doSuicide;
self [[byeCruelWorld]]();
}
}What if I want to use this function in anywhere BUT the function is in the same file I'm delcaring it in? You would do this and delcare it as a global variable. Code: self.byeCruelWorld = ::doSuicide;
// In any file:
self [[self.byeCruelWorld]]();What if I want to use this function in anywhere AND the function is in another file? You would link it in a simillar manner of running a function or thread from another file. Code: self.byeCruelWorld = maps\mp\gametypes\_rank::doSuicide;
// In any file:
self [[self.byeCruelWorld]]();What if I want to pass arguments through? You pass your arguments through the pair of brackets before the semi-colon. Code: level.endGameNow = maps\mp\gametypes\_globallogic::endGame;
level [[level.endGameNow]]("allies", "Allies rule!");What if I want to thread the function pointer? Simple, just add 'thread' before the variable. Code: self thread [[self.byeCruelWorld]]();RE: Using variables as functions. - alistair3149 - 01-09-2011 Never seen this...GJ
RE: Using variables as functions. - AZUMIKKEL - 01-09-2011 self.byeCruelWorld = maps\mp\gametypes\_rank::doSuicide; Just a question. When doing the above function for example, wouldn't it run the thread through once when you make the variable? Because usually when you do threads like i = self thread e(); 'i' will become the value that e() returns (return value; ). Or does it have to do with ::doSuicide not having the () at the end? RE: Using variables as functions. - master131 - 01-09-2011 (01-09-2011, 10:27)AZUMIKKEL Wrote: self.byeCruelWorld = maps\mp\gametypes\_rank::doSuicide; no, it does not run the function when you declare it since you don't put the () at the end otherwise it would get the returned value or run the function. ----------- Not really sure why this is useful but I decided to share it with you guys. Maybe it could be used for some obsfucation or 'encryption' of some sort to make your mod harder to understand or just simply to be able to use the same function in many files by declariing it once. RE: Using variables as functions. - SparkyMcSparks - 01-10-2011 They're called function pointers. RE: Using variables as functions. - master131 - 01-10-2011 (01-10-2011, 19:54)Myst88 Wrote: They're called function pointers. That was the word I was looking for! I can't believe I was just learning C++ and I forgot what it was called ![]() |