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 / [tier2] how do I concatenate a char + int into OpenToRead file name?

Author
Message
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 13th Jan 2013 18:35 Edited at: 13th Jan 2013 18:44
Hello,

I'm working in tier2 and doing a function to load levels.

Ideally if you were on level 1 it should load level1.dat, if you were on level 2 it should load level2.dat when you call it and so forth.

So my code at the moment



So obviously I want to replace "level1.dat" with a variable that is proper level to load.

I can not figure out how to concatenate

"level" + levelNumber + ".dat" into "level1.dat"

How would I do that?

Thanks in advance

PS: I would prefer a solution that does not involve boost libraries as I don't know how to integrate them into XCode

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 13th Jan 2013 18:58 Edited at: 13th Jan 2013 19:33
In C:

mystring = format("level%d.dat",level);

In Pascal:

mystring := format('level%d.dat',[level]);

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 13th Jan 2013 19:04
Jim I think there is a bracket error somewhere in the pascal version
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 13th Jan 2013 19:04
Well, I don't use tier 2, but in tier 1 it would be something like this.

I can't see it being much different in tier 2. There must be similar commands available. I altered your first line in my example to increment level by one each time and so move through the levels, each time you run it.

erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 13th Jan 2013 19:26
Those examples don't seem to work for me.


Jim's example like this



Produces this error from Visual Studio 2010:

Quote: "1>c:\agk\projects\carls\sidescroll\editor.cpp(147): error C2371: 'tempLevelToload' : redefinition; different basic types
1> c:\agk\projects\carls\sidescroll\editor.cpp(145) : see declaration of 'tempLevelToload'"




DVader's example I do not know how to implement in Visual Studio C++ ; Visual studio is tricky with strings and doesn't let me do a str(levelnumber) command; or if it does I do not know it. This is part of my problem how to cast an int as a character value with both the constraints of VS 2010 + AppGameKit .. and then concatenate this into a single string.

I appreciate the suggestions but these don't seem to be working out, any other ideas?

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 13th Jan 2013 19:34 Edited at: 13th Jan 2013 19:35
@bj - thanks - edited!

@erebusman - you've left the format() command out, so it won't work. That is standard C and does not use Boost.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 13th Jan 2013 20:21
@Jim:

Thanks .. can't believe I missed that.

But then I get a different error.



Produces error from VS 2010


Quote: "1>c:\agk\projects\carls\sidescroll\editor.cpp(149): error C3861: 'format': identifier not found"


I tried doing std::format and it said format is not a member of std

So I googled the use of format and found this page http://www.cplusplus.com/reference/regex/match_results/format/ which looks very different than the use you describe .. I do not understand this page right now but I am trying to just 'dumb' through it and see if I can randomly get something to work.

Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 13th Jan 2013 20:40
I'd just use a uString (AGK's native String class). It's operator overloaded to append ints and floats to the original string. Small example:



DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 13th Jan 2013 20:44 Edited at: 13th Jan 2013 20:45
Well, as I have not tried tier2 I couldn't say if it is possible to use the same method. But I would imagine it should be.
This is what I have found in the agk help files regarding the str command using tier2.


erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 13th Jan 2013 20:55
@Hodgey thanks that worked; I had not seen uString in the docs at all , is that undocumented or I don't know where to look?


This code ended up working properly:



Thank you very much!

Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 13th Jan 2013 21:12
Quote: "I had not seen uString in the docs at all , is that undocumented or I don't know where to look?"

It is undocumented AFAIK but you can go snooping through all of the AppGameKit header files. There's a file called uString.h. I briefly talked to Paul about it here.

As it's undocumented, I think someone should add it to the community wiki.

erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 13th Jan 2013 21:56
Quote: "As it's undocumented, I think someone should add it to the community wiki. "
[quote]

You read my mind I'll add it this afternoon after I reached my programming goal for the day.

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 13th Jan 2013 22:51
You probably need to #include stdio.h

There are very many different ways of doing this in a variety of languages.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 14th Jan 2013 07:15
@Hodgey: wiki updated

@Jim: Oh I don't doubt it; I have only been doing C++ for about 5-6 months and I'm amazed at how many ways there are to do things. That ends up being one my problems sometimes ; most particularly using VS2010 (Microsoft = proprietary much?) backs me in to a few corners when I read generic C++ docs and then it doesn't work out in VS2010.

Furthermore my primary targets are 1: iOS and 2: Windows so VS2010 works okay for Windows but if I do something crazy in VS2010 Xcode may not like it ; therefore any solution has to be transferable to Xcode for me.

