Create a function pointer in Game Maker Studio - function-pointers

I would like to dynamically change the script that is executed when a index in an array is called.
The following is valid syntax:
actions[0] = script_do_something
...
actions[n] = script_do_something_else
How do I execute the scripts stored in the array? If I cant, then how else could I replicate a function pointer in GMS?
Unfortunately this doesn't work:
actions[0]()
I would like to avoid if statements as I feel they will get too long and messy.

you should use script_execute function.
script_execute(actions[0]);
also you may pass arguments:
script_execute(actions[0], arg1, arg2);

Related

Lua override class function in included file

I'm sorry I'm new to LUA scripts and I have to work on code written by others.
Please don't focus on code, my problem is only about included files and priority evaluating which function has to be called, in case of overriding.
Let's say I have a file Terrain.lua containing a class Terrain, which has a function Terrain:generate() and Terrain:generate() calls Terrain:getLatitude().
Terrain was included in a script MyScript.lua, which overrided Terrain:getLatitude() as follows:
include("Terrain");
function Terrain:getLatitude()
new code;
end
function myFunction()
local myTerrain = Terrain.create();
myTerrain.generate();
end
This has the effect of overriding getLatitude(): when myTerrain.generate() is called, generate() is the code from the included "Terrain", but getLatitude() is the local function with the new code, even if called by a function from the included class.
Now let's say I want to put some of the code in an external file Custom.lua. Custom (and not MyScript) has to override getLatitude().
This is the situation:
Terrain.lua contains Terrain class and these functions
Terrain.create()
Terrain.generate()
Terrain.getLatitude()
MyScript.lua is the script being executed, and include Custom:
include("Custom");
function myFunction()
return customFunction()
end
Custom.lua contains:
include("Terrain");
function Terrain:getLatitude()
new code;
end
function customFunction()
local myTerrain = Terrain.create();
myTerrain.generate();
end
Now, if I call customFunction() from MyScript, getLatitude() from Terrain is used, instead of getLatitude() from Custom. I assume ovveride is possible only inside the currenti file being executed? How can I achieve overriding in an included file?
I hope this example is enough to understand my problem, without posting a lot of code. Thank you.
Firstly, some corrections: there is no local function's in your question; include is not part of any lua standard, what that function actually does may be quite important.
Finally, Lua does not have actual class system, what you use in the question is merely a syntactic sugar (misleading and confusing as I find it) over table assignments. Lua is an interpreted language, so what may seem to you as a class definition is not a static structure known from the very beginning of the program execution but a code that gets executed from the top of the file to the bottom.
Thus, if we assume that include is similar to the require, then the your question code will be equivalent to the following:
do--terrain.lua
Terrain = {
create=function()
local created_object
--some code to assign value to created_object
return created_object
end
}
Terrain.generate = function(self) end
Terrain.getLatitude = function(this_is_a_self_too)
--some code that uses `also_self` as a reference to the object when called as object:generate()
end
--do end block is essentially an equivalent of file, its local variables are not seen outside
--global variables it has assigned (like `terrain`) will stay accessible AFTER its end
--changes it done to global variables will also remain
end
do--Custom.lua
Terrain.getLatitude = function(this)--this is the assignment to a field in a table stored in the global variable Terrain
--this function will replace the one assigned to the `getLatitude` field
end
customFunction = function()
local myTerrain = Terrain.create();
myTerrain.generate();--this one probably needs `:` instead of `.`
--depends on actual code inside terrain.lua
end
end
do--MyScript.lua
myFunction= function()
return customFunction() --this line calls the global variable customFunction
end
end
Thus if your actual setup is similar to the one in question, then the "override" will take effect after the Custom.lua is executed and for all the subsequent calls to the Terrain.getLatitude regardless of whether or not they've called the file. (And any later file can override it again, and all calls after that will be using the new one)
It is probably more complicated to do a limited override in this setup. That again depends on the actual details of how your team has defined the Terrain class and the class system itself.

How do I create a function prototype in powershell?

