Screeps: Is there a way to set properties to a container? - properties

I'm trying to set inventory filters for my container structures. I'm not sure how to write to a structures memory.

there only exists one Memory object "Memory"
if you have a room, for example, room.memory is just a shorthand for
Memory.rooms[roomName]
in the same way, you could create your own memory for containers like this:
let container = myContainer
if (!Memory.objects) Memory.objects = { } // init memory
if !(container.id in Memory.objects) Memory.objects[container.id] = { }
let containerMemory = Memory.objects[container.id]

Related

In Terraform, can I choose whether or not a variable is addressed based on a different Variable? NIC, Dynamic Static

I am trying to see if there is a way in Terraform code to optional provide the need for a variable based on the input of a different variable. For example, I have a module that creates a NIC within Azure, the code can look like the following:
resource "azurerm_network_interface" "networkinterace" {
name = "${var.vm_name}-NIC1"
resource_group_name = azurerm_resource_group.vm_resource_group.name
location = azurerm_resource_group.vm_resource_group.location
ip_configuration {
name = "IPName"
subnet_id = var.subnet_id
private_ip_address_allocation = var.ip_address_allocation #You can set this to Static
private_ip_address = var.private_ip
}
tags = var.tags
}
If the user decides the private_ip_address_allocation value to be "Dynamic", then the need for private_ip_address is not needed. However, if the user decides that the value is "Static" then the value is in fact needed.
My immediate solution was to have the user either place the IP value x.x.x.x if they choose Static, and to place "null" if they choose Dynamic. However, running the plan over and over will state that there will be a change and the IP will be changed to null if apply is ran. This actually doesnt do anything on the NIC itself, but I would like this to be more idempotent and show that no change will be made if Plan is ran. Thoughts?
You can do that.
I assume there is a variable somewhere called is_dynamic:
variable "is_dynamic" {
default = true
}
Then in your network interface you can set your private_ip_address as follows:
ip_configuration {
...
private_ip_address = var.is_dynamic ? null : var.private_ip
...
}
Or probably you have "Dynamic" and "Static" as a variable value:
variable "allocation" {
default = "Dynamic"
}
then you can try something like
ip_configuration {
...
private_ip_address = var.allocation == "Dynamic" ? null : var.private_ip
...
}

Using global variable in ASP.NET Core controller

The question is simple but I don't know how use it.
For example there is a controller
public class MainController : Controller
{
private int a;
public IActionResult Index(bool set = true)
{
if (set) a = 10;
return View(a)
}
}
If I get in Index page at first time, I set a = 10. And I get in Index page again (for example refresh Index page or paging in Index page, i.e. move in same page) Actually, I get in Index page with url : ~Index?set=False after first access.
Then the a has 0 (default for int variable). I did not know the Controller page (Controller class) is always initialized when I gen in it even when I move to same page.
So, I want to use variable like global variable not using session.
Is there any way?
It sounds like you wish to persist a variable between requests.
Per user
If you wish to store a variable that persists but is only visible to the current user, use session state:
public int? A
{
get
{
return HttpContext.Current.Session["A"] as int?;
}
set
{
HttpContext.Current.Session["A"] = value;
}
}
Note that we are using int? instead of int in order to handle the case where the session variable has not yet been set. If you prefer to default to 0, you can simply use the coalesce operator, ??.
Truly global
If you wish to persist a variable in a manner where there is only one copy for all users, you can store it in a static variable or in an application state variable.
So either
static volatile public int a;
Or
public int? A
{
get
{
return HttpContext.Current.Application["A"] as int?;
}
set
{
HttpContext.Current.Application["A"] = value;
}
}
Obviously variables that are shared between users can change at any time (due to activity in other threads), so you should be careful about how you handle them. For variables that are int-sized or smaller, the processor will perform atomic reads and writes, but for variables larger than an int you may need to use Interlocked or lock to control access.
You do not need to worry about thread synchronization for session variables; the framework handles it for you.
Note: The above is just an example to help you find the right API. It does not necessarily demonstrate the best pattern-- accessing HttpContext via the static method Current is considered bad form, as it makes it impossible to mock the context. Please see this article for ways to expose it to your code via DI.

What WinJS.Application.sessionState really stores

