Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/06/23 11:29
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
7 registered members (fairtrader, Quad, miwok, Martin_HH, AndrewAMD, alibaba, dpn), 581 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
string = another string/name #405576
08/02/12 14:41
08/02/12 14:41
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
Hi,

I wanted to use
Code:
str_replace(STRING* str, char* original, char* replacement)

for changing an empty string (STRING* emptystring_str = "";).

But this doesn't work with me because as soon as I start the map I get an error saying that 'str_replace' is an "undeclared indentifier". Is str_replace outated than or only for C-sript (and not Lite-C?)?

Than I tried:

Code:
...
emptystring_str = "TEST";
...



and I also tried:

Code:
...
STRING* emptystring_str = "TEST";
...



but that did not change emptystring_str (it was still empty).

Any ideas? Thanks in advance

Re: string = another string/name [Re: Reconnoiter] #405577
08/02/12 14:46
08/02/12 14:46
Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Rei_Ayanami Offline
Expert
Rei_Ayanami  Offline
Expert

Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Include <strio.c> wink

Also, if your string is not global, you are initializing it wrong wink

Re: string = another string/name [Re: Rei_Ayanami] #405582
08/02/12 16:54
08/02/12 16:54
Joined: Jul 2002
Posts: 3,208
Germany
Error014 Offline
Expert
Error014  Offline
Expert

Joined: Jul 2002
Posts: 3,208
Germany
To elaborate on this, str_replace is a helper function that's defined in strio.c. Unless you include that file (by writing "#include <strio.c>" someplace before you need it), you cannot use it.

If you define a string like
Code:
STRING* emptystring_str;


then what you are actually defining is a pointer (as the asterisk shows you). This means that there's actually not really a string anywhere, you just told the engine that you need a place to store a reference to one. So you'd have to create one. For that, we have str_create:

Code:
STRING* emptystring_str; //Now, a pointer exists
emptystring_str = str_create("TEST");



str_create returns a pointer, to which you can set emptystring_str.

If your string is global (meaning you define it outside a function), then yes, you could write
Code:
STRING* emptystring_str = "TEST";


But really, that's just the lite-c compiler being overly generous and doing the above for you. It's still insanely comfortable. I also use it, but I have the good sense to be ashamed. wink


Perhaps this post will get me points for originality at least.

Check out Dungeon Deities! It's amazing and will make you happy, successful and almost certainly more attractive! It might be true!
Re: string = another string/name [Re: Rei_Ayanami] #405584
08/02/12 17:14
08/02/12 17:14
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
-EDIT EDIT i just tried the nametouchedunit_str = str_create("TEST"); but that did not work for me, please see below:

Ah well that explains the undeclared indentifier, thanks!

I want to have the Include list as short as possible cause I have alot of other include files already (to keep it clear). So what function/line(s) can I use to change the string? The string is global btw. The string shows the name of an enemy close to the mousepointer (it is activated by MOUSE_TOUCH event and deactivated by the MOUSE_RELEASE). Here is the code (at first glance it looks more than it is wink ):

Code:
// the line below is the init (or prototype or whatever it is called) of the string
STRING* emptystring_str = "";
...
...

// init/prototype of the text
TEXT* touchedunit_txt =
{
	font = arial15B_font;
	string("Name:", emptystring_str);
	flags = OUTLINE;
}

...
...

// hit event of enemy unit
function enemy_hit()
{
...
...
if (event_type == EVENT_TOUCH) // the unit was touched with the mouse?
  {
    emptystring_str = blabla enemy name;
    info_touchedunitON();
  }
  else if (event_type == EVENT_RELEASE) // the mouse was moved away from the unit?
  {
    info_touchedunitOFF();
  }
...
}

...
...

// show and position the text
function info_touchedunitON()
{
set(touchedunit_txt,SHOW);	
	while(1)
	{touchedunit_txt.pos_x = mouse_pos.x + 50;
	touchedunit_txt.pos_y = mouse_pos.y + 50;
	if (blblablabla == blablblabla) break;
	wait(1);}
}

//reset the text	
function info_touchedunitOFF()
{
reset(touchedunit_txt,SHOW);
}





Last edited by Reconnoiter; 08/02/12 17:26.
Re: string = another string/name [Re: Reconnoiter] #405635
08/03/12 23:45
08/03/12 23:45
Joined: Jul 2002
Posts: 3,208
Germany
Error014 Offline
Expert
Error014  Offline
Expert

