• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
InvalidPointer

C#-style property getters/setters

8 posts in this topic

Just gauging interest in the concept, though I don't know what percentage of AS users frequent these forums. I've got a (second-generation, actually) implementation going that makes minimal changes to the actual back-end but makes a few enhancements to the parser/builder interfaces that let the compiler infer or generate function signatures on-the-fly. The net result is that you can now use the super-sexy C# property syntax like so:

In interfaces:
[code]
int TestProperty
{
get;
set;
}

AppInterface@ AltTestProperty
{
get;
}

float AnotherTestProperty
{
set;
}
[/code]

In actual classes and/or global property definitions:
[code]
int SomeRandomInteger;

int AGlobalProperty
{
set
{
// You even get the implicit 'value' parameter gratis!
SomeRandomInteger = value;
}
get
{
return SomeRandomInteger;
}
}
[/code]

while still 'talking' to them via the bog-standard
[code]
AGlobalProperty = 50;
[/code]
in external code. If anyone's interested, give me a holler here, but I'll probably just ship this off to WitchLord as soon as I work out a few parser kinks (for the interested, defining multiple getters/setters within a single property block doesn't fall-through quite as elegantly as I expected, the major result being some odd error messages) but at time of writing this does all compile and work more or less correctly. Next on the agenda is maybe using this to do some cool foreach trick :)

EDIT: I'll probably also make a few syntax tweaks to allow property getters to be optionally const, but I'm still thinking about how I want that to work.
0

Share this post


Link to post
Share on other sites
I like it, and would gladly incorporate it into the script library.

I especially like the fact that you only modified the parser and builder, which I interprets as the fact that you're translating the above syntax to the more explicit syntax that I currently use in AngelScript. This is good because it maintains backwards compatibility and also allows the script writer to fallback on the more explicit syntax when they want to write something that is goes against these automated code generations (for example a get accessor that returns a handle, but a set accessor that receives a reference.
0

Share this post


Link to post
Share on other sites
thumbs up to "InvalidPointer" and "WitchLord" for incorporating this into angelscript. This brings angelscript a step closer to powerful languages.

my theme for embedded language is to have all the object oriented, functional and other-developer friendly "features of the core language itself", BUT without the baggage of a large runtime library.
properties are very important part of that. [img]http://public.gamedev.net/public/style_emoticons/default/rolleyes.gif[/img]

0

Share this post


Link to post
Share on other sites
[quote name='Andreas Jonsson' timestamp='1319308909' post='4875392']
I like it, and would gladly incorporate it into the script library.

I especially like the fact that you only modified the parser and builder, which I interprets as the fact that you're translating the above syntax to the more explicit syntax that I currently use in AngelScript. This is good because it maintains backwards compatibility and also allows the script writer to fallback on the more explicit syntax when they want to write something that is goes against these automated code generations (for example a get accessor that returns a handle, but a set accessor that receives a reference.
[/quote]
Exactly. The older, 'first-generation' approach created a separate object aspect category, imaginatively titled 'virtual properties' :) which sort of worked as an almagation of 'real' properties and actual methods. You could attach getter/setter functions arbitrarily and had a slightly more natural approach to app-side reflection at the cost of pretty extensive internal rewrites and a lot more code duplication than I think was healthy. This does seem like a good approach in the long run (and could actually be made using the infrastructure I did keep; it was actually lifted more or less wholesale) but ultimately I wasn't comfortable making large refactorings without a *very* thorough understanding of why the existing API is structured the way it is. One more thing to add to the roadmap? :)

I will also mention I made a few changes to how the naming scheme in particular works internally (with some justification, bear with me) along with this-- while both work entirely independently and you're free to swap back to the old style in the official version, it seems to me that prefixing accessors with 'get_' and 'set_' in the new style could be *extremely* confusing when script writers are attempting to resolve syntax errors-- the signature will obviously look a lot like that of a 'real' function which may be confusing when the actual code will look really different. While arguments could be made that things like line and column numbers can be used as a failsafe, I think this actually gives IDEs and occasionally script writers too much credit! :P As a result, getters are now named "<propertyname> {getter}" and setters named "<propertyname> {setter}" respectively. This style has a number of advantages-- no coding style interactions, the declarations themselves actually look a lot more like the code they refer to, and lastly 'break' the parser in such a way so as to render them uncallable via traditional script syntax-- you *must* use the property syntax as opposed to freely mixing
[code]set_AGlobalProperty(50)[/code] and [code]AGlobalProperty = 50[/code] from the example in the OP. The main downside to this is that using app-side reflection to look up functions by declaration/register application-defined system functions becomes slightly more complex-- I'm adding some special-case methods for this since it seems the user probably has all the required contextual information anyway. Simply iterating over all functions requires no changes and works out-of-the-box.

