Tuesday, 15 February 2011

javascript - How can values from one object be used to edit those of another object? -



javascript - How can values from one object be used to edit those of another object? -

explanation of layout/format:

in game coding, there different resources. upgrades items cost varying amounts of these resources, although cost 2 or 3 resource types. each of several hundred resource types have multiple parameters (quantity, flavour text, etc.), decided sensible create info pertaining these resources via objects. layout decided upon follows ($ denotes root game object):

$.resources.genericresource = { resource1: { amount: 0, earnt: 0, flavourtext: "generic flavour text" } resource2: { amount: 0, earnt: 0, flavourtext: "not generic flavour text" } resource42: { amount: 0, earnt: 0, flavourtext: "the reply life, universe, , everything" //possibly... } }

every resource has amount integer variable, (as name suggests) denotes quantity of each resource owned.

each purchasable item (upgrade, building, etc.) has base of operations cost of number of resources (as few other things). so, denoted via object:

$.upgrades.genericupgrade.basecost = { resource1: 7, resource2: 1, resource42: math.pi }

note both of these initialised more intelligently has been detailed here - illustration show format used.

reason layout/format requirement:

now, these need objects opposed arrays, because there info associated each of resources - , game go on updated new resources (so save-file problems arise index editing). however, instead of basecost every purchasable item containing key every resource (for values zeros), have contain values different zero. increment neatness, ease of updating, file size , sanity no end.

my problem:

quite simply: have absolutely no thought how create loop which, given basecost object, returns values of related $.resources object , due fact objects, unlike arrays, non-indexable. need edit $.resources value after successful purchase, although much easier given names of pertinent keys, simple for loop

$.resources[resource].amount -= cost;

for each of resources.

tl;dr: don't know how iterate upon 2 separate objects given key names 1 of them.

possible solutions (or attempts/beginnings @ thus) myself:

before go further, firstly create couple of helper variables reference basecost , $.resources objects, cut down code length (in real code former reference $.upgrades object):

var $u = $.upgrades.genericupgrade.basecost; var $r = $.resources;

i began thinking through how input gained - firstly number of keys $.upgrades.genericupgrade.basecost need ascertained. simple plenty - similar following:

var keys = object.keys($u); var keyquantity = keys.length;

the previous code snippet contains yet useful variable: array containing key names strings. useful final replacement, firstly need for loop within validity of purchase found (aka. whether or not expensive):

var keysindex; (i = 0; < keyquantity; i++) { keysindex = keys[i]; //is resource cost greater amount of resource owned? if ($u[keysindex] > $r[keysindex]) { //code either break out of loop, homecoming false, or similar } }

this complete: required negate cost if item not expensive, , set of code together. following:

function purchaseupgrade(upgrade) { var $u = $.upgrades[upgrade].basecost; var $r = $.resources; var keys = object.keys($u); var keyquantity = keys.length; //helper variable assist key referencing: var keysindex; //boolean decide whether or not purchase valid var isvalid = true; //check resources in $u: (i = 0; < keyquantity; i++) { keysindex = keys[i]; //is resource cost greater amount of resource owned? if ($u[keysindex] > $r[keysindex].amount) { //invalidate purchase isvalid = false; //as no more permutations necessary, break loop: break; } } //if resource costs lesser owned resources, following: if (isvalid === true) { (i = 0; < keyquantity; i++) { keysindex = keys[i]; $r[keysindex].amount -= $u[keysindex]; } $.upgrades[upgrade].owned += 1; } }

the function can called purchaseupgrade("nameofupgrade").

wrapping up

i realise have solved problem whilst writing out... still, there no apparent similar questions on stackoverflow, have still decided post this, may come in useful else @ point. however, if thinks have better/more efficient answer, sense free post - in case, select 'best answer'.

javascript object

No comments:

Post a Comment