ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > CopperCube > Programming and Scripting
forum topic indicator Help! With my inventory system
person icon
Null
Guest
Quote
2024-02-02 22:31:58

I need help with this behavior extension i am trying to create.
i believe i have everything correct but whenever coppercube editor trys to verify it it says (syntax error missing ; before statement). but i have triple checked idk howmany times and i dont believe im missing one but if there is another problem that anyone could point out for me or correct maybe the way im doing it or some other type of error in my code, plz help! thank you! the code behavior is as follows: /*
<behavior jsname="behavior_InventorySystemBehavior" description="InventorySystem">
<property name="" type="" default="" />
</behavior>
*/

// Define the behavior for the inventory system
behavior_InventorySystemBehavior = function(){
// Initialize variables
this.inventory = {};
this.slotPrefix = "slot";
this.maxSlots = 15;
this.texturePath = "./textures/";
this.overlayNode = null;
}

// Function to retrieve the 2DOverlay1 node from the scene
behavior_InventorySystemBehavior.prototype.retrieveOverlayNode = function() {
// Attempt to retrieve the 2DOverlay1 node from the scene
this.overlayNode = ccbGetSceneNodeByName("2DOverlay1");
if (!this.overlayNode) {
console.error("2DOverlay1 node not found in the scene.");
}
}

// Function to add an item to the inventory
behavior_InventorySystemBehavior.prototype.addItem = function(itemName) {
// Check if the item already exists in the inventory
if (this.inventory[itemName]) {
// If item exists, increase its quantity
this.inventory[itemName].quantity++;
} else {
// If item doesn't exist, add it to the inventory with quantity 1
this.inventory[itemName] = {
quantity: 1,
texture: this.texturePath + itemName + ".png" // Assuming texture path is ./textures/itemName.png
};
}
// Update the inventory display
this.updateInventoryDisplay();
};

// Function to update the display of the inventory slots
behavior_InventorySystemBehavior.prototype.updateInventoryDisplay = function() {
for (var i = 0; i < this.maxSlots; i++) {
var slotName = this.slotPrefix + i;
var item = this.inventory[slotName];
var slotNodeName = slotName;
var slotNode = this.2DOverlay1.getChild(slotNodeName);
if (!slotNode) {
console.error("Inventory slot node not found:", slotNodeName);
continue;
}
if (item) {
// If an item exists in the slot, set its texture
slotNode.setTexture(item.texture);
} else {
// If no item in the slot, clear the texture
slotNode.setTexture(""); // Assuming setTexture("") clears the texture
}
}
}

// Function to initialize the inventory system
behavior_InventorySystemBehavior.prototype.init = function() {
// Initialize inventory slots
for (var i = 0; i < this.maxSlots; i++) {
var slotName = this.slotPrefix + i;
this.inventory[slotName] = null;
}
}

// Create an instance of the inventory system
var inventory = new behavior_InventorySystemBehavior();

// Initialize the inventory system
inventory.init();

// Example usage:
// Assuming the player picks up an item named "exampleItem"
// Call addItem function to add it to the inventory
inventory.addItem("exampleItem");

person icon
Null
Guest
Quote
2024-02-02 22:41:23

it also says when i testing the game that behavior_InventorySystem is not defined. i think like not defined in copperlichdata or something. how would i go about this if theres someone who has successfully implemented a extension before and knows what im doing wrong plz tell me.

person icon
okeoke
Registered User
Quote
2024-02-03 06:53:35

Hi Null,

1. You have a syntax error on line 29. In js you can not call object property that starts with a numeric literal like obj.2property, you need to use obj['2property'] syntax. This is also the same for variables, their names can't start with number.

2. Your behavior lacks an onAnimate method, which is mandatory. It defines logic which is called on every game frame if a behavior is attached to the node;

3. You don't need to instance your behavior manually as you do on line 74. It will be instanced automatically in case it is attached to a game node. If you still need to run the init method, it could be ran on a first frame inside onAnimate function.
if (!this.isInitialized) {
this.isInitialized = true;
this.init();
return;
}


4. If you're building for desktop console global object is not available, so line 22 and 51 will cause an error.

5. updateInventoryDisplay will not work in case you don't have some other helper which contains getChild and setTexture methods defined. Refer tohttps://www.ambiera.com/coppercu...on how to get node children and set a texture.

You can also check mine implementation for some general tips:https://zeicmann.itch.io/ccb-var...


Create reply:










 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


icon_holyicon_cryicon_devilicon_lookicon_grinicon_kissicon_monkeyicon_hmpf
icon_sadicon_happyicon_smileicon_uhicon_blink   






Copyright© Ambiera e.U. all rights reserved.
Contact | Imprint | Products | Privacy Policy | Terms and Conditions |