Object to function pointer?
BlitzMax Forums/BlitzMax Programming/Object to function pointer?
| ||
Im using TMap to store a string as a key, with a function pointer as the value. I have to force both as objects when inserting them into the map. How do I get the function pointer back and call it?! Im a bit stumped here... Function CreateEntityByClassname:TEntity(Class:String) If MapContains(TEntity.Classes,Class) Local c()=MapValueForKey(TEntity.Classes,Class) Return c() EndIf Return Null End Function Function RegisterClass(cr:TEntity(),name:String) MapInsert(TEntity.Classes,Object(name),Object(cr())) End Function Theres some code for you. What im tryign to exactly do is register a creation function with a classname for each entity. When you want to create that entity, you call CreateEntityByClassname with the class you want to create, it will then call that entities classnames creation function and return the entity created. The creation function isn't in a type or a method. |
| ||
Hi, You can't convert between object<->function. You'll have to create a wrapper object for the function, eg (untested): Type TEntityCreator Method CreateEntity:TEntity() Return _cr() End Method Function Create:TEntityCreator( cr:TEntity() ) Local t:TEntityCreator=New TEntityCreator t._cr=cr Return t End Function Field _cr:TEntity() End Type Function CreateEntityByClassname:TEntity(Class:String) Local c:TEntityCreator=TEntityCreator( MapValueForKey( TEntity.Classes,Class ) ) If c Return c.CreateEntity() Throw "Unrecognized Classname:"+Class End Function Function RegisterClass(cr:TEntity(),name:String) MapInsert TEntity.Classes,name,TEntityCreator.Create( cr ) End Function |
| ||
Thank you, that seems to be working :) |