Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial Using variables as functions. (Function Pointer)
#1
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]]();
    }
}
So now when I spawn, it runs the function that is assigned to 'byeCruelWorld' which causes me to commit suicide. However there is a downside since I can only use 'byeCruelWorld' in the same function for obvious reasons.

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]]();
[Image: 30xhrep.png]

A casual conversation between barata and I about Nukem.
Reply

#2
Never seen this...GJBig Grin
[Image: wyipjqo9qon7rc2v1lo.jpg]
Reply

#3
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?
YouTube 1:Azumikkel- Modding
YouTube 2:DerpShark- Gaming Entertainment
Website:Jensby.me
Contact: im[at]jensby.me
Reply

#4
(01-09-2011, 10:27)AZUMIKKEL Wrote: 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?

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.
[Image: 30xhrep.png]

A casual conversation between barata and I about Nukem.
Reply

#5
They're called function pointers.
Reply

#6
(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 Dodgy

[Image: 30xhrep.png]

A casual conversation between barata and I about Nukem.
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Help Help Function Fire dargula 1 1,972 10-31-2013, 23:03
Last Post: Nekochan
  [Release] Matrix Functions Yamato 1 2,518 07-29-2013, 20:14
Last Post: Rendflex
  [Release] nCr and Factorial Functions Yamato 2 2,508 06-18-2013, 17:24
Last Post: Nekochan
  [Tutorial] How to use variables as long as server runs Justin 4 3,930 05-04-2013, 20:54
Last Post: Nekochan
  [Release] Random Functions 1.1 Yamato 22 12,296 04-20-2013, 10:13
Last Post: Dominator56
Information [Tutorial] [CHAPTER 2]C++ Crash Course - Variables Ich1994 0 1,803 02-04-2013, 18:47
Last Post: Ich1994
  Black Ops II IWI block decompression/decryption function kokole 7 4,957 12-07-2012, 22:53
Last Post: master131
  Help Maximum Parent Script Variables DidUknowiPwn 11 7,711 11-21-2012, 19:30
Last Post: DidUknowiPwn
  Functions surtek 14 10,503 09-19-2012, 07:57
Last Post: Pozzuh
Information [Solved] Info about functions, events and etc. Nerus 2 2,313 09-09-2012, 20:57
Last Post: surtek

Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2024 Melroy van den Berg.