Strings in functions (SOLVED)

Started by Hans, Tue 22/04/2008 14:20:31

Previous topic - Next topic

Hans

I'm trying to make a function that could, for example, make a character face down.
This with the following (working!) code in it:

cName.FaceLocation(cName.x, cName.y+10);

In which cName ofcourse stands for the characters name.
I want to replace this 'cName' with a string inside the function.
How to do this?
I couldn't find much about the use of strings (in functions).
I don't think this will work, but it's as far as I could get:

function facedown(string charactername) {
charactername.FaceLocation(charactername.x, charactername.y+10);
}

How do I make this code working?
Best would be if I could make a function compatible with instances. This would result in cName.FaceDown();
Is that possible?

SSH

You don't want a String, you want a Character *

Code: ags

function facedown(Character *charactername) {
  charactername.FaceLocation(charactername.x, charactername.y+10);
}
12

Khris

Or, even better:

Code: ags
function FaceDown(this Character*) {
  this.FaceLocation(this.x, this.y+10);
}


// usage:
player.FaceDown();


Betterererer: use an enum (eDown, eUp, ...  ->  Character.Face(eDown); )

Hans

Okay... I do understand SSH's reply. Thanks for that. So apart from the string-->character part I wasn't too far off I see.

But KhrisMUC? What did you do apart from flipping Character and "charactername" (in your words "this")? Is see it resulted in the possibility of using player.FaceDown(); but how come?

And how exactly do I use enums?

Khris

I agree that the syntax of declaring these extender functions seems weird, but that's the way to do it. Ask CJ :)

enums... look em up in the manual...?

Hans

So just for the record: switching type and parameter gives the possibility of using the character instance?

And why did you both use the * ? Is that a code?
"you want a Character *"
"function facedown(Character *charactername)"
"function FaceDown(this Character*)"

And a thing about enums. I think this 'll be the same as using multiple parameters in a function, but it couldn't hurt to ask: how do I use AND instances AND enums in one function?
Would that be:

enum DirectionOption {
Up, Down, Left, Right
};
function Face (charactername Character, DirectionOption direction)

(I don't know how to make something appear as code on the fora jet.)

And the last thing: I can imagine I shouldn't use some names as parameters/enum name, because AGS Editor wouldn't know which one would be the type and which one would be the parameter. Is that correct?

Khris

Extender functions

* / Pointers

Enums

last thing: this switch-around is only done to the type to be extended, with everything else it's strictly "type variable_name".

Declaring an extender function with additional params:
Code: ags
function SayXTimes(this Character*, String text, int times) {
  ...
}


Oh, and BB-code:

Hans

Thanks. Stupid I didn't see the "How do I post images, smileys and formatting?". I'd already read about enums, but Extender functions and pointers (I won't use for the time coming) were new to me. I find it quite strange I didn't find anything about Extender functions in the manual first. But thanks for that. I've learned a lot from you guys.

SMF spam blocked by CleanTalk