Page 1 of 1

Help with functions

PostPosted: Wed Nov 23, 2011 8:26 am
by ChooJeremy
Hi, I'm creating a game where you can click on something and text will appear telling you more information.
I started off with a Mouse button down(left) event on the actors and typed in the code:
Code: Select all
strcpy(txt.text, "This option has not been unlocked yet!");
txt.UserVar = 0;

Where UserVar is needed to tell me when 2.5 seconds have passed and the text will disappear.

However, I found out that I was using the above code a lot of times, just with different text. So I decided to write a function for the above code and call it up when I needed it.
I wrote:
Code: Select all
void MakeText(char TxT)
{
    strcpy(txt.text, "TxT");
    txt.UserVar = 0;
}

in the global code, and GE accepts it just fine.

However, when I wrote
Code: Select all
MakeText(This option has not been unlocked yet!);

in the original Mouse button down event, GE tells me "Error line 1: Undeclared identifier This"

What did I do wrong?

Re: Help with functions

PostPosted: Wed Nov 23, 2011 9:22 am
by lcl
Your code allows only one character input, you have to define it so you can imput strings.
Also, you had "" on sides of TxT, which would just set your actors text to be TxT instead of the contains of variable named text.
Ao, take the quotes away, too.
Here's the correct code:
Code: Select all
void MakeText(char * TxT)
{
    strcpy(txt.text, TxT);
    txt.UserVar = 0;
}

And when you're using this code, you have to have the actors name somewhere in your script, because of a GE "bug".
So, for example you can put the name in comment.
(This is the code where you use your function):
Code: Select all
//txt
MakeText(This option has not been unlocked yet!);

'//'means a commented line, which doesn't have any functional script in it

Ask if you don't understand somethin! :)

Re: Help with functions

PostPosted: Wed Nov 23, 2011 10:11 am
by ChooJeremy
Thanks, but I'm afraid it's still not working. I'm getting the error "Error line 2: Undeclared identifier This"

And when you're using this code, you have to have the actors name somewhere in your script, because of a GE "bug".

So whenever I want to call up this function I need to have "txt" in somewhere in the script?

Re: Help with functions

PostPosted: Wed Nov 23, 2011 10:24 am
by lcl
Oh there's one thing I didn't notice..
When using your function, you have to put the line in quotes, like that:
Code: Select all
//txt
MakeText("This option has not been unlocked yet!");


ChooJeremy wrote:So whenever I want to call up this function I need to have "txt" in somewhere in the script?

Correct :)

Re: Help with functions

PostPosted: Thu Nov 24, 2011 3:58 am
by ChooJeremy
Thanks! It works now :D
One last thing. If I want to define a string in Global code, I use char * var1, and if I want to define a char I use char var1?

Re: Help with functions

PostPosted: Thu Nov 24, 2011 5:36 am
by skydereign
I'd suggest using char arrays for strings. If you declare a char* in global code, then you'll need to allocate the char* before you can use it (so if you don't know what that means then stick with char arrays). And yes, to declare a single char, you just use char.
Code: Select all
char string[10]; // 10 being the size

Re: Help with functions

PostPosted: Thu Nov 24, 2011 7:28 am
by ChooJeremy
Yup, I don't know what that means.
Good to know. I'll just stick with char arrays then.

Edit: I get it, but I'm meeting a problem when using it in GE.

I type in char Gender; in global code and save it.
Then, in the game, when I ask the user to select their gender, (and let's say they choose male), the following code runs:
Code: Select all
strcpy(&Gender, "M");


GE accepts that, but after running the game and activating the event, I get a runtime error: Access beyond array.
Can someone help me?

Re: Help with functions

PostPosted: Fri Nov 25, 2011 9:54 am
by skydereign
You can't use strcpy for setting a single char, as you only have a single space, which if you add the null character to end the string, you have two. You should just set the char directly.
Code: Select all
Gender='M';

Re: Help with functions

PostPosted: Sat Nov 26, 2011 2:16 am
by ChooJeremy
Ok, thanks!