How to pass character names into function

Started by ZayanM, Sat 17/12/2005 15:51:43

Previous topic - Next topic

ZayanM

I made a function in the global script which is

//
function TalkAt (int talkToID, int xDistance, int yDistance, int conversation){

cEgo.Walk(character[talkToID].x + xDistance,  character[talkToID].y + yDistance, eBlock, eWalkableAreas);

gMain.Visible = false; Wait (1);

RunDialog(conversation);

}
//

It works, but how can I replace int talkToID with something else, so when I call the function I would not have to put 0, 1, 2 etc... instead I can put the name/script-o-name like cEGO?

This will make it more readable and easier to edit.

Thanks in advance.

monkey0506

#1
Yes, you can...just replace "int talkToID" with "Character* talkTo".  Then you could change this line:

cEgo.Walk(character[talkToID].x + xDistance,  character[talkToID].y + yDistance, eBlock, eWalkableAreas);

To this:

cEgo.Walk(talkTo.x + xDistance, talkTo.y + yDistance, eBlock, eWalkableAreas);

[EDIT:]

Also, just in case you didn't know, if you want the player character to move (in case you ever decide to have more than one player (and assuming that cEgo is the player)) you can just replace cEgo with player.

Ashen

Also, the character's SCRIPT name (NOTE, not the SCRIPT-O-NAME, so EGO not cEgo) can be passed as an int, so you could use your existing function, and TalkAt (EGO, 12, 12, 2);. Personally, I think Pointers are the better way (as above), but it's still easier than just using the number.
I know what you're thinking ... Don't think that.

ZayanM

Thanks alot for the quick replies. :)

Is Character* the pointer or is it a type?

monkey0506

#4
Character is a type.  The asterik (*) declares the variable (in this case talkTo) as a pointer.  I generally stick the asterik next to the type instead of the variable name, but that can be confusing if you declare several variables at once globally, then the following:

Character* char1, char2, char3;

Wouldn't compile.  You would have to type it as:

Character *char1, *char2, *char3;

In AGS you can't create variables of certain types (these are called managed types and include Character, GUI, GUIControl, Label, Button, etc.), but you can create pointers to them.  You have to include several asteriks in the above example because that is simply the way that the variable declarations are parsed, with the asterik sticking to the first variable, and unless you put another asterik it would parse it as trying to declare a variable of that type instead of a pointer.

Now I'm pretty sure that this is confusing...so...look up pointers in the manual and just read through the sections about them...

[EDIT:]

I take it that Ashen preferred my description better since he deleted his and then edited the end of my post...:D

ZayanM

Okay thanks a lot.
I don't understand pointers yet, but I'm going to try and learn them.

monkey0506

#6
The basic idea of a pointer is that instead of creating a new variable, you are just going to "point" to one that is already stored in the memory.  In AGS (as I described in my previous post (in my most recent edit)) there are certain "managed" types that you cannot create instances of (you can't create a variable of these types).  That is because AGS "manages" them.  For example, the GUI type is managed.  All of the GUI variables that will be included in your game are already included by AGS, you can't create any new ones.  So, if your list of GUIs (in the Game Editor GUI pane) looks like this:

1: MAIN
2: INV
3: QUIT
4: RESTART
5: PAUSE

Then there would be five managed GUI objects created by AGS, one for each GUI.  You can access them with the built-in pointers (the gui array and the script o-names are pointers to these GUI objects), and you can also create your own pointers.  For example, if you wanted to find out what GUI is under the mouse, and then set the text on a label to its ID, you could do the following:

Code: ags
GUI *guiat = GUI.GetAtScreenXY(mouse.x, mouse.y);
if (guiat != null) {
  string buffer;
  StrFormat(buffer, "%d", guiat.ID);
  lblMyLabel.SetText(buffer);
  }


I hope that this helps to clarify things and not just muddle them up worse.

[EDIT:]

I forgot to mention that pointers can be "null" which means that they aren't "pointing" to anything.  If you try to work with a null pointer (except when comparing it), such as when I typed guiat.ID then the game would crash because there is no GUI object being pointed to by guiat, so it doesn't have any ID member.

To prevent this, you can check it's value, as I did with the statement if (guiat != null).  You can also compare it to other GUI objects, such as if (guiat == gMain) would test whether guiat was pointing to the MAIN GUI.  Even if guiat is null, it is still safe to check it against a gui like this, and as long as you have a GUI named "MAIN" (with the script o-name gMain) it would pass as false, so the script wouldn't run.

SMF spam blocked by CleanTalk