Advertisement

Giver class member variables start values.

Started by May 15, 2012 09:53 AM
4 comments, last by WitchLord 12 years, 6 months ago
I am wondering if there is anyway to do something like:

class cMyClass {
int mlCount=0;
}


or if that is not possible, some way at least let all the basic types (float, int, etc) be 0 from start?

The only way I can think of now is to iterate all of the member vars upon creating a class object and then zeroing them. Is there any simpler way to achieve this or even better to do something like the sample code above.

When scripting, it is often very convenient to declare member variables near the method where they are used, and thus it can be cumbersome to always go to the construct to init them. For instance:

class cMyClass {
....
int mlNumOfSlugAttacks=0;
SlugAttacksPlayer()
{
mlNumOfSlugAttacks++;
if(mlNumOfSlugAttacks>3) DoSomething();
}
...
}


Any ideas on ways to achieve this would be very helpful! Perhaps I am missing some feature that is already in the language?

or if that is not possible, some way at least let all the basic types (float, int, etc) be 0 from start?


I think it really helpful!


class MyClass {
int Count=0;
}


it is really awesome! :)

subscribe!
Advertisement
Hello, as far as I know, you cannot declare and initialize class member variable. You have to initialize it in constructor. It doesn't work like in C# here :).

class cMyClass {
int mlCount = 0;
}



This syntax for providing the initial values of members has been on my to-do list for quite some time. It would be a very convenient way of doing the initialization.

It is however not an essential feature as the initialization can be done the traditional way with constructors, and thus not something I've prioritized over the time. Should someone like to implement the feature for me I'd gladly add it to the library though.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game


Should someone like to implement the feature for me I'd gladly add it to the library though.


Oh, you so sneaky ;)

Do you mean implement in a something like script_builder? Or directly in the lib? If in script_builder I could give that a go, but not sure how to go about., so any pointers might persuade me to do it :)

Oh, you so sneaky ;)


I do my best :)




Actually, this would have to be part of the core library itself as it would be such an integral part of the language.

I'll try to list the things that need changes, though it will most likely not be trivial task to get this to work.

- asCParser::ParseClass(). This method needs to change the way it parses the members so the initialization can be informed. I would probably change it to call the ParseDeclaration() or ParseGlobalVar(), as the syntax is identical.

- asCBuilder::CompileFunctions(). This method needs to be changed so that the compiler receives the initialization statements for the class members when compiling the class constructors.

- asCCompiler::CompileDefaultConstructor(). This method would probably have to be completely rewritten to initialize the members according to the expressions given in the declaration.

- asCCompiler::CompileFunction(). This method will have to be modified to handle the compilation of manually defined constructors, where part of the initialization is informed in the member declaration and part of it in the constructor itself. This will likely be the most complicated part to get right. If a member is initialized both in the declaration and the constructor, then the constructor should possibly ignored the initialization from the declaration, or otherwise first do the initialization from the declaration and then do an assignment with the value from the constructor.


If you decide to take up the task I suggest you take it step by step. First make the parser understand the declarations, but then have the builder ignore it so it works either way. Then as the second step make the CompileDefaultConstructor() work by initializing each member correctly. Only when this is working should you work on the CompileFunction().

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement