Help with functions

Game Editor comments and discussion.

Help with functions

Postby ChooJeremy » Wed Nov 23, 2011 8:26 am

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?
ChooJeremy
 
Posts: 9
Joined: Wed Dec 08, 2010 7:39 am
Score: 1 Give a positive score

Re: Help with functions

Postby lcl » Wed Nov 23, 2011 9:22 am

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! :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Help with functions

Postby ChooJeremy » Wed Nov 23, 2011 10:11 am

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?
ChooJeremy
 
Posts: 9
Joined: Wed Dec 08, 2010 7:39 am
Score: 1 Give a positive score

Re: Help with functions

Postby lcl » Wed Nov 23, 2011 10:24 am

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 :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Help with functions

Postby ChooJeremy » Thu Nov 24, 2011 3:58 am

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?
ChooJeremy
 
Posts: 9
Joined: Wed Dec 08, 2010 7:39 am
Score: 1 Give a positive score

Re: Help with functions

Postby skydereign » Thu Nov 24, 2011 5:36 am

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
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help with functions

Postby ChooJeremy » Thu Nov 24, 2011 7:28 am

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?
ChooJeremy
 
Posts: 9
Joined: Wed Dec 08, 2010 7:39 am
Score: 1 Give a positive score

Re: Help with functions

Postby skydereign » Fri Nov 25, 2011 9:54 am

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';
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help with functions

Postby ChooJeremy » Sat Nov 26, 2011 2:16 am

Ok, thanks!
ChooJeremy
 
Posts: 9
Joined: Wed Dec 08, 2010 7:39 am
Score: 1 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest