How to directly reference an object?

Monkey Forums/Monkey Programming/How to directly reference an object?

xAD(Posted 2012) [#1]
Is it possible? I don't recall seeing any pointer stuff in Monkey, did I miss something?
I'll need to go through all drawn objects, but in specific orders rather than just generically modifying each. Searching through them to find a specific name in a field, then searching to find the next, etc. will, I think, take too long.
So I need a way to get directly to a specific object.

Please answer at a remedial level :)


ziggy(Posted 2012) [#2]
Get an instance of the object and store it. Monkey uses references instead of pointers:
[monkeycode]
Local myObject = New MyObject 'An object is created here, and the variable myObject points to it (it is a reference)

myObject = New MyObject 'The same variable now points to another object (it holds a reference to a new MyObject). As the previous object is not being referenced my any variable, it gets deleted.

local anotherReference = myObject 'Now, the varaible "anotherReference" also points to the same object as myObject.

myObject.myField = 100 'This will be modified on the object pointed by "myObject" and by "anotherReference"

MyObject = Null 'anotherReference still points to the same object instance.

anotherReference = Null 'No one else is pointing to the object, so it is deleted from memory
[/monkeycode]


NoOdle(Posted 2012) [#3]
xAD can you post a little more information please?

I'll need to go through all drawn objects, but in specific orders rather than just generically modifying each

Is this to draw each object at the correct depth or? A list can be used and then the contents sorted using a field value meaning you could then iterate over the list knowing the order is correct.

So I need a way to get directly to a specific object.

You could store the objects in a Map using a key to retrieve them or you could also use an array and have a field in the object that stores the index for quick retrieval.


xAD(Posted 2012) [#4]
Thanks, ziggy. I think I actually knew that at some point, but was having an extreme mental block.
This variable type that references an object...is there anything odd about it I should know? I assume you can't do maths on it, but is it valid to make an array of them? Or use it as a Field in another object?
How would you declare that field? Field objectReferencer:MyObject?

NoOdle, changing the apparent depth objects are drawn at would be one possibility.
You know how a game loop generally goes input-processing-output?
I'm separating special effects from game code. So my loop would go input-game_processing-effects_processing-output.
I've had too many projects screech to a halt because I've painted myself into a corner with an effect tied into some game process or other, so I've been working out a way to generically isolate the two


ziggy(Posted 2012) [#5]
This variable type that references an object...is there anything odd about it I should know?
No, they're like pointers but they give you direct access to the pointed object, no cast operations required and you can't manipulate the mem address they're pointing. This pointer's like approach is called references. It's a a bit more abstracted and highlevel than traditional C pointers, but you can do mostly the same but with easier memory handling and without the risk of trying to access a memory section that has already been released. That prevents invalid memory access and also helps preventing memory leaks on your game logic.

I assume you can't do maths on it
You can't make maths on it, but make maths on its contents, like modifing a field.

it valid to make an array of them?
Of course! Any array is in fact an array of them.

Or use it as a Field in another object?
Yes! of course. Regular variables, except from Int, Float, Bool and String are of this kind, feel free to use them as you please. That's how you're suposed to make OO coding on Monkey.

How would you declare that field? Field objectReferencer:MyObject?
Yes.


Also, it may help you to know that you can use any typename as a casting operation:

Local b:MyClass = MyClass(object)

In this sample, if the variable called "object" contains a reference to a MyClass object (or any object extending MyClass), it is assigned to the variable "b", otherwise "b" gets a value of Null (that is no reference). That's very useful.


tiresius(Posted 2012) [#6]
I was wondering about the same thing and hopefully this clears it up and I can clean up some of my clunky code searching for objects. Thanks!