Button set a label text (SOLVED)

Started by Yagami Raito, Thu 22/11/2007 18:26:17

Previous topic - Next topic

Yagami Raito

Hello !

My question is, how i can change the text on a labbel by clicking on a button.

Well, I'm using a GUI of my creation, on this GUI, there is a Button, when i use this button,  the text on a label located on the same GUI change.

Ex : My button is called Fireball

-When i click on the Button called Fireball, the text on the label change for a text who explain the rules of the fireball (fireball is blablabla...).


Second question :

Im using an @OVERHOTSPOT@ systeme, where hotspot names appair near the cursor, and i want to know if the button name on a GUI can appair near the cursor too.

Sorry for my bad english I'm French.
Thank you in advance.

Khris

#1
1. Assign a scriptname to the label (e.g. "spell_desc"), then use the .Text property to change the label's text:

Code: ags
function Fireball_OnClick(...) {
  spell_desc.Text="fireball is blablabla...";
}


2. Assign a scriptname to the label holding "@OVERHOTSPOT@", e.g. "cursorlabel".
Inside the repeatedly_execute function in the global script, use code like this:

Code: ags
  GUIControl*gc=GUIControl.GetAtScreenXY(mouse.x, mouse.y);
  Button*b;
  if (gc!=null) b=gc.AsButton;
  String s="";
  if (b==Fireball) s="Cast fireball spell";
  if (b==Electrojolt) s="Cast electrojolt spell";
  if (b==quit) s="Quit the game";
  ...
  if (s.Length==0) s="@OVERHOTSPOT@";
  cursorlabel.Text=s;


Yagami Raito

#2
Hi there KhrisMUC !

First, Thank you for the answer of the first question , it s work perfectly !

But i have a problem on the second question, i implanted the code in my game, in the repeatedly_execute section, but when i launch the game i have an error message :

Error : Run_text_script1: error -6 runnin function repeatedly_execute :
Error : Null pointer referenced
in global script (line 14)

The line 14 is :  Button*b=gc.AsButton;

I haven't modified the code you posted here, i just renamed the label "cursorlabel"

Im searching why, but don't found, what Null pointer referenced mean ?

Ho and im using AGS 2.72, that's why this error appair ?

Khris

#3
Oh jeez, I'm sorry, I'll update the code in my previous post right away.

I forgot to check if gc isn't null before calling gc.AsButton. You can't call a function or property (.AsButton in this case) of something holding null, doing that will throw the "Null pointer referenced" error.

Yagami Raito

Work Perfectly ! Thank you KhrisMUC !

Ashen

Couldn't you simplify that a little, to:
Code: ags

  GUIControl*gc=GUIControl.GetAtScreenXY(mouse.x, mouse.y);
  if (gc == Fireball) cursorlabel.Text="Cast fireball spell";
  else if (gc==Electrojolt) cursorlabel.Text="Cast electrojolt spell";
  else if (gc==quit) cursorlabel.Text="Quit the game";
  // Etc
  else cursorlabel.Text="@OVERHOTSPOT@";


That'd also make it easier to add descriptions for non-Button controls ("Enter a name for your save game" on a TextBox, "Select a game to load" over a ListBox, etc).
I know what you're thinking ... Don't think that.

Yagami Raito

Your code is more simple Ashen, thank to you too.

When i said Its work  perfectly, I'm said it too fast, when i place the cursor on a button,hotspot,character, the name appair on top-left of the screen, but i want it follow the cursor and not staying in the top left.. How i do that with your code ?

I think it s with the line 1 "GetAtScreenXY(mouse.x, mouse.y);".

Khris

Ashen: True, there's really no need to convert it to a button.

Yagami:
I hope you didn't replace your existing rep_ex code?
The code will only adjust the text on the label, positioning it near the cursor is a different thing. I assumed you had that in place when you opened the thread.

The GetAtScreenXY(mouse.x, mouse.y); line won't do anything to the GUI's position; it's used to get the GUIControl at the current mouse position.

Yagami Raito

Héhé ^^

Now i can say it : It's work PERFECTLY !

Thanks to you KhrisMUC and Ashen :)

SMF spam blocked by CleanTalk