PDA

View Full Version : Rar @ 1,000 onActions!


xXziroXx
04-09-2006, 03:00 AM
Ok, im working on a Inventory but Ive encountered somewhat of a *problem*. Let me show you:



... Some script ...

new GuiScrollCtrl( "InventoryScroll" ) {
height = 149, width = 32 + (32 + 8)*10;
x = 12, y = 175;
hscrollbar = "dynamic";
vscrollbar = "alwaysOff";

for (i = 0; i < clientr.Weapons.size(); i ++) {
tokens = clientr.Weapons[i].tokenize(";");
new GuiBitmapButtonCtrl( "InventoryButton" @ i) {
x = 16 + (32 + 8)*i, y = 16;
width = 32, height = 32;
mouseoverbitmap = tokens[1];
normalbitmap = tokens[1];
pressedbitmap = tokens[1];
}
}
}




Is there another way to make it check if an icon is pressed rather then doing like this for each slot:


//UGH!
function InventoryButton0.onAction()
{
player.chat = "PRESSED 0!";
}

function InventoryButton1.onAction()
{
player.chat = "PRESSED 1!";
}

function InventoryButton2.onAction()
{
player.chat = "PRESSED 2!";
}

etc, etc..



I just HATE alot of basicly exactly the same functions!

napo_p2p
04-09-2006, 03:07 AM
Is there another way to make it check if an icon is pressed rather then doing like this for each slot:

Yes. You can use 'catchevent'. - catchevent(obj, oldevent, newevent);

new GuiBitmapButtonCtrl( "InventoryButton" @ i) {
x = 16 + (32 + 8)*i, y = 16;
width = 32, height = 32;
mouseoverbitmap = tokens[1];
normalbitmap = tokens[1];
pressedbitmap = tokens[1];

//added this line:
thiso.catchevent(this, "onAction", "onButtonPressed");
}


Now, every time a button is pressed, 'onButtonPressed' will be called (with a parameter that is a reference to the object that called it).

Example:
function onButtonPressed(obj) {
player.chat = temp.obj.name;
}

xXziroXx
04-09-2006, 03:09 AM
Napo, I think I might love you! ;o

ApothiX
04-11-2006, 03:45 PM
It might also be easier if you assign a variable to the object containing the number of the control.

For example:
new GuiBitmapButtonCtrl( "InventoryButton" @ i) {
x = 16 + (32 + 8)*i, y = 16;
width = 32, height = 32;
mouseoverbitmap = tokens[1];
normalbitmap = tokens[1];
pressedbitmap = tokens[1];

// ** Added
this.buttonnumber = i;

thiso.catchevent(this, "onAction", "onButtonPressed");
}

// ** Modified
function onButtonPressed(obj) {
player.chat = "Button Number: " @ obj.buttonnumber @ " pressed!";
}