2.8 Extender Functions and Member Variables

Started by Recluse, Fri 07/09/2007 03:24:56

Previous topic - Next topic

Recluse

Is there any particular reason why this won't work?

Code: ags

function (Object* object, String str){
this.Name = str; // EDIT, used to read object.Name = str;
}


It's telling me that it can't access object.Name since it's a private member variable.

What's the point of having extendable functions if we can't access private variables? I really don't want to hack around this one, since I would absolutely HATE to have to create code that's more complex than neccesary. Is there any way to access this member variable, or am I just doing it wrong?
All your verbcoin are belong to us.

Gilbert


monkey0506

#2
As Gilbot pointed out, you use the this keyword. And extender functions are implemented like this:

Code: ags
function SetName(this Object*, String str) {
  this.Name = str;
  }


EDIT:

Wait...of course you implemented the extender function properly. Otherwise you would have other issues on your hands. The error given is:

Code: ags
Error (line X): property 'Object::Name' is read-only


That's because Object::Name is internally defined as (taken from 2.72's ROOMEDIT.CLB):

Code: ags
managed struct Object {
  // ...
  readonly import attribute String Name;
  // ...
  }


You really needn't worry about that 'attribute' in there...it's not really relevant to the problem. The problem is that the property is declared as readonly and not writeprotected which for the end-user are functionally the same...unless you want to try setting the value of the property. readonly variables can only be set using certain methods not available to the AGS script (such as plugins). I'm not completely clear how they work, simply that they do.

If you could convince CJ that he needs to change this one keyword, then we could implement much more useful extender functions, but this does bring into question what we should actually have access to via extender methods and what we shouldn't. There are many hidden properties within AGS that are presumably vital to normal functioning. But where do we draw the line? Clearly things like ID properties should never be modified by a user-script. But what about other properties? Perhaps this is something that should be opened for more discussion?

Rui 'Trovatore' Pires

BTW, I was just checking the manual for some info in the "this" keyword. I'm not a programming mage, but am far from being a newbie, and everything in that help page is understandable (even arrays and structs, which for a while have messed with my head) except for the use of the "this" keyword, because the example itself isn't obvious to understand (function Whatever::Whatever2? WHat's the "::" doing in there? What is it for, why is it there?). Could I ask for a bit of clarification in the manual, CJ?
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

scotch

#4
A readonly property can't be changed because it doesn't provide a write function. It's not a case of AGS blocking access so much as not providing it, probably for good reason.

In AGS an object's Name never changes at runtime currently (much like the ID), and it's likely the engine expects it to stay that way, otherwise it'd provide access. The point of extender methods is only to look tidier. You can't do anything with them you couldn't before.

Rui, :: is the "scope resolution operator" in programming languages such as C++. It's used to refer to things in particular structs/classes/namespaces. Just means "Name" that is a member of "Object".

Recluse

#5
Oh dear, I seem to have confused you all while bringing a very good point of conversation to the table. (Don't hurt me)

I actually need the object's DESCRIPTION to change, as given in the 2.8 editor. From what I can see in the help file, this is possible to set in the editor, but I have yet to find a function that does this. Unless of course Description is a text property, in which case it needs to be noted as such in the help files.

EDIT: Even if Description is a text property, (and from what I can tell it is), there is no Set function for text properties...  :-\  I'm at a loss as to how to do this, without hacking into SSH's Description Module and making it work that way.
All your verbcoin are belong to us.

SSH

You can't change Object names at run time. The easiest workaround is usually to use a different object: tunr the old one off, turn te new one on at exactly the same place as the old one.
12

Recluse

Weeelll that makes sense... Dunno why I didn't try that before...  ::) Thanks SSH and everyone else who contributed!
All your verbcoin are belong to us.

SMF spam blocked by CleanTalk