Shared LuaProxy for use extensions in lua. (Update on 2013-4-8)

Here

Welcome correction.

Today, added HttpClient support, request a url (get/post) in one line:
CCHttpRequest:open(“http://google.com/search?q=LuaProxy&safe=strict”, kHttpPost, “query=param1&other=params”):sendWithHandler(function(res, hnd) —[[ do something ]] end)

Just compiling TestLua on window. The following extra changes where needed:
in CCBProxy.h (adjusted the CCBproxy.cpp the same way)
//interface methods
virtual cocos2d::SEL_MenuHandler onResolveCCBCCMenuItemSelector(CCObject * pTarget, const char * pSelectorName);
virtual cocos2d::extension::SEL_CCControlHandler onResolveCCBCCControlSelector(cocos2d::CCObject * pTarget, const char * pSelectorName);
virtual bool onAssignCCBMemberVariable(cocos2d::CCObject * pTarget, const char * pMemberVariableName, cocos2d::CCNode * pNode);
virtual void onNodeLoaded(CCNode * pNode, CCNodeLoader * pNodeLoader);

except that onAssignCCBMemberVariable was different needed some small changes(different version I guess):

bool CCBProxy::onAssignCCBMemberVariable(cocos2d::CCObject * t, const char * pMemberVariableName, cocos2d::CCNode * n) {
CCString v(pMemberVariableName);
_memVars->setObject(n, v.getCString());
return true;
}

—————— also

added to properties in the configuration Properties / Linker / input/ additional Dependencies
pthreadVCE2.lib;libcurl_imp.lib;

all compiled but not tested yet. Hope this will help others

Thanks, I hope this works it would be terrific… Will try Monday I have to go away this weekend

Good luck.

Andre

In my app, that is fine.
The 2nd argument in member function “onAssignCCBMemberVariable” of interface “CCBMemberVariableAssigner” is CCString, so I simple use that, won’t change.

There must be two different versions.
I am using cocos2d-2.1beta3-x-2.1.1 which I believe is the most recent version.
I check and CCBreader uses CCString in places so obviously there no reason not to use it there.

I there a repository where the current code is maintained?

Did you get your code from another source?

I am wondering where else our code differs. This difference can be detected by the compiler others would not be.

This is very frustrating.

Andre

Ouch! Sorry, I’m using 2.0.4 …
Interface changed … I hate that …
After I come from travel, I’ll change it.

this works on window to a certain extent:

local function ExtensionsTestProgram(scene, layer) — the caller creates a scene with a layer and replaces the current scene with this new scene
local proxy = CCBProxy:create()
local n = proxy:readCCBFromFile(“ccb/HelloCocosBuilder.ccbi”) — Got a CCNode
local l = tolua.cast(n, “CCLayer”) — Cast the node into CCLayer
layer:addChild(l)
end

I had to change CCBProxy::readCCBFromFile second line to

lib->registerCCNodeLoader(“HelloCocosBuilderLayer”, ProxyLayerLoader::loader());

otherwise HelloCocosBuilderLayer is not register.

I really do not know what I am doing :slight_smile:

Good luck

Andre

Hi,shawn,you do great job!

but i can’t compile with the code in xcode.

andre give the way to resovled the errors in ccbproxy.cpp

there is still many compile error with other cpp files in folder UI

could u try to correct these problems in Xcode!

thanks for your work

I came back form South Africa, There is very very beautiful.


Don’t add UI/UI* into XCode project, it’s only ready for win32.
Maybe I forget to mark it.

Now, this repo just fit my project, somewhere is not perfect, and I’m going to fix it, in those days.


To Andre Arpin:
You should write some helper function, like this:
function MyScene:new()

self.*proxy = CCBProxy:create
self.*nodes = {}

end
function MyScene:loadCCBI(file, key, t)
local n = self.*proxy:readFromCCBFromFile
if t then n = tolua.cast end
if key then self.*nodes[key] = n end
return n
end

I’d add 2.1.1 support of CCBProxy::onAssignCCBMemberVariable.
And update ui/UIEventDispatcher, you can keep it in Xcode.
Since the choice of using lua, so I don’t hope do more things in C++, just support basic function, and reduce the number of call C function in lua.

hi,Shawn,
Your work is great,but I don`t understand how to use it.I meet the trouble like Andre.
could you give an example for loading the “HelloCocosBuilder.ccbi”?

hi,shawn

when i use a complex ccbi file in MAC OS, i got many errors with handleMenuEvent and handleButtonEvent. i think there’s some memory overflow ,could u check for it

thanks a lot!

hi,shawn
:(,I Modify the code,add the function registClassName for rejection map.now it can show ccbi picture.The lua file is like this:

local function TestSelector(target, event)
print(target, event)
end

function ExtensionsTestProgram(scene) — the caller creates a scene with a layer and replaces the current scene with this new scene
local proxy = CCBProxy:create()
proxy:registClassName(“HelloCocosBuilderLayer”) —regist for map
local n = proxy:readCCBFromFile(“ccb/HelloCocosBuilder.ccbi”) — Got a CCNode
local l = tolua.cast(n, “CCLayer”) — Cast the node into CCLayer

local label = proxy:getNodeWithType(“mTestTitleLabelTTF”, “CCLabelTTF”)
print(label)
local SpriteObj = proxy:getNodeWithType(“mBurstSprite”, “CCSprite”)
print(SpriteObj)
proxy:handleSelector(TestSelector)
scene:addChild(l)
end

but the code, proxy:getNodeWithType, proxy:handleSelector,they can not work,print function just get nil.
I do not know how to use it. T-T

I’d add 2.1.1 support for all files.

to h h:
proxy:handleMenuEvent() just simple call CCMenuItem::registerScriptTapHandler.
proxy:handleButtonEvent() created a LuaEventHandler and store into proxy’s dictionary. You should try to release the proxy while scene poped.

to zero gz:
You should try this to get a sprite:
local n = tolua.cast(proxy:getNode“mBurstSprite”, “CCSprite”)
Now, handleSelector just a pilot function, it’s not finished.
And, I don’t know registClassName how to work, and what does it do?

shawn,i used only one game scene,but add two layer in the scene.

when i use menuitem, it always do the right job.

but button can’t work


when i set breakpoint
in create handler, and controlAction function
and run the program step by step, it works

since the breakpoint was cleared, things went wrong. the data in LuaEventHandler object been broke , such as _lua= 0x00000000

i to use global var to avoid collectgarbage collect useful objects, but this dosent work.

For button, I wrote these in my lua scene manager class:

function Scene:new()

self.*ccbProxy = CCBProxy:create
self.*ccbProxy:retain()

end
function Scene:bindButtonEvent(b, fn, e)
self._ccbProxy:handleButtonEvent(b, fn, e or 0)
end

So I can easily bind in scene manager (sub class) like this:
self:bindButtonEvent(button, function(s, e) dosomething() end)

hi, Shawn:
I am still puzzled, T-T
Could you show me an example for loading the “ccb/HelloCocosBuilder.ccbi”(just the main scenen) by the simple lua code?

if I try to load HelloCocosBuilder.ccbi like this:
local proxy = CCBProxy:create()
proxy:readCCBFromFile(“ccb/HelloCocosBuilder.ccbi”).
I get an error for “Assertion failed!
Line 70,Expression:ccNodeLoadersIterator != this~~>mCCNodeLoaders.end"
ccb/HelloCocosBuilder.ccbi has a user define Layer named “HelloCocosBuilderLayer”.I need to bind the Layer name to SyncClass,so that while loading the user define layer,it can know how to load it.
c++ code is: lib~~>registerCCNodeLoader(”HelloCocosBuilderLayer“, ProxyLayerLoader::loader());
—————————————————————————-
Shawn write To Andre Arpin:

You should write some helper function, like this:
function MyScene:new()

self.*proxy = CCBProxy:create
self.*nodes = {}
….
"

The helping code I do not unstand :(, could you show me the detail?
if the code try to load ccb/HelloCocosBuilder.ccbi, it also has the same call “proxy:readCCBFromFile(”ccb/HelloCocosBuilder.ccbi“)”.Maybe it will get an error.

my cocos2dx version is 2.0.4
ccb/HelloCocosBuilder.ccbi is in file cocos2d-2.0-x-2.0.4\samples\TestCpp\Resources\ccb .

Example, like this, read a layer, and add to scene.

local file = “ccb/xxx.ccbi”
local proxy = CCBProxy:create()
local node = proxy:readCCBFromFile(file)
— If you were added a label named “Lab”
tolua.cast(proxy:getNode“Lab”, “CCLabelTTF”):setString“I found the Lab”
local scene = CCScene:create()
scene:addChild(tolua.cast(node, “CCLayer”))
CCDirector:sharedDirector():runWithScene(scene)

Here, the “proxy” is a temp var, you may put it in any longer life place, don’t forget retain.

@shawn

i comment the line h~~>autorelease,then it works.
LuaEventHandler * LuaEventHandler::create{
LuaEventHandler *h = new LuaEventHandler;
h~~>_lua = l;
//h->autorelease();
return h;
}