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.

Newcomers AppGameKit Corner / More AGK2 ++ Questions

Author
Message
Phollyer
14
Years of Service
User Offline
Joined: 20th Sep 2009
Location: Houston TX
Posted: 20th May 2017 18:40

I've deduced that we don't have String or std::string instead everything uses char*

so how do we concatenate these char* or convert a std::string into a char*?

I often use Log(" Value of MY ==>"+str(MY)+"----------------------------------------------") which easily makes these entries stand out

is there some way to do this ion c++?

in AGK1 I use a lot of constants like these

#Constant ColorBlue 0,0,255
#Constant ColorYellow 255,255,0
#Constant ColorOrange 255,165,0

then use the color constant in ant color setting...

is there a way to do this in AGK2?
Lucas Tiridath
AGK Developer
15
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 20th May 2017 21:33
Phollyer wrote: "so how do we concatenate these char* or convert a std::string into a char*?"

String handling in C++ is quite different from AppGameKit BASIC, and there are quite a number of ways of doing this. If you're just getting started with C++ though, I would consider using stringstreams, as they're comparatively easy to get to grips with and less error prone than other options.

Phollyer wrote: "I often use Log(" Value of MY ==>"+str(MY)+"----------------------------------------------") which easily makes these entries stand out

is there some way to do this ion c++?"

In this specific example, there are actually two things you need to translate. The first problem you'll have is that AGK's Log function doesn't send its log messages to the Visual Studio output, so probably isn't very useful to you. Instead, if you're developing on Windows in Visual Studio, you'll probably want to use OutputDebugString instead. Note that this will only work on Windows, so if you're targeting other platforms too, consider wrapping it in #ifdef _WIN32 ... #endif processor directives.

To put these two together into a working example, one way to translate your logging example into C++ would be something like this.



One thing it's important to bear in mind when working in AppGameKit Tier 2 is that when AppGameKit functions return a char * to you, you are responsible for freeing the memory, so it's very rarely going to be safe to use the result inline. Instead, you'll need to capture the pointer, use it wherever, and then delete it afterwards. Of course if it's just for debugging purposes, you may not care about memory leaks, but it's a good habit to get into.

Phollyer wrote: "in AGK1 I use a lot of constants like these

#Constant ColorBlue 0,0,255
#Constant ColorYellow 255,255,0
#Constant ColorOrange 255,165,0

then use the color constant in ant color setting...

is there a way to do this in AGK2?"

This one's easy in C++ (I'm assuming you mean Tier 2 here rather than AppGameKit 2). AppGameKit BASIC's #Constant is pretty much identical to C++'s #define, so just change the #Constant for #define and it should work.

Hope that helps.
Phollyer
14
Years of Service
User Offline
Joined: 20th Sep 2009
Location: Houston TX
Posted: 21st May 2017 02:16
Wow!
Thanks for the detailed response!
I am new to c++, not visual studio with numerous years of c#

I've deduced that global variables belong in the template.h file, the template.app is not app wide
I do like to breakup code into numerous smaller files

Pete
Lucas Tiridath
AGK Developer
15
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 21st May 2017 07:54
Quote: "I am new to c++, not visual studio with numerous years of c#"

Cool. I'm just the opposite! I've done quite a lot of C++ but almost no C#! Anyhow if you need any more help transitioning to C++, just let us know

Quote: "I've deduced that global variables belong in the template.h file, the template.app is not app wide"

Kind of. Header files are commonly used to declare things like classes, global variables and so on, but you also need to define them somewhere. #including a header file literally just copy-pastes that file into where the #include is. This means that if you actually define your global variables in the header, and then include the header in multiple cpp files, you'll get errors because you'll effectively be declaring the variable multiple times. If you want a global variable, you probably want to put something like this in the header



and then something like this in the cpp file.

Login to post a reply

Server time is: 2024-04-23 21:54:13
Your offset time is: 2024-04-23 21:54:13