Passing Object Pointers to Functions

Blitz3D Forums/Blitz3D Beginners Area/Passing Object Pointers to Functions

Irvau(Posted 2016) [#1]
So I tried passing some object pointers to a function, and I've run across something unexpected.
Whenever I run my code, I'm greeted with an error stating that the global pointer passed to a function doesn't exist.

Here's some code; it may not be the code I'm working on, but it demonstrates the result just as well.


I had thought that the object pointer "A" would've been passed and referenced as "B" within the function, and whenever the "new" operator was used that the global pointer would be assigned the new object. Apparently, however, this converted "B" into a local pointer.

Now, I tried creating the object before passing it's pointer to the function.



Indeed, "A\LAWL" did equal 5.

So what's up with this? Does creating a new object within a function convert that object to a local one, regardless if the name used is the same one as a parameter?

If so, is there a way to keep on referencing the passed pointer within a function? I'm not too sure if I could go about creating the objects before passing them to a function, as the objects have to pass some conditionals so that they can be renewed.


Floyd(Posted 2016) [#2]
Arguments are always passed by value, i.e. a copy of the original.

You are passing a pointer, not the object pointed to.

So B is a copy of A, but they point to the same object.


Bobysait(Posted 2016) [#3]
The only trick to pass a variable to a function is using an array

type mytype
	field v%;
End type

local a.mytype[0];

do(a);
print(a[0]\v);

waitKey();
end;

function do(v.mytype[0])
	v[0] = new mytype;
	v[0]\v = 5;
end function


ps : the array can be local or global.


Irvau(Posted 2016) [#4]
Huh, neat. That pointer vs object dealio gets me easily. :P

So arrays are the only data type passed by reference instead of by value?


steve_ancell(Posted 2016) [#5]
Also works this way, although this way the object needs to be created outside of the function beforehand. The way I show is OK for a quick 'n dirty way to access one object, Bobysait's way seems a lot more practical.


Graphics 800, 600
SetBuffer BackBuffer()

Type mytype
	Field v%
	
End Type

Local b.mytype = New mytype

Do(b)

Cls
Text(0, 0, b\v)
Flip

WaitKey()
End


Function Do(v.mytype)
	v\v = 5
	
End Function




Irvau(Posted 2016) [#6]
Well, as stated above, I need the object to pass some some conditionals within the function before it's to be re-created, so I can't really create the object before passing it on.

Thanks nonetheless!


steve_ancell(Posted 2016) [#7]
That's sort of the same way it is done in Monkey-X, create an object from within a function and store it in an array. Although it's a lot easier to do in Monkey-x.


steve_ancell(Posted 2016) [#8]
I used to store objects in arrays with Blitz3D too when I was still using it, although I never did it the same way as Bobysait does it. That is kind of cool! ;)


Irvau(Posted 2016) [#9]
Huh, I thought that objects in Monkey X were added to arrays/lists so that the GC wouldn't pick them up. Monkey X doesn't have operators such as "After" and "First" which allow it to sift through a pool of objects, instead they have to be assigned to individual lists.

Here it seems to be that arrays are the only data type passed by reference.


steve_ancell(Posted 2016) [#10]
The Garbage Collector in Monkey-X will pick up anything that is dereferenced regardless of whether it's in an array or not.

To explain it a bit further. If you were to create a global object it will always exist, as soon as you redefine it as null it will get mopped up by the GC.

If you create an object inside a function it will get mopped up as soon as the function has run its course due to the variable dying in the proccess. Hence why objects are placed in an array, also because it allows for a multiple collection of one class type.

You can also do linked-lists in Monkey-X.


steve_ancell(Posted 2016) [#11]
My apologies if it seems like I don't make sense sometimes, I'm better at doing rather than explaining things.

And if it ever seems like I jump the gun or fly off the handle then please don't take it personally, I just get like that sometimes.


Spencer(Posted 2016) [#12]
In Blitz3D/BlitzPlus you can use Handle and Object to pass a reference of an object around.


Type Point     ;Define our Point Type.
  Field x
  Field y
End Type

Global p.Point = New Point        ;create a new point
Global p_ref = Handle(p)          ;get the "handle" or reference to our new point


Function UpdatePoint(point_ref)                    ;Point is passed by reference

  Local refPoint.Point = Object.Point(point_ref)   ;Reference is explicitly cast back to an Object of type Point.

  refPoint\x = 100                                 ;Update the X coordinate

End Function


UpdatePoint(p_ref)              ;pass our reference to the function                 

Print "p.x = " + p\x            ;verify that x has been updated.

Input "Press Enter to Exit"