![]() |
Problem using a level NPC that's joined to a class
Hey there!
This is me and my friend's first attempt at GS2. This is what we've got so far. We're just wondering; are we on the right track here? We have encountered some problem with triggeraction when trying to trigger serverside from clientside, in a level NPC. Any ideas or suggestions on what to improve or totally change? In case the aweful script does not tell; we're trying to create a shop system..or something closely remote to one... Help is very much appreciated, thanks! Class: PHP Code:
PHP Code:
|
Quote:
|
Quote:
Just wondering; what do you think about the structure of our shop system? Any thoughts or suggestions? |
Box formatting is the devil.
|
Quote:
With this type of loop where you're accessing 'i' it would take less script time to do: PHP Code:
|
You forgot temp.s on your is.
|
Quote:
Could you please expand or elaborate a bit on the meaning of Box formatting? :) I suppose our structure is not that good? And thanks for all the replies and comments so far! |
PHP Code:
|
Box formatting is the ugliest thing to read through ever, not to mention how it expands the code for no good reason. :rolleyes:
|
Hi!
This is what we've got so far. We have now updated the script so that our trigger action triggers onActionBuyItem. Although, we have a variable called this.itemselected, which will store information regarding the currently selected item. this.itemselected contains one of the two available gui controls: Shop_Item_Fireball or Shop_Item_Sword. The gui looks just fine when running the script. We can click on the different items and the currently selected item changes. Although, when we trigger the server via triggeraction, the gui control which is stored in this.itemselected disappears. We've noticed that this happens when we use this.itemselected as a parameter for triggeraction. If we use a string, for example "higuys" as parameter to the triggeraction everything works just fine. I get a feeling that we're not ready to build a shop system! PHP Code:
|
If I understand it correctly, this.itemselected is a GUI control, yeah? GUI controls are a clientsided object and thus can't be read on serverside.
|
Yepp. Since this.itemselected contains a gui control. But when I add one more parameter to the triggeraction it works just fine.
triggeraction(this.x + 0.5, this.y + 0.5, "BuyItem", this.itemselected, null); Any thoughts regarding that? |
You can cut down on your 'itemSelected' code with catchevent. I.e:
PHP Code:
It would also fix your problem where you're passing an object to the server-side instead of string. Objects behave weirdly (as you can see) resulting in their name getting passed or it passes null/0. |
Thank you very much!
I was a little bit concerned about how to solve that problem. I'm also a little bit concerned about where to put the raw data. I would like to create an array consisting of arrays to keep track of all the items: //{Gui name, name, price} this.shop_item_fireball = {"Shop_Item_Fireball", "Fireball", 30}; and then create an array called this.listofItems = {this.shop_item_fireball}; When this is done on the server side I would like to pass that information to the clientside so that the GUI can be built. Would that be inefficient? Or what do you think? |
Your best bet would be to do something like this:
PHP Code:
Then in your shopkeeper class just process the this.items array and create the list you need. Also on the server-side verify their purchase against the server-side items array. |
Quote:
|
Quote:
Whereas if you store it twice, once on the server-side and once on the client-side. You have access to it on the client-side right away without sending it through your server-to-client data routing system. Then when his shop is working the way he wants, he can always go in after and setup a way to sync the shop data to the client. |
This is what we've got so far!
Although I'm interested in re-doing the shop with DB NPC's instead of putting all the item information in the shop class. Do you think that would be more efficient or is this a good solution? Don't really know how to set it all up using DB NPC's yet though. PHP Code:
|
You can use += and -= to add/subtract from a value (purely based on your preference, but most people do that).
Besides that, the only thing that sticks out is the way you're forming your array. Rather than looping through an array, use it like a hash map. PHP Code:
PHP Code:
|
Okay, thank you very much :)
So you don't think we need to re-build it using DB NPCs? We don't really know how to use a DB NPC properly but I understand that's the way the "pro's" are doing it? Thank you :) |
I would recommend a DB NPC only because it will let you keep one copy of the data rather than keeping it in a class joined to many NPCs. As a general rule, I would recommend not storing data in classes.
|
Then we will try that solution :)
If we use a DB NPC named DB_Items we could store all the information regarding the items in that NPC. But what happens with the clientside of the level NPC? Shall we have a copy of all the items information on the clientside? Because we can't read the DB NPCs information from the client side right? Thank you :) |
Quote:
PHP Code:
|
Ah I see, will try this later on when I get the GUI to be more flexible :) Sorry for asking so many dumb questions, but I'm kind of stuck both on the DB NPC and on the GUI part!
Why doesn't this work: PHP Code:
PHP Code:
Hmm... Thank you :) |
'this' refers to the GuiShowImgCtrl instead of the level npc or weapon. You have to use thiso to access your listofitems.
I would recommend doing.. new GuiShowImgCtrl("Split_ShopItem_" @ this.listofitems[0][0]) instead of just using the item's name as well. |
Omg, thank you! Getting a little tired ;D
|
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:
Weapon -shopcontrol: PHP Code:
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:
|
Here's one way of doing it, you assign a shop id to your npc.
I.e: this.shop_id = "basicshop"; PHP Code:
PHP Code:
PHP Code:
|
Thank you :) I tried something similar just now, but failed because I didn't make the function getItems in the DB NPC public. I suppose it has to be public?
|
Quote:
|
Thank you very much! Now I think I understand the DB NPC :)
Although, I still have problems with the PlayerTouchsMe. It seems as if the player touches the NPC when the player is located to the top left of the NPC. This is the NPC script: PHP Code:
PHP Code:
|
You're setting the shape to a 32px by 32px block, which doesn't perfectly fit the character. Try something like 38x48 (change the setShape). I can't remember exactly what it should be, so just change the values until it's working as you want it to, being sure to update the level in-between changes.
|
Ah okay, I thought 32x32 pixels was the default character size :O
Isn't the "hit box" drawn from the upper left corner of the NPC to the lower right corner as a rectangle with the size specified in setShape? |
For normal NPCs 48 by 48 will do fine on server-side, and you're right on about the hit box.
The point of your shop class is to make it accept any shop id, so in your NPC you should be setting the shop id not in the class itself. I.e: PHP Code:
PHP Code:
|
Yepp :) I did that just to check if it worked. I'm going to setup different shop IDs and add them to the different NPCs before joining the class.
But if I set the setshape to 48, 48 wouldn't I touch the NPC even when I'm far away from it? EDIT: NOticed that it worked just fine :) But what about the 32, 32 hit box? Feels like I need an explanation why the hit box is 32,32 while the NPC should be set to 48,48? Is it because the NPCs image blocks the player? |
Quote:
|
| All times are GMT +2. The time now is 07:32 PM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.