Posts: 18
Threads: 7
Joined: May 2016
Reputation:
0
07-04-2016, 03:34 PM
(This post was last modified: 07-04-2016, 03:35 PM by clop1000.)
Is it possible to get a reference to an object through return type?
How do i do that?
for instance i want to make a simple function -
object findemptycell()
{
object o;
int i=0;
while (true)
{
o = retobject(i);
if (o != null)
{
if (o.category ==PowerCell)
{
if (o.position.x != nan)
{
message("sdsd");
return o;
}
}
i++;
}else break;
}
}
But this does not work.
Posts: 828
Threads: 26
Joined: Oct 2012
Reputation:
113
The only problem I see with your code is that the function may not return a value at all. Try adding a "return null;" at the end. Other than that, I think this should work.
Posts: 18
Threads: 7
Joined: May 2016
Reputation:
0
07-04-2016, 04:22 PM
(This post was last modified: 07-04-2016, 04:22 PM by clop1000.)
Yeah. I just got some kind of a glitch.
Now i am curious about - is there some kind of garbage collector.
extern void object::Spider2()
{
int[] q;
q = qw();
message(q[2]);
}
public int[] qw()
{
int testarray[]={1,2,3,4,5};
return testarray;
}
If am i correct - with every call of function a brand new array will be created?
And last but one will stay in memory without link on it. - with means wasted memory?
Can you advise me about this things?
Posts: 828
Threads: 26
Joined: Oct 2012
Reputation:
113
You don't need to worry about any memory management in CBot - the interpreter will handle everything for you (unless there is a bug, which is very likely because of how terrible the CBot interpreter code is). The array will be destroyed as soon as all references to it go out of scope (in this case - at the end of Spider2 function)
Posts: 18
Threads: 7
Joined: May 2016
Reputation:
0
07-04-2016, 05:14 PM
(This post was last modified: 07-04-2016, 05:14 PM by clop1000.)
But copy of this will be still active?
And a really little question. What type category are? I mean what type of variable can contain it?