In my Windows Store App I store data directly in the sessionState object so I don't need to move data there later on. In one case I store an object that has accessor methods for variables that are declared in the containing scope like so:
(function ()
{
var a = [];
var index = -1;
WinJS.Application.sessionState.data =
{
add: function (item)
{
index = a.length;
a.push(item);
},
currentItem: function ()
{
return a[index];
}
};
})();
My question is if the sessionState object will store a and index since they're scope referenced or not since they're are not really in it.
You can use "data" to manipulate "a" and "index" until the first time your app was suspended. Any data stored in the sessionState object is automatically serialized to disk when your app is suspended. Functions will be removed. After resuming, we lost 2 functions "add" and "currentItem".
See more: http://msdn.microsoft.com/en-us/library/windows/apps/hh440965.aspx

toString returning all of an objects getter methods

I'm using ColdFusion, but I'd be interested to know how other languages cope with this.
Is there a better way of returning all of an objects variables (getters) without writing a massive toString() method on the object.
variables.oCity = createObject("component", "_lbr._core._locations.city").init();
variables.oCity.setName(request.parameters.sCityName);
variables.oCity.setCountryID(request.parameters.nLocationCountryID);
if(request.parameters.nStateID eq 0){
variables.stArgs = {};
variables.stArgs.sState = request.parameters.sLocationCountry;
variables.stArgs.nCheckCountryID = request.parameters.nCountryID;
variables.oCity.setStateID = application.stObj.oLocationBusiness.getState(argumentCollection=variables.stArgs).getStateID();
} else {
variables.oCity.setStateID = request.parameters.nStateID;
}
My code looks like that. What I'd like is to output everything I have just set (as well as anything that the object defaults too) without writing a giant toString that concatenates all the various variables that might look like this:
Object: StateID = 12, Name = "Argentina", CountryID = 32, CityID = 44
My heart tells me this is unlikely.
This depends on how you are storing your variables within your object. I generally store all of my variables in a variables.instance structure. I then create a get() that simply returns the variables.instance structure:
public struct function get(){
return Duplicate(variables.instance);
}
If you use cfproperty, and accessor=true or persistent=true in CF9+, a dump of the cfc will return all the properties without getter=false.

Efficient way to run multiple scripts using javax.script

I am developing a game where I'd like to have multiple scripts that all implement the same structure. Each script would need to be run in its own scope so that code doesn't overlap other scripts. For example:
structure.js
function OnInit() {
// Define resources to load, collision vars, etc.
}
function OnLoop() {
// Every loop
}
function ClickEvent() {
// Someone clicked me
}
// Other fun functions
Now, lets say I have: "BadGuy.js", "ReallyReallyBadGuy.js", "OtherBadGuy.js" - They all look like the above in terms of structure. Within the game whenever an event takes place, I'd like to invoke the appropriate function.
The problem comes down to efficiency and speed. I found a working solution by creating an engine for each script instance (using getEngineByName), but that just doesn't seem ideal to me.
If there isn't a better solution, I'll probably resort to each script having its own unique class / function names. I.e.
BadGuy.js
var BadGuy = new Object();
BadGuy.ClickEvent = function() {
}
I don't think you need to create a new ScriptEngine for every "Guy". You can manage them all in one engine. So with advance apologies for butchering you game scenario.....
Get one instance of the Rhino engine.
Issue eval(script) statements to add new JS Objects to the engine, along with the different behaviours (or functions) that you want these Objects to support.
You have a couple of different choices for invoking against each one, but as long as each "guy" has a unique name, you can always reference them by name and invoke a named method against it.
For more performance sensitive operations (perhaps some sort of round based event loop) you can precompile a script in the same engine which can then be executed without having to re-evaluate the source.
Here's a sample I wrote in Groovy.
import javax.script.*;
sem = new ScriptEngineManager();
engine = sem.getEngineByExtension("js");
engine.getBindings(ScriptContext.ENGINE_SCOPE).put("out", System.out);
eventLoop = "for(guy in allGuys) { out.println(allGuys[guy].Action(action)); }; "
engine.eval("var allGuys = []");
engine.eval("var BadGuy = new Object(); allGuys.push(BadGuy); BadGuy.ClickEvent = function() { return 'I am a BadGuy' }; BadGuy.Action = function(activity) { return 'I am doing ' + activity + ' in a BAD way' }");
engine.eval("var GoodGuy = new Object(); allGuys.push(GoodGuy); GoodGuy.ClickEvent = function() { return 'I am a GoodGuy' }; GoodGuy.Action = function(activity) { return 'I am doing ' + activity + ' in a GOOD way' }");
CompiledScript executeEvents = engine.compile(eventLoop);
println engine.invokeMethod(engine.get("BadGuy"), "ClickEvent");
println engine.invokeMethod(engine.get("GoodGuy"), "ClickEvent");
engine.getBindings(ScriptContext.ENGINE_SCOPE).put("action", "knitting");
executeEvents.eval();