Thirsty Panther Posted October 10 Posted October 10 Getting started with programing in Leadwerks can be difficult especially if you are a beginner. With the explosion of AI, such as Chat GPT and Google Gemini, we now have tools to help us on our journey. For example, how do I implement an inventory system in Leadwerks? In the past you would search the Leadwerk forums, google and Youtube. You would come across people with similar questions but with no concrete examples. Today you can open up Google Gemini ( or any other AI flavor you prefer) and type in your question? This is what I did with Gemini. First I asked if it knew the Leadwerks API? It answered: Yes, I am familiar with the Leadwerks 5 API, although it's important to know that Leadwerks 5 has been rebranded and is now known as Ultra Engine. This change also means that the official documentation and learning resources are now found under the Ultra Engine name. The API for what was Leadwerks 5 is designed to be user-friendly, catering to both beginners and experienced developers. It provides a framework for creating 3D games and applications. You can interact with the engine's features through two primary programming languages: Lua: This is the high-level scripting language that is heavily emphasized for its ease of use and rapid development. The official documentation for Ultra Engine is currently focused on the Lua API. C++: For more experienced developers who require lower-level control and performance, the engine offers a C++ API. This allows for more complex programming and direct interaction with the engine's core. Apart from getting the rebranding wrong it does know the 2 programing languages that Leadwerks uses. If you AI does not recognize Leadwerks you can point it to the Leadwerks API at https://www.leadwerks.com/learn . Gemini goes on tell me about key features of the API as well as where to find the documentation for it. Now back to our original question, "How do I make an inventory system in Leadwerks?" So I typed this question into Gemini. This was the response I got back: Of course. Here's a guide on how you would approach making an inventory system in Leadwerks 5, which is now known as Ultra Engine. This process involves three main parts: setting up the data structure to hold the inventory items, creating the logic to manage these items, and building the user interface (UI) to display it. This guide will focus on the concepts and logic using Lua, as it's the most common scripting language for the engine. 1. Storing Inventory Data 🎒 First, you need a way to keep track of what the player is carrying. A table (which is Lua's version of an array or dictionary) is perfect for this. You can create a global table or a table attached to the player's script to act as the inventory container. Lua -- In your player script player.inventory = {} player.inventory.maxSlots = 20 -- Example: set a max number of items Each item in the inventory should also be a table, containing details like its name, description, quantity, and maybe an icon path. Lua -- An example of an item local healthPotion = { name = "Health Potion", description = "Restores 10 health points.", quantity = 1, icon = "Materials/UI/Icons/health_potion.tex", maxStack = 5 -- How many can be in one stack } When you add this item to the player's inventory, you would insert this table into player.inventory. 2. Managing Items (Adding & Removing) You'll need functions to handle adding and removing items from the inventory table. Adding an Item When adding an item, your function should check a few things: Does the player already have this item? If so, can the item be stacked? Is there an empty slot in the inventory? Here’s a conceptual function for adding an item: Lua function AddItemToInventory(item) -- 1. Check if the item can be stacked with an existing one for i, existingItem in ipairs(player.inventory) do if existingItem.name == item.name and existingItem.quantity < existingItem.maxStack then existingItem.quantity = existingItem.quantity + 1 print(item.name .. " added to stack.") return true -- Item was added end end -- 2. If not stackable, check for an empty slot if #player.inventory < player.inventory.maxSlots then table.insert(player.inventory, item) print(item.name .. " added to a new slot.") return true -- Item was added end -- 3. If no space, the inventory is full print("Inventory is full!") return false -- Could not add item end Picking Up Items To pick up an item from the game world, you would typically use a collision check or a raycast. When the player collides with an item object and presses a key (e.g., 'E'), you call your AddItemToInventory() function and then destroy the object in the world. Lua -- In a script attached to the pickup item (e.g., a potion model) function Script:Collision(entity, position, normal, speed) -- Check if the colliding entity is the player if entity:GetKeyValue("name") == "player" then -- Attempt to add the item to the player's inventory if AddItemToInventory(healthPotion) then self.entity:Release() -- Destroy the pickup object end end end 3. Displaying the Inventory (UI) 🖼️ The final piece is showing the inventory to the player. Ultra Engine's UI system is created using its command-based API. Create a Canvas: You'll need a main UI element, often a Canvas, that serves as the background for your inventory screen. You'd typically make this appear or disappear when the player presses a key (e.g., 'I'). Create Slots: Loop through your player.inventory table and create a UI element for each item. This could be an Image or a Panel for each slot. Display Item Info: For each slot that has an item, you would display its icon and quantity. You can use Image:SetMaterial() to show the item's icon and Text:SetText() to show the quantity. Here is a simplified example of what the UI creation logic might look like: Lua -- Function to draw or refresh the inventory UI function DrawInventory() -- First, clear any old inventory UI elements -- Create the main inventory panel local window = Window("Inventory", 100, 100, 400, 500, window.Titlebar) local panel = Panel(10, 40, 380, 450, window) local slotSize = 64 local padding = 10 local itemsPerRow = 5 -- Loop through the inventory and create a visual slot for each item for i, item in ipairs(player.inventory) do local x = (i - 1) % itemsPerRow * (slotSize + padding) local y = math.floor((i - 1) / itemsPerRow) * (slotSize + padding) -- Create the slot panel local slotPanel = Panel(x, y, slotSize, slotSize, panel) -- Display the item icon local itemIcon = Image(0, 0, slotSize, slotSize, slotPanel) itemIcon:SetMaterial(item.icon) -- Display the quantity local quantityText = Text(tostring(item.quantity), 0, 40, slotSize, 20, itemIcon, Text.Right) end end This combination of data management, interaction logic, and UI rendering forms the foundation of a complete inventory system in Ultra Engine. The answer is set out in 3 parts, storing inventory data, managing items, and displaying items. It also give you example code it put in your own game. The great thing about AI is that if there is something you don't understand or you get an error with, you just ask the AI to explain it, or to offer ways of fixing it. For example, you don't understand what a raycast is or how to do it in Leadwerks. Ask the AI. Answer: A raycast is like a laser beam you shoot out from a point in your game world. It travels in a straight line and tells you if it hits something, what it hit, and where it hit. It's a fundamental tool in game development used for things like detecting if a player is looking at an object, checking for obstacles, or determining where a bullet hits. It then goes on to explain how it works in Leadwerks and gives example code to attach to you camera script so that you can use it. The AI's today are very good at coding and explaining coding that the need for tutorials is no longer necessary. 2 1 Quote
eox1 Posted October 11 Posted October 11 nice! i'm tempted myself, just to accelerate the learning process....i'm also tempted to ask the same questions as yours to chatgt and copilot, should be interesting to see how they interpreted the same APi btw Elon Musk said a few days ago that him/the company is planning to launch a full game authoured by xai (grok) for the next year 2 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.