And, as the final touch, do we like [code]int ConstTestProperty
{
get const
{
return 5;
}
}[/code] or do we prefer [code]int AlternateConstTestProperty
{
const get
{
return 6;
}
}[/code]
I personally much prefer #1 as it feels a lot more like the 'void Foo::Bar() const' you'd see in normal const member functions and is probably easier to implement, but whatever floats everyone's boat.

EDIT: Also, does it make sense for setters to be declared const? I mean, I can think of cases where you might just be modifying a global property (this could be a good way to do static properties, btw) without changing the state of the actual object itself, but I'm weighing if this is considered poor enough form for it to be killed at a syntactic rather than stylistic level.
0

Share this post


Link to post
Share on other sites
Seems to me as if the const keyword should be valid in both circumstances.

[code]


int Property
{
// const int get_Property() const;
const get const
{
return m_iProperty;
}
// int set_Property();
set
{
m_iProperty = value;
}
}
[/code]

Also, shouldn't the setter return the value being set so that calls can be chained?
0

Share this post


Link to post
Share on other sites
[quote name='ekimr' timestamp='1319546639' post='4876719']
Seems to me as if the const keyword should be valid in both circumstances.

[code]


int Property
{
// const int get_Property() const;
const get const
{
return m_iProperty;
}
// int set_Property();
set
{
m_iProperty = value;
}
}
[/code]

Also, shouldn't the setter return the value being set so that calls can be chained?
[/quote]
Nope, that would mean circumventing the actual getter outright. As it stands, chaining works for the examples above, though it's more of a two-step process. The setter is first invoked, and then the getter is called which will mutate the new value however it sees fit-- that's what will come out 'on the other side' of the chain. In this regard, I follow the original getter/setter conventions exactly. In fact, I don't change that part of the code at all! :)

Re: return type mixing-- I'm not sure that's a good idea to swap types around like that. You can decorate the actual emulated type however you see fit-- things like [code]const Object@ AConstProperty
{
get const
{
return constPropertyBacking;
}
set
{
constPropertyBacking = value;
}
}[/code] will work just fine. I will take that to mean the const 'postfix' style is what we prefer, though, so that's how I'll implement it.

Also, made a few changes to the built-in array type so that it now uses virtual properties. You can use the more mundane [code]for( uint32 i = 0; i < someArray.length; ++i )[/code] to retrieve the array length, but it gets really interesting when you change this value-- the array will actually now resize itself to the specified length!

EDIT: const behavior implemented, just developing a method for specifying this for app-defined classes. Other than that, I think we're in business and patches shall be forthcoming.
0

Share this post


Link to post
Share on other sites
I look forward to the patches. I hope to be able to release version 2.22.0 by the end of next week, so I'll probably not include this until then, but perhaps for version 2.22.1 (unless it requires changes to the API, because then it would have to wait until 2.23.0).
0

Share this post


Link to post
Share on other sites
[quote name='Andreas Jonsson' timestamp='1319596355' post='4877018']
I look forward to the patches. I hope to be able to release version 2.22.0 by the end of next week, so I'll probably not include this until then, but perhaps for version 2.22.1 (unless it requires changes to the API, because then it would have to wait until 2.23.0).
[/quote]

In that case, I'll actually hold off for a bit and bundle this with the final/override keyword feature set, since this would require some changes for consistency anyway. I can probably put together some documenation as well.

EDIT:
Changes are tested, and a patch file for revision 1015 created and sent.
0

Share this post


Link to post
Share on other sites
I've checked in the contribution now in revision 1043.

I've a few changes but it works mostly the same way that you implemented it. Notable changes:

- The keywords final, override, get, and set are not reserved, as I do not want to prevent anyone from using them in variable and function names
- I decided not to go with the naming convention 'property {getter}' and 'property {setter}'. So the current way of implementing property accessors still work. With this I also do not have to provide all the new interface methods that you implemented (RegisterVirtualPropertyAccessor, etc).

Your main argument to provide the different naming convention can likely be fixed by providing more informative error messages instead. The manual will also explicitly state what is done behind the scenes when declaring a virtual property using the new syntax.

I'll probably continue to enhance this with automatic implementation for the getter and setter, as well as automatic declaration of the real property, if it is used in the get/set implementation.

An improvement to the final/override keywords is the implementation of the keyword abstract as well.


Thanks a lot for the contribution.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0