Informations
Jump to content

Lorem Ipsum...

Click to Dismiss this Notification
Ładowanie danych...

Recommended Posts

Hello everyone!

Welcome to this introductory tutorial! 😊

In today's lesson, we'll focus on utilizing tables instead of a series of if-else statements. We will also explore the use of ipairs instead of table.getn (shoutout to contributions from the community! ❤️

Why Does This Matter?

I've noticed many developers struggling with creating quests due to having too many if and elseif statements without knowing where to properly place else or end clauses. This often results in quests exploding to over 300 lines with at least 20 elseif statements.

Fear not! I’ll guide you on how to streamline your script to around just 20 lines, making it not only easier to understand but also simple to add new items!

Let's consider an example of item trading, where a player exchanges items with an NPC to receive a new item.

Example Quest Code:

 
This is the hidden content, please

Let's Break It Down

We begin by initializing our table of items:

 
This is the hidden content, please

If you want to add another item, simply insert a new line with the following format:

 
This is the hidden content, please

Displaying Items in the Game:

To populate the item's display for the player, we utilize the following code:

 
This is the hidden content, please

Here we create a local empty variable called menu, then proceed to load the first tab from the data array. This updates our menu with item names.

Allowing Player Selection:

This line displays the item selection to the player:

 
This is the hidden content, please

Counting the Selected Item:

For checking the selected item’s quantity, we apply:

 
This is the hidden content, please

To clarify:

  • pc.count_item refers to the function that checks the player’s item count.
  • data is our item table.
  • selected_item represents the currently chosen item.
  • [2] accesses the second parameter in the selected item (the item's vnum).
Link to comment
Share on other sites


Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

spacer.png

Disable AdBlock
The popup will be closed in 5 seconds...