Console Function Execution
by Demolishun · 02/15/2012 (1:42 am) · 0 comments
Part of my work with attaching Python to T3D I brainstormed an idea: Create an object in the console that communicated directly with a Python object. Sounds simple? I don't think so!
Console function execution is really interesting. When you type in "somefunction()" a lot of stuff happens before the actual function is called whether it be a script function or a method of an object. So when I wanted to change what happens when a call is made on an object it took some digging to understand how to change that behavior. I am still not sure I have it right as I have not written the code to test it yet. So here is what I found:
If anyone has a better idea on this let me know. My goal is to keep the calling of methods on my new object as dynamic as possible. That way I can call any function I want without worrying about it getting exposed to the script explicitly.
Console function execution is really interesting. When you type in "somefunction()" a lot of stuff happens before the actual function is called whether it be a script function or a method of an object. So when I wanted to change what happens when a call is made on an object it took some digging to understand how to change that behavior. I am still not sure I have it right as I have not written the code to test it yet. So here is what I found:
- Taking the approach of extending a ScriptObject (pseudo-code):
- I found in console.cpp on around line 1051 defined execute for calling a function on a SimObject. This is part of the call chain for a function call in the console. That is where I discovered that the namespace is the root of all function calls against SimObjects.
- An approach I may take is to define a new function type that will get returned for calls against functions on my new extScriptObject. Then I can use the type to call a callback that is designed to check against a python object associated with this new object type. I will have to define a new Namespace object to avoid changing the existing one. I think that will be the cleanest route.
class extScriptObject: ScriptObject{
// redefine get set of datafield methods
getDatafield(){
retrieve data from python object
}
setDatafield(){
set data of python object
}
// redefine getNamespace
getNamespace(){
implement alternate namespace object that does a function lookup in python and calls that function if it exists
}
};If anyone has a better idea on this let me know. My goal is to keep the calling of methods on my new object as dynamic as possible. That way I can call any function I want without worrying about it getting exposed to the script explicitly.
About the author
I love programming, I love programming things that go click, whirr, boom. For organized T3D Links visit: http://demolishun.com/?page_id=67
