Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Vectors in T2

Author
Message
The Zoq2
15
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 21st Jun 2013 16:46
Hi guys, ever since I started learning about vectors in C++ I have tried to use them for my agk projects, but I have eventually reached a point where most of the vector functions crash the program when called. After doing some research, it seems that most functions using iterators return an assertion error in debug mode. I am also experiencing diffirent problems in diffirent projects (in my test project vectors mostly work fine in the app::Loop function but not in external functions, that is not the case in my main project)

In order to track down what the problem may be I decided to do some experimetns.

First of all, define a vector and call the .clear() function in the main loop. (clear is the function that I want to use right now).



This works fine and I get no crash.

Placing the defenition in the template.h file however, causes a debug assertion failed on the first "vec.clear()" function.

The code works with a struct instead of an int defined in the .h file but the vector defined in the cpp file.

I then went on to do the same tests in another class and got the same resutls, defining the vector in the .cpp file works while defining the vector in the .h file throws an error.

It also seems like most commands dealing with elements in the vector (front, back, erase) have similar issues.

Is this intended, am I doing something wrong or is this a bug in AppGameKit?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 21st Jun 2013 17:23 Edited at: 21st Jun 2013 17:25
This is not an AppGameKit issue. It is probably an issue with how you are using it.

I am using vectors and lists all over the place in Tier 2.

Quote: "Placing the defenition in the template.h file however, causes a debug assertion failed on the first "vec.clear()" function."

Can you show what you are putting in the header file?

Unless the "std::vector<int> vec;" is inside a class in a header file (and therefore not executed until the class is instantiated), using it like that in a header file is a mistake.

If you want to create a global object that can be referenced in another file, you do it like this:
global.h (note the use of the keyword 'extern')


global.cpp (note that the keyword 'extern' is NOT used here)


The "#ifdef" and "#endif" in the header file are to make sure the contents only get included once by any .cpp file. It is not impossible that you end up with a lot of .cpp files that reference lots of others through the headers. So, we use the "#ifdef" and "#endif" to make sure we don't get multiple definitions of things. This is more important with respect to classes than simple examples like the one above.

Now, you can include globals.h in any .cpp file and it has access to the vec list. And they will all be accessing the same list, not separate copies of it.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
The Zoq2
15
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 21st Jun 2013 18:09
My template.c++ file




And my template.h file



And this is how I use most of my variables. Apart from my global ones which I have defined as static within a Global class in a .h file, initialised in a .cpp file, and then used with Global::Variable after I have included the files.
XanthorXIII
AGK Gold Backer
13
Years of Service
User Offline
Joined: 13th May 2011
Location:
Posted: 21st Jun 2013 18:34
Why are you nesting a class and struct in class app. Just a question that is all. Trying to get a feel for your program.

Right now I'm working on some stuff that I hope can get the T2 Community out of the Global Variable mindset that the T1 Group has to deal with.
The Zoq2
15
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 21st Jun 2013 18:39
Oh, this is just my test project and I created the struct and class to test the vector issue.
XanthorXIII
AGK Gold Backer
13
Years of Service
User Offline
Joined: 13th May 2011
Location:
Posted: 21st Jun 2013 19:14
I wouldn't place any variables in Template.h at all. Use Template.cpp for that.
Follow what AL has said about the use of globals.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 21st Jun 2013 19:53 Edited at: 21st Jun 2013 19:57
Okay, using 'std::vector<int> vec;' inside of app::Loop() isn't the way to go.

This causes a new one to be created every time app::Loop() is called.

Only ever declare a variable in app::Loop() if it is something that is simple, like 'integer i' when 'i' is used in a 'for' loop.

Uncomment the declaration in temple.h and remove the declaration in your app::Loop().

And, assuming that 'Class.h' is something you created with a class named 'Class'. Bad idea. It is not recommended to use reserved words (even if you change the letter cases) for class names. You never know if something under the hood has already done that and it can cause issues. And you don't know if somewhere someone has done something like "#define Class class", which would really mess you up. (EDIT: and I have seen this done in some systems so that definitions look more elegant.)

You can declare an entire class within another class definition, but you need to at least do it like "class something {};" so that it is a complete class.

As for your usage of vec in app::Loop(), 'vec.push_back()' will cause a problem because you are not passing in an integer for it to push back. So this method requires something to add to copy and create a new entry in the vector.

The 'back()' vector method returns a reference to the last item. If the vector is empty, there is nothing to reference and trying to assign a value to it won't work. The method does NOT add something to the end of the vector. The push_back method is what does. So, trying to overwrite the last entry using back() immediately after you have cleared the vector will definitely fail.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 21st Jun 2013 19:56
XanthorXIII, I agree that using globals should be kept to a minimum (or not at all). But there are definitely cases where it is the simplest way to handle a simple thing.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
The Zoq2
15
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 21st Jun 2013 20:39
Quote: "Okay, using 'std::vector<int> vec;' inside of app::Loop() isn't the way to go.

This causes a new one to be created every time app::Loop() is called.

Only ever declare a variable in app::Loop() if it is something that is simple, like 'integer i' when 'i' is used in a 'for' loop.

Uncomment the declaration in temple.h and remove the declaration in your app::Loop()."


I am aware of that, however what I found out is that if I declare vectors in the .h file, the .clear() function returns an error which is what I want to solve.

Quote: "And, assuming that 'Class.h' is something you created with a class named 'Class'"


I am well aware of that aswell, however since this was a test, I didn't care.

Quote: "As for your usage of vec in app::Loop(), 'vec.push_back()' will cause a problem because you are not passing in an integer for it to push back. So this method requires something to add to copy and create a new entry in the vector.