Joined: Jul 2002
Posts: 3,208
Germany
I see your concern about not wanting to include <strio.c>, though in this particular case, you'd save yourself some work by allowing that one.
If you prefer, you can also just copy str_replaceall from that file into your script (and everything that particular one needs - I haven't looked at it).

If you'd rather do that yourself, then you should look at the str_cpy, str_cat, str_clip, str_trunc and str_stri - instructions. Or, if you'd rather go on a "per-character" level, check out str_setchr and str_getchr (and note that str_setchr requires a var - if you want to use a char, you have to cast it to int first).


~ ~ ~

Okay, let's look at your code, starting with this line:

Code:
STRING* emptystring_str = "";



First, a note - as I've said, this is lite-c only. You normally wouldn't be able to initialize that this way. But that's really just a note - it's okay here.

But let's think about that: You've created a string-pointer (thats really just what you'd expect after writing STRING*), and you've asked lite-c to create a new string object for you, that holds, in your case, a maximum of... zero characters.
Alright, so what happens? If you create this stuff, your computer needs to get some area of memory for it. It then "marks" that area as used, and the STRING* points to that (meaning: It holds now the adress of that particular memory block).
As a side note here, it used to be so that you couldn't exceed the length of the original string (in fact, only by checking the manual just now did I learn that this limit no longer exists. laugh Learn something new everyday!)

Okay, so let's summarize this. emptystring_str is a pointer. It is not the object itself. Think of it as the adress in memory (which is really just a number). Cool beans.

Actually, while I'm rather confident as to what the error is in your code, I'd be lying if I said that I'm 100% sure in every detail in the explanation below. After all, I just learnt that strings behave differently from what I'm used to (I only "recently" switched to A8). I provide it anyhow, but please understand that I'm far from an expert. laugh

Code:
emptystring_str = blabla enemy name;



Now, what does this do? You set the POINTER, that, well, just points to something to something else. After this line, emptystring_str points to whatever "blabla enemy name" pointed. Let's have an example.

Consider this scenario:

STRING* str1;
STRING* str2;

Let's assume they are both correctly initialized. Also, let's assume that str1 points to memory adress 100, where the string "Bananas" is stored. str2 points to memory adress 200, where we can find "Apples".

If you now write:

str1 = str2;

then you only changed the memory adresses those things point to.
str1 now points to memory adress 200 as well. If you read it out, you'll get "Apples", but it's really THE SAME "Apples" as str2 holds. If you CHANGE str2 afterwards:

str_cpy(str2,"Cherries");

then you changed the memory area, and reading out str2 will result in "Cherries". Since str1 points to the same area, if you now read out str1, you'll get "Cherries" as well. So the fate of "Apples" was that that bit got overwritten with "Cherries". Is... Is that bad? I don't know how things go in Fruit land.
What happens with "Bananas"? Good question. It's sitting in memory, it's flagged as being used, and there's no way to access it ever again. It's wasted memory. You don't get it back. A memory leak! Oh no!


Instead, if you work with strings, always, always use the str_-functions, unless you're 100% sure you want to do something with the pointers (as opposed to their content).

So here, what you'd do is:

str_cpy(emptystring_str, blbalba enemy name);


I'll spare you the "BUT BE SURE ENEMY NAME IS DEFINED"-stuff, and I also refrain from noting that emptystring_str is totally not empty anymore after one such instruction wink

You wouldn't have to go via the two strings in your TEXT*-object, if you don't want to. You could also go:

str_cpy(emptystring_str,"Name:\n");
str_cat(emptystring_str,enemy_name);

But, you know. Perhaps you prefer your way.

Of course, if you're really crafty, you can just keep emptystring_str to be JUST a pointer, with no actual object behind it, and then point that STRING-pointer to whereever the enemy_names are stored. In that case, you couldn't use a TEXT*-object like that. But have you seen draw_text ?
This is a bit more complicated, and depending on your code structure involves some work, but it allows you to save a bit of memory. ... But of course, it's not like you're "wasting" any amount that one notices on a modern computer.

Last edited by Error014; 08/03/12 23:52.

Perhaps this post will get me points for originality at least.

Check out Dungeon Deities! It's amazing and will make you happy, successful and almost certainly more attractive! It might be true!
Re: string = another string/name [Re: Error014] #405641
08/04/12 09:47
08/04/12 09:47
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
Thank you Error014, str_cpy nailed it laugh.

As a side note, your explanation is very clarifying, comprehensive and humorous laugh.


Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1