How to get the CCBAnimationManager from a custom class in cocos2dx - cocos2d-x

I'm loading a CCB file from my Cocos2d-x project, but I can't get access to the CCBAnimationManager from a custom class.
In Cocos2d with objective-C is as simple as:
CCBAnimationManager* animationManager = self.userObject;
But the same implementation in C++ returns a null value and the execution will cause a crash...
any idea? Thks

Yes, it is not quite simple as in obj-C. New TestCpp project got an example for this. Look at the (CocosBuilderTest->AnimationsTest).
Basically you need to create a CCNodeLoaderLibrary and register it with your custom loader class.
CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary();
ccNodeLoaderLibrary->registerCCNodeLoader("TestAnimationsLayer", AnimationsTestLayerLoader::loader());
and then Create a CCBReader with that ccNodeLoaderLibrary
cocos2d::extension::CCBReader * ccbReader = new cocos2d::extension::CCBReader(ccNodeLoaderLibrary);
and now, you will be able to get hold of CCBAnimationManager via this ccbReader
ccbReader->getAnimationManager()

Related

Making changes to functions of protoc generated APIs

Suppose I have a person.proto file-
syntax = "proto3";
/* Class Person */
message Person{
string name = 2;
}
Then, I use protoc to generate the source code in Python, or C++.
Question 1:
Suppose I generated the API code, now what I can do to create an object of Person class, is:
person = person_pb2.Person() # In Python
Person person; // In C++
What I want is, every time I create an object of Person class, the reference to this object is stored with a unique ID inside a map.
Basically, I want to create something like a map, to store the reference of every object of each class that is ever created.
One way to achieve this I thought of is to add a custom line of code (which calls a different function) to the classes' constructor, such that every time an object is created, that function will add reference to it to the map.
Is this approach good? If yes, then how to achieve this- customizing class constructors of protoc generated source code (in every language- C++, Python, Java, etc.). If no, then what could be a better solution?
Question 2: For the fields, we have setters and getters. e.g.
person.name = ... # In Python
person.set_name(...) // In CPP
So, what if I want to add something more to the set_name() function, how can we do that?
Like if I want to call a function inside set_name(), then how to achieve that?
In short, both the questions sum up as "How to make changes to the functions of API that protoc generates (in all the languages)?"
Is there something to do with plugins? Or with descriptor files? Or something else?

Access functions and variables from another script in Unity

