To start with I want to thank you all for your replies and good support
We're closing in on the main goal!
We don't have a DB just yet, but we're close to it...I think!
This is what we've got so far:
Class shop
PHP Code:
function onCreated()
{
setShape(1, 32, 32);
}
function onPlayerTouchsMe()
{
this.listofitems = {{"Fireball", "split_fireball", "split_fireball.png", "split_fireball_selected.png", 30},
{"Sword", "split_sword", "split_sword.png", "split_sword_selected.png", 20},
{"Bow", "split_bow", "split_bow.png", "split_bow_selected.png", 10}
};
triggerclient("gui", "-shopcontrol", this.listofitems, this.x, this.y);
}
function onActionBuyItem()
{
this.chat = "I received your request!";
}
Class shop serves as our temp DB. We will fix a DB to hold the information about the items. But for now it's just a class
This class is joined to the NPC who is the shopkeeper. When a player touchs him/her it will send a triggerclient to the shopcontrol weapon:
Weapon -shopcontrol:
PHP Code:
//#CLIENTSIDE
function onActionClientSide(listofitems, npcx, npcy)
{
this.npcx = npcx;
this.npcy = npcy;
this.listofitems = listofitems;
new GuiBitmapBorderCtrl("Shop_Window")
{
this.profile = GuiBitmapBorderProfile;
this.width = screenwidth / 2;
this.height = screenheight / 2;
this.x = screenwidth / 2 - this.width / 2;
this.y = screenheight / 2 - this.height / 2;
this.canmove = false;
this.canresize = false;
new GuiButtonCtrl("Shop_Button")
{
width = 100;
height = 40;
x = Shop_Window.width / 2 - this.width / 2;
y = Shop_Window.height - this.height - 10;
text = "Buy";
}
}
for(i = 0; i < this.listofitems.size(); i++)
{
new GuiShowImgCtrl("Shop_Item_" @ thiso.listofitems[i][0])
{
x = 20 + i * 20 + i * 32;
y = 20;
width = 32;
height = 32;
this.imageselected = thiso.listofitems[i][3];
this.imagenotselected = thiso.listofitems[i][2];
this.image = this.imagenotselected;
thiso.catchevent(this.name, "onMouseDown", "onItemSelected");
}
Shop_Window.addControl("Shop_Item_" @ thiso.listofitems[i][0]);
}
Shop_Window.show();
}
function onMouseDown()
{
if(!((mousescreenx in |Shop_Window.x, Shop_Window.x + Shop_Window.width|)
&&(mousescreeny in |Shop_Window.y, Shop_Window.y + Shop_Window.height|)))
{
Shop_Window.destroy();
}
}
function Shop_Button.onMouseDown()
{
triggeraction(this.npcx + 0.5, this.npcy + 0.5, "BuyItem", "Buy", this.itemselected.substring("Shop_Item_".length()));
Shop_Window.makefirstresponder(true);
}
function onItemSelected(selected)
{
for(i = 1; i < Shop_Window.controls.size(); i ++)
{
temp.control = Shop_Window.controls[i];
temp.control.image = (temp.control == selected ? temp.control.imageselected : temp.control.imagenotselected);
}
this.itemselected = selected.name;
}
The shopcontrol weapon is basically just a gui interface which shows all the possible items to buy.
This is where the trouble starts. When I'm about to send the chosen item back to the npc something goes wrong. It seems like the triggeraction doesn't really do what I thought it would do. I thought it would trigger the npc closest to the coordinates given in the triggeraction (x, y, ...). But maybe I'm doing it all wrong?
Also, it seems as if the NPC's shape is out of synch. The player touches it when the player is positioned at the top left tile of the NPC. Maybe there's something I've done with setshape that doesn't work?
Thank you
EDIT: I managed to get the data back to the NPC! So there's no problem with the triggeraction(npcx, npcy, ...) command
But the NPC PlayerTouchsMe is still troubeling us.
EDIT2: As stated above I managed to get the triggerserver and trigger client between the NPC and the weapon to work. I have now created a DB NPC named DB_Items:
PHP Code:
function onInitialized()
{
onCreated();
}
function onCreated()
{
/*
Format:
this.item_id = {Name, WeaponName, Image, ImageWhenSelected, Price};
*/
this.lisofitems = {{"Fireball", "split_fireball", "split_fireball.png", "split_fireball_selected.png", 30},
{"Sword", "split_sword", "split_sword.png", "split_sword_selected.png", 20},
{"Bow", "split_bow", "split_bow.png", "split_bow_selected.png", 10}
};
}
function getItems()
{
return this.listofitems();
}
Is that a proper way of scripting the DB NPC? Or do I have to trigger the DB NPC and then trigger back to the level NPC in order to get the information the DB holds?