Passing datablocks or con objects from script to C++
by Dave Strock · in Torque Game Engine · 08/03/2002 (9:05 pm) · 2 replies
Does anyone know if its possible, and if so how, to pass a datablock from script to C++ code? Or possibly create a con object and initialize it in script and then pass it to C++ code?
My problem is this: I have a bunch of items I'm keeping track of in C++ code (an inventory system). The problem is initializing these items. They have many data members, and depending on their type they different numbers of data members. Passing 10+ variables through a console function to C++ to be initialized there isn't fun, nor elegant. Neither is creating a console function to set/get at each of these elements. So I was thinking there must be a better way. Some way to either send the datablock to C++ where it can access the data members there for initialization. Or possibly declaring my "item" class to be a con object and initializing it in script and then passing it to C++.
Can anyone point me in the right direction here?
My problem is this: I have a bunch of items I'm keeping track of in C++ code (an inventory system). The problem is initializing these items. They have many data members, and depending on their type they different numbers of data members. Passing 10+ variables through a console function to C++ to be initialized there isn't fun, nor elegant. Neither is creating a console function to set/get at each of these elements. So I was thinking there must be a better way. Some way to either send the datablock to C++ where it can access the data members there for initialization. Or possibly declaring my "item" class to be a con object and initializing it in script and then passing it to C++.
Can anyone point me in the right direction here?
About the author
#2
I pretty much knew what conobjects are, and how they work, I've just never actually implemented my own. So far I've done everything through exposing C++ functions to the script using Con::addCommand().
As for why I'm doing it in C++ rather than script, one major reason is that I'm more comfortable writting C++ code. (Typeless languages mess with my mind sometimes :) And another is that I just didn't think it could be done in script, atleast how my knowledge of the scripting language was at the time.
Basically, what I'm doing is an inventory for the RPG that I'm currently working on. Players can pickup items and move them around the inventory, stack like items, open up bags and other containers to put items in, inspect items, etc.
Here is what I'm currently doing, let me know if this is a good idea, or if there is a better way to do it.
I have a bunch of items, declared in various datablocks in script. Then in ItemData::onPickup() in ~/server/scripts/item.cs I'm adding the item to my C++ inventory like this:
addNewItem is a C++ function that I exposed to script using Con::addCommand(). That function then constucts the item using the paramaters passed in, and then adds it to the inventory system.
Then I have other functions exposed to script to get at these members to do things like get the bitmap to display the icon for the item in the inventory gui, or decrement the amount of the item, etc.
This system works ok, I have it managing the items fine, and the gui works, etc, but its not a very clean system overall. Thats why I initially posted this, thinking it might be better if you could pass in a datablock to addNewItem() or something like that.
Since then I've found that you can constuct a datablock whenever you want, by just having the name of the datablock you want, so part of this question I already answered for myself.
Actually, is there a better way to get at a datablock than this:
That way works, but it seems kinda silly to create an item like that just so I can call getDatablock() on it. Its all I've managed to get working tho.
Any suggestions that anyone has would be great.
08/06/2002 (3:24 pm)
Thanks for the reply Bryan.I pretty much knew what conobjects are, and how they work, I've just never actually implemented my own. So far I've done everything through exposing C++ functions to the script using Con::addCommand().
As for why I'm doing it in C++ rather than script, one major reason is that I'm more comfortable writting C++ code. (Typeless languages mess with my mind sometimes :) And another is that I just didn't think it could be done in script, atleast how my knowledge of the scripting language was at the time.
Basically, what I'm doing is an inventory for the RPG that I'm currently working on. Players can pickup items and move them around the inventory, stack like items, open up bags and other containers to put items in, inspect items, etc.
Here is what I'm currently doing, let me know if this is a good idea, or if there is a better way to do it.
I have a bunch of items, declared in various datablocks in script. Then in ItemData::onPickup() in ~/server/scripts/item.cs I'm adding the item to my C++ inventory like this:
// %data == the datablock of the item that was picked up %user.addNewItem(%data.getName(), %amount, %data.description, %data.typeID, %data.iconPath, %data.size);
addNewItem is a C++ function that I exposed to script using Con::addCommand(). That function then constucts the item using the paramaters passed in, and then adds it to the inventory system.
Then I have other functions exposed to script to get at these members to do things like get the bitmap to display the icon for the item in the inventory gui, or decrement the amount of the item, etc.
This system works ok, I have it managing the items fine, and the gui works, etc, but its not a very clean system overall. Thats why I initially posted this, thinking it might be better if you could pass in a datablock to addNewItem() or something like that.
Since then I've found that you can constuct a datablock whenever you want, by just having the name of the datablock you want, so part of this question I already answered for myself.
Actually, is there a better way to get at a datablock than this:
%obj = new Item() {
datablock = %itemName;
};
%data = %obj.getDatablock();That way works, but it seems kinda silly to create an item like that just so I can call getDatablock() on it. Its all I've managed to get working tho.
Any suggestions that anyone has would be great.
Torque Owner Bryan "daerid" Ross
Anything you can access in the scripting engine had to be declared as such (either via ConsoleMethod, ConsoleFunction, or ConObject::addField), so you should be able to access a conobject from c++ with no problems.
My question tho, is why are you initializing script objects in c++ code? Why not just initialize them in script?