I've been searching for this for few hours now and didn't get any result. I have a script called GameSetup.js that is attached to an empty game object (named GM) . It holds references to my current camera and has functions to get screen width and other stuff. I'd like to access these functions ( and variables) from other scripts. so for example I have another empty game object called "Blocks", one of its components is BlockGenerator.js , which has BlockGenerator() function. I want to be able to use screenWidth() in BlockGenerator() like this:
BlockGenerator.js
BlockGenerator(){
var Blocknumber : int = GameSetup.ScreenWidth / blockWidth;
}
GameSetup.js
function ScreenWidth() : float{
var screenWidth : float = (mainCam.ScreenToWorldPoint(new Vector3(Screen.width,0f, 0f)).x)* 2;
return screenWidth;
of course GameSetup is not recognized in BlockGenerator.js if I use GetComponent(), for example. I'm not sure if I can drag GM game object in the inspector and pass it to BlockGenerator.js , I haven't tried. but the thing is, it makes the game script very complicated for no good reason. how can I setup and access global functions and variables?
In the class that holds the BlockGenerator method you need to define a public variable of type GameSetup. Then in the inspector drag the object whose instance variables you wish to access. After that you can access the object in your BlockGenerator method and call its methods and access its instance variables.
Another way is to find the object by tag in BlockGenerator via https://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html or search by type with http://docs.unity3d.com/Documentation/ScriptReference/Object.FindObjectOfType.html.

getDefinitionByName not finding a variable that exists

I am using flash.utils.getDefinitionByName in an attempt to grab an art asset. I use this function quite a bit and haven't had trouble until now. Check it:
assetName = Assets.MegaBerry; // works
assetName = getDefinitionByName("Assets.MegaBerry") as Class; // doesn't work
What the heck?? Error response for the second line is "Variable not found."
If it matters: Assets is a file in my root source directory (it has no package; Assets is the fully qualified name) and I've tried putting:
import Assets;
at the top with no luck.
For reference, in Assets.as I have:
[Embed(source = "../art/Inventory/MegaBerry.png")]
public static var MegaBerry:Class;
Your problem is that embedding the resource into the Assets class will create a static variable of type Class that belongs to that class - which is what you are referencing when you use Assets.MegaBerry: A variable(!) of type Class.
It does not, however, register the MegaBerry class to a fully qualified class name. To do this, you have to use - who would have guessed it - registerClassAlias at some point in your application:
registerClassAlias("Assets.MegaBerry", Assets.MegaBerry);
After that, it will be available everywhere else when calling getDefinitionByName.
** EDIT **
Well that's some unexpected behavior... It turns out, the class that was embedded is in fact automatically registered, but under {className}_{variableName}, instead of the notation you would expect. So using:
getDefinitionByName("Assets_MegaBerry") as Class;
should to the trick.
registerClassAlias also works, but then you need to call getClassByAliasinstead of getDefinitionByName. Sorry for the mix-up.
** END EDIT **
You can also use the Embed tag to inject the resource into a separate class file, which you can then reference as expected by using getDefinitionByName, or simply using an import:
package assets {
[Embed(source="../art/Inventory/MegaBerry.png"]
public class MegaBerry extends BitmapData {
}
}
Instead of calling
assetName = getDefinitionByName("Assets.MegaBerry") as Class;
, instead just use:
assetName = Assets["MegaBerry"];
try:
[Embed(source = "../art/Inventory/MegaBerry.png" , symbol="MegaBerry")]
public static var MegaBerry:Class;
In actionscript, objects actually have a name property that is different from the actual variable name as it shows in code.
For example, if you create a variable as follows,
var myBerry = new MegaBerry();
Then getDefinitionByName("myBerry") will return null.
Only when you set the name of the variable by writing myBerry.name = "myBerry", will getDefinitionByName("myBerry") return what you want it to. The name of the object doesn't necessarily have to be equal to the variable name in code.
In your specific case, I don't think you need to use any of that anyways. Have you tried assetName = new MegaBerry() ?
If you want to find out what the fully qualified name of you class really is, you may do the following:
trace(getQualifiedClassName(Assets.MegaBerry));
You may do that from inside Assets.as, for instance.
You can feed that string back to getDefinitionByName() and get a reference to the class.
trace(getDefinitionByName(getQualifiedClassName(SomeClass)));
// output [class SomeClass]
And remember, getDefinitionByName() only gets you references for classes that are in the same scope as the getDefinitionByName call itself. So, if you are loading external SWFs, getting class references will depend on the application domain you are using and the place, where this code executes.

AS3 - Parametrized Factory method using actual class name

Rather than use a hard-coded switch statement where you pass it the string name of a class and it then instantiates the appropriate class, I'd like to pass the actual name of the class to my factory method and have it dynamically create an instance of that class. I thought it would be trivial and am surprised it is not working. I must be missing something quite basic:
sample code:
createProduct(50, "Product1Class");
createProduct(5, "Product2Class");
private function createProduct(amount:uint, productClassName:String):void {
var productReference:Class;
try {
productReference = getDefinitionByName(productClassName) as Class;
for (var i:uint = 0; i < amount; i++) {
var product = new productReference() as ProductBaseClass; // throws reference error!
}
} catch (error:ReferenceError) {
throw new ReferenceError(error.message + " Have you linked a library item to this class?");
}
}
The only thing that may be a little odd (not sure) is that these "products" are actually linked Library items (ie: I have a movieClip in the Library that has a linkage to Product1Class and another to Product2Class both of which extend ProductBaseClass, which in turn extends MovieClip.
Why the ReferenceError?
If you have a runtime loaded library then the Class's are not compiled into the main swf, so you get the runtime reference error when you try to create them.
To work around this you can declare "dummy" vars of the classes you want to compile, or if using the flex compiler there are options to include the classes you are missing.
e.g. declare these anywhere in your project
private var p1:Product1Class;
private var p2:Product2Class;
Its a frustrating problem, if your classes extend MovieClip which is a dynamic class you might be able to access the properties etc by doing something like this:
var product:MovieClip = new productReference() as MovieClip;
p1["someCustomProperty"]; //Dot notation might work here as it is a dynamic class
Chris is absolutely right, the ReferenceError is actually being thrown during the call to getDefinitionByName, meaning that the reflection method cannot find Product1Class or Product2Class in your application domain. You can always check if a definition is available by checking the application domain directly, like:
// inside your createProduct method, yields 'false'.
ApplicationDomain.currentDomain.hasDefinition( productClassName );
Are these library assets loaded in at runtime? If so, you can either make sure that the library swf is loaded into the current application domain by adding an appropriately configured LoaderContext to your Loader, or you can replace the call to getDefinitionByName with the loaded swf's application domain's getDefinition method.
getDefinitionByName() and ApplicationDomain.currentDomain.hasDefinition() require full qualified class names. The example code in the original post works when Product1Class and Product2Class are in the default package. However, if you move the product classes to another package, you have to make sure that you are supplying the fully qualified class name to getDefinitionByName().
So if we put our product classes in com.example.products, then the call becomes:
productReference = getDefinitionByName("com.example.products.Product1Class") as Class;
I'm not really sure what the best practice is with this kind of dynamic factory class, but what I ended up doing (since all products were in the same package) was to create a constant within my factory class that defines the package for my products:
private const PRODUCT_PACKAGE:String = "com.example.products."; // note the trailing period
So that way your client code doesn't need to know (nor define) the product package. You just prepend this constant to your product class name when using getDefinitionByName().

Adding state to bean generated via CGLIB's BeanGenerator

I see the following example code on how a Java Bean can be created dynamically. What I am not able to figure out is how I can update the state of an instance once created.
So in the following example how can I set the value of foo to "footest" for instance bean?
BeanGenerator bg = new BeanGenerator();
bg.addProperty("foo", Double.TYPE);
bg.addProperty("bar", String.class);
Object bean = bg.create();
Thanks
Kumar
A few ways come to mind:
1/ Call "setFoo()" on "bean" using reflection. Not pretty.
2/ Use a helper library to do the same thing - e.g. Commons-Beanutils o.a.c.BeanUtils.PropertyUtils.setProperty(bean, "foo", "footest").
This way works nicely for me. You could use the Spring equivalent if you're already using Spring.
3/ Use CGLIB's BeanMap upon your new bean. e.g.
Map beanMap = BeanMap.create(bean);
beanMap.put("foo", "footest");