[][src]Struct screeps::memory::MemoryReference

pub struct MemoryReference(_);

A Reference to a screeps memory object

Methods

impl MemoryReference[src]

pub fn new() -> Self[src]

pub unsafe fn from_reference_unchecked(reference: Reference) -> Self[src]

Creates a MemoryReference from some JavaScript reference.

Warning: MemoryReference is only designed to work with "plain" JavaScript objects, and passing an array or a non-plain object into this method probably won't be what you want. MemoryReference also gives access to all properties, so if this is indeed a plain object, all of its values should also be plain objects.

Passing a non-plain-object reference into this function won't invoke undefined behavior in and of itself, but other functions can rely on MemoryReference being "plain".

pub fn bool(&self, key: &str) -> bool[src]

pub fn path_bool(&self, path: &str) -> bool[src]

pub fn f64(&self, key: &str) -> Result<Option<f64>, ConversionError>[src]

pub fn path_f64(&self, path: &str) -> Result<Option<f64>, ConversionError>[src]

pub fn i32(&self, key: &str) -> Result<Option<i32>, ConversionError>[src]

pub fn path_i32(&self, path: &str) -> Result<Option<i32>, ConversionError>[src]

pub fn string(&self, key: &str) -> Result<Option<String>, ConversionError>[src]

pub fn path_string(&self, path: &str) -> Result<Option<String>, ConversionError>[src]

pub fn dict(
    &self,
    key: &str
) -> Result<Option<MemoryReference>, ConversionError>
[src]

pub fn path_dict(
    &self,
    path: &str
) -> Result<Option<MemoryReference>, ConversionError>
[src]

pub fn dict_or_create(
    &self,
    key: &str
) -> Result<MemoryReference, UnexpectedTypeError>
[src]

Get a dictionary value or create it if it does not exist.

If the value exists but is a different type, this will return None.

pub fn keys(&self) -> Vec<String>[src]

pub fn del(&self, key: &str)[src]

pub fn path_del(&self, path: &str)[src]

pub fn get<T>(&self, key: &str) -> Result<Option<T>, ConversionError> where
    T: TryFrom<Value, Error = ConversionError>, 
[src]

Gets a custom type. Will return None if null or undefined, and Err if incorrect type.

Example

use log::info;
use screeps::{prelude::*, LocalRoomPosition};

let creep = screeps::game::creeps::get("mycreepname").unwrap();
let mem = creep.memory();
let pos = mem.get::<LocalRoomPosition>("saved_pos").unwrap();
let pos = match pos {
    Some(pos) => {
        info!("found position: {}", pos);
        pos
    }
    None => {
        info!("no position. saving new one!");
        let pos = creep.pos().local();
        mem.set("saved_pos", pos);
        pos
    }
};
info!("final position: {}", pos);
creep.move_to(&pos);

pub fn get_path<T>(&self, path: &str) -> Result<Option<T>, ConversionError> where
    T: TryFrom<Value, Error = ConversionError>, 
[src]

Gets a custom type at a memory path. Will return None if null or undefined, and Err if incorrect type.

Uses lodash in JavaScript to evaluate the path. See https://lodash.com/docs/#get.

pub fn set<T>(&self, key: &str, value: T) where
    T: JsSerialize
[src]

pub fn path_set<T>(&self, path: &str, value: T) where
    T: JsSerialize
[src]

pub fn arr<T>(&self, key: &str) -> Result<Option<Vec<T>>, ConversionError> where
    T: TryFrom<Value, Error = ConversionError>, 
[src]

pub fn path_arr<T>(&self, path: &str) -> Result<Option<Vec<T>>, ConversionError> where
    T: TryFrom<Value, Error = ConversionError>, 
[src]

Trait Implementations

impl AsRef<Reference> for MemoryReference[src]

impl Default for MemoryReference[src]

impl TryFrom<Value> for MemoryReference[src]

type Error = ConversionError

The type returned in the event of a conversion error.

Auto Trait Implementations

Blanket Implementations

impl<T, U> IntoExpectedType<U> for T where
    U: FromExpectedType<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.