I can't find an example. How do I make a prototype for my powershell functions?
function exampleFunc();
//other stuff that calls the example function
Function exampleFunc(){//stuff}
PowerShell doesn't support prototype functions, forward declarations or whichever term you want to use for this. In PowerShell, when you use the function keyword you're defining a function. If you call it twice with the same function name, you change the function's definition.
This question about the same issue with bash lists the common methods to get around the issue. You can do the same things in PowerShell.
Another option is to use the Begin {} Process {} End {} advanced function construct, and put all your function declarations in the Begin {} portion.
You could call for snippets from Powershell ISE (Integrated Scription Environment) by right click or access via quick buttons Ctrl+J.
And here is what #bacon-bits code would look like
function main()
{
cls
Begin
Process
End
}
function Begin {
}
function Process {
}
function End {
}
main

calling a class/table method in WinSQL

new to Cache so please forgive me,
I'm trying to call a class/table method in WinSQL. Basically trying to go select table_methodname() but it says it's not finding the stored function.
In Cache SQL there seems to be either a result set stored procedure which acts like a normal T-SQL stored proc or a class/table method (stored function) that acts like a function.. those are the ones I'm having trouble calling in WinSQL..
thanks!
In order to call a ClassMethod from SQL, you must mark the ClassMethod with the [SqlProc] modifier.
ClassMethod MyMethod() As %Integer [SqlProc]
{
...
}
Then, you can call it with
SELECT MySchema.MyClass_MyMethod()
Documentation is here:
http://docs.intersystems.com/ens20141/csp/docbook/DocBook.UI.Page.cls?KEY=ROBJ_method_sqlproc

Calling a Module Function By Reference

I am looking at building a dispatch table for calling a number of Perl modules that I wrote.
As an example, if I have a package called Hello.pm with a simple function hello() in it, I would like to get a code reference to this function.
The following does not work:
my $code_ref=\&Hello->hello();
$code_ref->();
But if the function hello is exported from the package, then the following works:
my code_ref=\&hello;
code_ref->();
Does anyone know the correct syntax for the first case? Or is this simply not possible?
In the end, I would like to populate a hash table with all my code references.
##### Thanks for All Answers
The correct invocation as pointed out by several answers is:
my $code_ref=\&Hello::hello;
$code_ref->();
I have some 10 modules in 10 different files that I would like to load in a dispatch table.
This makes it easier for me to have the configuration loaded as data, and separate from code.
This allows me to load additional modules in a testbench without modifying my code-simply modify the configuration file. Mark Dominus, author of Higher Order Perl, has some nice examples on this.
If you want to refer to the hello sub in the Hello module, then call it, use:
my $code_ref = \&Hello::hello;
$code_ref->();
If you want to call a method named "hello" in Hello, you can do it like this:
my $method = "hello";
Hello->$method();
\&NAME takes a reference to a sub. Hello->hello() is not a sub name. As an expression, it would be a method call.
To get a reference to a method, use can.
my $method_ref = Hello->can('hello');
That will search the inheritance tree if necessary. Now that you have a reference to the right method, you can call it:
Hello->$method_ref()
-or-
$method_ref->('Hello')
If you need a callback that can't call the method properly, you'll need to create a callback that does.
my $code_ref = sub { Hello->hello(#_) };
Here's what it looks like fully dynamic:
my $pkg = 'Hello'; # Also works with object!
my $method_name = 'hello';
my $method_ref = $pkg->can($method_name);
my $callback = sub { $pkg->$method_ref(#_) };
What you probably want is
my $code_ref = \&hello;
$code_ref->();

MobileSubstrate: MSHookFunction example

I am trying to write a MobileSubstrate plugin which hooks into a C-method. I tried to edit the famous "ExampleHook", by just writing a demo MSHook and hook it in the Initialize method.
This is probably too optimistic and it doesn't work. But I cannot find anywhere a simple example of a MSHookFunction(). There is barely information about this on the Internet. It might be possible I misunderstood the whole concept of MSHookFunction.
Please, can anybody help me out with a little example code? I would deeply appreciate any help.
Best regards,
Marc Backes
I realize you have found this, but I am posting this answer to help whoever else may be needing this.
A simple example can be found at the MobileSubstrate article on the iPhone Dev Wiki, and an actual example of this in a project is at this bit of User Agent Faker.
But what is an answer without an actual explanation? Therefore, here we go!
void MSHookFunction(void* function, void* replacement, void** p_original); is the function definition for MSHookFunction, the magic function which causes your function X() to be interposed by Y(), for instance.
That is, when a program commonly would call X(), the call will be redirected to Y() instead. This is pretty much a basic explanation of function interposing.
Now, what are the parameters, and their usefulness?
function is a function pointer to the function you want to interpose. That would be a function pointer to X(), in our quick explanation.
replacement is a function pointer to the function you want function to be interposed with. In our quick explanation, that would be a function pointer to Y().
p_original is a pointer to a function pointer, which from now on will point to what function used to be.
The reason this is there is simple: If you intend to modify behavior, but not suppress it, you'll still need to call what X() used to be. But a common call to X() wouldn't work as intended, as it would end calling Y() instead of the default function.
Therefore, you have a function pointer to call X() as if it wasn't interposed.
Now, explaining the devwiki example:
static void (*original_CFShow)(CFTypeRef obj); // a function pointer to store the original CFShow().
void replaced_CFShow(CFTypeRef obj) { // our replacement of CFShow().
printf("Calling original CFShow(%p)...", obj);
original_CFShow(obj); // calls the original CFShow.
printf(" done.\n");
}
...
// hook CFShow to our own implementation.
MSHookFunction(CFShow, replaced_CFShow, &original_CFShow);
// From now on any call to CFShow will pass through replaced_CFShow first.
...
CFShow(CFSTR("test"));
Here, we:
Pass a pointer to CFShow, the function we want to change default behavior from as the function parameter.
Pass a pointer to the function we just created, replaced_CFShow as the replacement parameter. That is, whenever CFShow would be called by default, replaced_CFShow will be called instead.
We pass a pointer to the original_CFShow function pointer as the p_original parameter. Since we still want the things CFShow still does by itself to be done inside our replacement function, we call it.