Screeps技术问询:能否为Container设置属性?如何写入其内存?
Hey there! Let's break down your two questions about Screeps containers and memory handling:
1. Can I set custom properties on a Container?
Unfortunately, you can't directly add custom properties to native StructureContainer objects in Screeps—these game objects get reinitialized every tick, so any custom attributes you attach directly would be lost immediately.
The standard solution here is to use the global Memory object to store custom data tied to the container's unique ID. Here's how to implement this:
Setting a custom property
// Retrieve your container (replace with your actual ID or retrieval logic) const myContainer = Game.getObjectById('abc123-container-id'); // Initialize the containers section in Memory if it doesn't exist if (!Memory.containers) Memory.containers = {}; // Store custom properties linked to the container's ID Memory.containers[myContainer.id] = { isMinerStorage: true, priorityLevel: 2, // Add any other custom properties you need };
Retrieving the custom property later
const myContainer = Game.getObjectById('abc123-container-id'); const containerData = Memory.containers[myContainer.id]; if (containerData) { console.log(`This container's priority is: ${containerData.priorityLevel}`); }
2. How to set inventory filters for Containers and write to structures memory?
Screeps doesn't have a built-in inventory filter system for containers, but you can build this logic yourself using Memory to store filter rules, then enforce those rules in your creep behavior code.
Step 1: Write the filter to Memory.structures
First, define which resources the container should accept and save the rule to memory:
const targetContainer = Game.getObjectById('abc123-container-id'); // Initialize Memory.structures if it doesn't exist if (!Memory.structures) Memory.structures = {}; // Store the inventory filter (e.g., only allow energy and utrium ore) Memory.structures[targetContainer.id] = { inventoryFilter: [RESOURCE_ENERGY, RESOURCE_UTRIUM], };
Step 2: Enforce the filter in your creep logic
When your creep tries to transfer resources to or from the container, check against the filter to control the action:
function handleContainerTransfer(creep, containerId) { const container = Game.getObjectById(containerId); const containerMemory = Memory.structures[containerId]; if (!containerMemory?.inventoryFilter) { // No filter set—allow any transfer (or handle as needed) creep.transfer(container, Object.keys(creep.store)[0]); return; } // Get the resource the creep is carrying const carriedResource = Object.keys(creep.store)[0]; // Only allow transfer if the resource is in the filter if (containerMemory.inventoryFilter.includes(carriedResource)) { creep.transfer(container, carriedResource); } else { // Redirect the creep to another container or handle the mismatch creep.say('Wrong resource!'); } }
This approach creates a custom inventory filter by using memory to track rules and checking those rules in your creep's behavior code.
内容的提问来源于stack exchange,提问作者Underonesky




