How to generalize a function?

Started by Clarabella, Tue 11/03/2008 16:45:20

Previous topic - Next topic

Clarabella

I use a sort of "status bar" in my game, a GUI with a label on which all things are happening in the game are written.
In order to write something like "Look at (objectname)" when I look at an object, I wrote this in the room script:

function oObject_Look()
{
lLabel.Text = String.Format("Look at %s", object[0].Name);
}

But it's a very annoying solution, because I have to repeat that line (lLabel.Text etc.) for every object, hotspot and character.
A more simple and faster solution, would be to make a general function, put it in the global script, and call it in the rooms whenever I Look at an object.
But is it possible to "generalize" a line like that? Is it possible to substitute object[0].Name with something general, something that doesn't refer to Object 0, but to every object I'm looking at?
I tried to search in the forum and in the manual, but I couldn't find an explanation, how to do functions related to all objects and not only to a particular object.
So, please, tell me what I have to write, or where I can find a tutorial for these kind of functions.
Thank you!

PS: I know of @OVERHOTSPOT@, but I don't like it because the name of the hotspot disappears when the cursor is not over it. Instead I want the name to be print on the GUI and to stay there until I do another action.

Radiant

Yes, this is certainly possible.

One way of tackling it is using the on_mouse_click() function in the global script, so that it works in each room. From there, you can do something like this:

Code: ags

  Object *o = Object.GetAt (mouse.x, mouse.y);
  if (o != null) {
    ILabel.Text = String.Format ("Look at %s", o.name);
  }


HTH!

Clarabella

I just tryed, but it gives an error message

"GetAt is not a public member of Object. Are you sure you spell it correctly?"

I tried changing Object.GetAt with object.GetAt (without capital letter), but it gives another error message:

" '[' expected "

Maybe is due to my AGS version? It's 3.0.

VK Dan

Try using this (existing ;)) function instead
Code: ags
Object.GetAtScreenXY(mouse.x, mouse.y)

Radiant

No, it's because I'm writing this down from memory and misspelled the function name :)


"Object. " is a set of generic functions, that do not reference any particular object yet.
"object[ ] " is an array that contains all specific objects in the current room. So you use things like "object[1].X" and so forth.

Same thing for Character/character, and so forth.

Clarabella

#5
First of all, thank you both for help!

Quote
Try using this (existing Wink) function instead
Code:

Object.GetAtScreenXY(mouse.x, mouse.y)

I always thought this function could refer only to a particular object (the one located at x, y coordinates). Is it possible to use it in a general way too?

Radiant

Quote from: Clarabella on Tue 11/03/2008 17:48:19
I always thought this function could refer only to a particular object (the one located at x, y coordinates). Is it possible to use it in a general way too?

It doesn't refer to a particular object, it returns a particular object. You can use it to find out which object (if any) the player is clicking on, and do something generic based on that.

Clarabella

#7
Ok, I tryed with GetAtScreenXY.
Now it gives a new error. At the line whith this code:

"lLabel.Text = String.Format ("Look at %s", o.name);"

AGS gives this bug report:

'name' is not a public member of 'Object'.
Why?  :(

*********

EDIT:
Ok, I'm stupid. I forgot the capital letter in o.Name!
Thank you very much for your help! Now it works perfectly!

Khris

In general, whenever you are implementing a short piece of code somebody else posted here, it's better to type it instead of copying & pasting it.
That way, you can use the auto-complete feature of AGS's script editor and won't run into those very avoidable errors. :)

Clarabella

Thanks for this suggestion, Khris, I will keep in mind!  ::)

SMF spam blocked by CleanTalk