- Home /
Function Abstraction Help
Hi, I am only going to show you snippets of codes, as the full scripts are too long.
The first script is where all the data is stored and has lots of functions to manipulate it. The function in question is like the one below (There are more, but they produce the same error):
var dataDict = new Dictionary.<int,LifeClass>();
public class InfoClass
{
var id : int;
var object : GameObject;
var script : Lifeform;
}
public class LifeClass
{
var self = InfoClass();
var mother = InfoClass();
var father = InfoClass();
var partner = InfoClass();
var children : List.<InfoClass> = new List.<InfoClass>();
}
...
function GetMother(targetId : int, mode : int)
{
if (mode == 0)
return dataDict[targetId].mother.id;
else if (mode == 1)
return dataDict[targetId].mother.object;
else if (mode == 2)
return dataDict[targetId].mother.script;
}
The second script that controls the AI, is where the functions are called. I am trying to keep a level of abstraction, to make everything easier for the user (me) to understand:
d = gameObject.Find("Data").GetComponent(Data);
...
d.GetMother(id, 1).transform;
Error reads 'transform' is not a member of 'Object'.
It should be a member as its type is GameObject.
What am I overlooking? Or is this just not possible?
Thanks for your time, sorry for the large amount of text.
Magnus :)
Answer by magnusm · Jan 12, 2014 at 07:02 PM
so that you understand why this is a fix - pragma strict enforces some degree of type safety. It is precisely because you are returning an 'Object' instance from Get$$anonymous$$other ins$$anonymous$$d of a 'GameObject' instance that this happens. A fix that doesn't lower your program$$anonymous$$g defenses is to return the correct type from the function i.e. use 'function Get$$anonymous$$other( targetID : int, mode : int ) : GameObject' as the prototype - however you will need to seperate out the functions by return type to use this. There is no way currently to know what Get$$anonymous$$other will return at compile time. However you are assu$$anonymous$$g it has the tranform member, so we are not returning the int (or possibly Lifeform).
EDIT: btw i would recommend never program$$anonymous$$g in this way - its not necessary and it makes code so hard to read that /the compiler/ can not understand your intent, let alone a human being - this is why the error occurs at all. every function can have a definite return type - even if you consider OO practices like inheritance - its exceptionally rare to need very late binding in practice, generally a network connection or an extremely hard application boundary has to be involved to really justify it - and even then late binding functionality in most languages is enormously heavyweight and overkill for what is needed...
Sweet, that's really helpful :)
I am going to bring back #pragma strict, and separate out the functions by type :)
Thanks
Why using seperate functions? you can just return the mother instance:
function Get$$anonymous$$other(targetId : int) : InfoClass
{
return dataDict[targetId].mother;
}
So ins$$anonymous$$d of the mode parameter you can pick the desired variable:
d.Get$$anonymous$$other(id).object.transform;
However if you really need them as seperate methods, feel free to implement them...
Ha, didn't know you could do '.object'. Very cool, thanks :P