The 'back()' vector method returns a reference to the last item. If the vector is empty, there is nothing to reference and trying to assign a value to it won't work. The method does NOT add something to the end of the vector. The push_back method is what does. So, trying to overwrite the last entry using back() immediately after you have cleared the vector will definitely fail."


Both of those lines are uncommented.

So, after doing some research like I said before, defining the vector in any .h file causes a crash when I use .clear or similar functions, declaring it in the .cpp file works.

I have found a somewhat temporary solution to the problem however. Defining the vector as a pointer in the .h file and then giving it a value in the code works.

something like this in the .h file

and this in a function inside the .cpp file


However, in this process I have found another interesting issue. My constructors don't seem to be called propperly. The debugger says that the constructor is run and the values are changed within it, but when i check the values of those variables outside the constructor, they are null.

More examples
The Class class again, acording to the visual studio debugger, the value of G gets set but the value iss still null/0 in the update function. (I create an object of Class in the template.h file) Is this to be expected or is something wrong?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 21st Jun 2013 21:05
I did see that you had those lines commented out. I assumed that you at one point had them uncommented and that was when you were experiencing the issues.

Your Class class uses vec, but it wouldn't have access to the vec defined in the app class without some reference to it (like maybe passing it a pointer to vec when app is instantiated in the app::Begin() method). Creating an instance of class B inside of class A does NOT give class B transparent access to members of class A.

I define vectors and lists inside of classes in my .h files all of the time.

Here is one of the classes that I use vectors in. The first file sets up the types I use all over in my WIP.
ta_types.h:


index_hash.h (since I can't use std::map, I make a pseudo-hash-like class to handle indices into other lists or vectors):


index_hash.cpp


Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
The Zoq2
15
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 21st Jun 2013 22:01
Quote: "Your Class class uses vec"


I guess that I should have been more clear about that, in "Class" (I realy should rename that ) I have another vector which I also call vec. Both of them have the same issues and I still am not able to define a vector within a .h file. Like I said, I can solve it by using vector pointers however, the constructor does not seem to run propperly.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 21st Jun 2013 23:01
This sounds like a case where I would have to see the entire project to figure out why you are having such difficulty with something that should work easily.

But, I do not really have the time to go through entire projects (other than my own nearly done WIP).

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
The Zoq2
15
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 21st Jun 2013 23:15
Understandable, however I am experiencing this issue with any AppGameKit project. Basicaly I just copied the template provided with 10813, fixed the directories and added the code I have been talking about before.

I guess I can solve the issues myself without constructors, thank you for the help
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 21st Jun 2013 23:24
Are you able to get the vanilla template (without any changes) to compile and run?

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
The Zoq2
15
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 21st Jun 2013 23:41
Yes, I just had to change the directories to point to my agk location and change the "Enable incremental linking" to "No (/INCREMENTAL:NO)" for the debug configuration. I believe I won't have to do that either since I installed VS2010 servicepack 1
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 22nd Jun 2013 00:16
If you still haven't gotten it working properly by the end of the weekend, I'll look at your project.

I can't stand to see someone stuck on something that should be so easy.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
The Zoq2
15
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 22nd Jun 2013 00:19
Thank you, hopefully I will be able to sort it out before then
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 22nd Jun 2013 13:27
Strange, i use vector since the begin of agk in a complete different way...
I use them for my internal manager.
But the Lady way is good too.

Long life to Steve!
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 22nd Jun 2013 18:27
xGEKKOx, my posted code is just one of about 8 or 9 classes that use vector or list. And most do it in different ways, clearly for different purposes.

After the troubles with UDTs in arrays in Tier 1, being able to use vector/list with structs or classes is wonderful!

Especially when you use derived classes. The basic object manager in the actual game play keeps a list of game_obj_base things. But every entry is actually something derived from game_obj_base. It works pretty cool.

After doing basically nothing but web sites for clients for the past 9 years, getting back into very real coding is fun. (Yes, with Javascript and PHP, I was doing real programming, but web sites just aren't any fun.)

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 22nd Jun 2013 19:53
I wanted to mean that i don't use class.
I didn't said taht your code is not good, indeed, i appreciated it.
In fact some times i think that i code like a child. Dunno why....
If you look at my games codes, you will see they are very little.

Don't know why, is since the time of c64, maybe i'm used to do....
So you written the code for Tier 1? (so i didn't understand).

Ohhhh yes, WEB have no fun!!!!! I remember that time, bad memories!!!


Long life to Steve!
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 22nd Jun 2013 20:57
I wasn't taking any offense. I was just indicating that I used vector/list in many more ways than the one shown here.

I try to not criticize anyone's coding styles/choices because there are so many ways to do the same thing and a lot of it really does come down to personal preference.

And I try never to take offense if someone criticizes mine (deliberately or otherwise). Just don't tell me flat out that I am wrong. As long as code works as expected, whatever a programmer does is not wrong.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 23rd Jun 2013 11:44
Yes, finally a coder wanna reach the objective, if possible, with less lines of code.
No matter how, just it work.

No no, i understood you didn't wanna offense... is my horrible english

Long life to Steve!
basjak
14
Years of Service
User Offline
Joined: 16th Apr 2010
Location: feel like signing up for mars
Posted: 23rd Jun 2013 15:09
@ ancient lady:

I don't believe you're offending anyone. am thrilled with your experience and actually you're the most active person in the forum.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd Jun 2013 18:44
basjak, sadly it means that I don't have enough active clients and that leaves me time to read and write.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master

Login to post a reply

Server time is: 2024-11-24 15:42:57
Your offset time is: 2024-11-24 15:42:57