With my current low level of experience external / non native C++ solutions are extremely painful (think 2 days of painful learning per item for instance)

Not saying the solution you provided can't work; I just lacked the expertise to get it to go myself

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 14th Jan 2013 16:25
For the record, I use sprintf to create complex strings:


A very important thing to keep in mind when using any AppGameKit function that returns a char* value is that you are responsible for clearing the memory allocated by the command. Once you are done with the char* variable that you store the return in, you need to delete it with "delete[] varnam;". If you don't clean up your strings, you will have memory leak problems. The std::string object makes copies of char* things passed, added, whatever, but does NOT clean up the memory from the original char* function. So you still need to clean up those values.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 16th Jan 2013 06:38
Thanks for the memory tips Ancient Lady...

I don't know *WHEN* to delete my char* value ?

For instance .. I created a char value in the top of my template file so its global like so



Then at 4 different functions in my program I either read what is in it, or place something else in it.

I do not re-create (that I'm aware of) as the program goes on? Unless placing a value inside it during a function constitutes recreating it?

I've only been doing C++ for about 5 months so I'm probably making invalid assumptions about some of this.

Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 16th Jan 2013 08:48 Edited at: 16th Jan 2013 08:48
Quote: "I don't know *WHEN* to delete my char* value ?"

I'll try to wrap this up in a simple yet informative explanation. As you know, variables are stored in RAM. In C++ I believe there a 4 distinct areas of memory; the first two being the code (including constants - values known at compile time), the second for static/globals. The third and fourth, stack and heap, are what we're interested in. When you create a variable such as:

{
int j;
}

The variable j is placed on the stack and when the variable goes out of scope (the program goes past the } or hits a return ) it gets popped off the stack. You don't need to worry about freeing variables on the stack as the program will do that for you.

When you dynamically allocate memory via new, malloc, etc, this memory is allocated from the heap and you are responsible for it. If you use new or malloc, you'll have to free the memory once you've finished with it (regardless of scope) via delete or free.

A small picture to help you visualise this:


In Tier 2, when an AppGameKit function returns a char*, it has already used the dynamic memory allocation functions (heap based) and so it's up to us to free the memory. Paul has confirmed that any char* returned by AppGameKit must be freed via the delete[] keyword (square brackets because it's an array or characters). There's an example on the wiki.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 16th Jan 2013 16:12
erebusman, Hodgey has the basics just right.

char* variables inside of functions assigned values (that aren't constants) using anything other than a constant (similar to your example) should be deleted before the function exists, just to be safe.

This is especially true if you reuse the variable within the function. Make sure to do delete[] on it before you assign another value.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 17th Jan 2013 06:43
when I do the following I get a horrible crash



I tried putting the square brackets or not putting the square brackets.. same thing.

When I dont use the delete command the program runs fine.

The text of the crash:


So obviously I'm doing it wrong? Haha, any tips? I thought it seemed quite simple from the examples above, but apparently not so much?

Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 17th Jan 2013 08:37 Edited at: 17th Jan 2013 08:37
Quote: "when I do the following I get a horrible crash"

This is because szText is on the stack not the heap. The only way for something to be on the heap is if you use the new keyword.

A code snippet to help tell the difference.


Quote: "@Hodgey: wiki updated"

Cool! Just had a look and it looks like the comment markers haven't made it through. Also, I'm afraid I'm no longer an AppGameKit CT, I had to step down when I became too busy. They are doing a fantastic job though.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 17th Jan 2013 17:23 Edited at: 17th Jan 2013 17:24
erebusman, change this:


to this:


Since you are declaring the variable as an array, you do not need to delete it afterwards. You only need to clean up char* variables, not char array variables.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 18th Jan 2013 05:24
@Ancient Lady : awesome thanks!

@ Hodgey: Well I've not found a use for the new thing yet; because as I understand it you would use that when you don't know in advance how big you want something to be so you can create it at run time.

In this case I do know how big I want it to be..

But I appreciate the example that does help! Thank you.

Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 18th Jan 2013 06:44
Quote: " as I understand it you would use that when you don't know in advance how big you want something to be "

That's a good way of putting it.

Quote: "In this case I do know how big I want it to be.."

I know. But AppGameKit does uses the new keyword when ever it returns a char*. You can imagine that it the function does something like:


Which is why you need to delete[] every char* the functions return, that's all.

Login to post a reply

Server time is: 2024-05-03 21:28:52
Your offset time is: 2024-05-03 21:28:52