[Solved] I need an expression for "If player carrying object"
Curt A. P.
22 Nov 2020, 17:30Not sure how to explain...
I know the
Got(object) function
but what I want is something like:
if Got ( -an object with object.stringattribute = this.stringattribute- )
Pertex
22 Nov 2020, 17:52something like
if Got(object) and object.stringattribute = "hallo"
mrangel
22 Nov 2020, 18:06You can use FilterByAttribute
to get a list of objects with a given attribute.
For example, FilterByAttribute (ScopeInventory(), "attributename", "value")
will return a list of objects in the inventory for which object.attributename = "value"
is true.
To turn a list into a boolean, you'd use ListCount
to get the number of objects in the list. So you can do something like:
if (ListCount (FilterByAttribute (ScopeInventory(), "color", "red")) > 0) {
msg ("Congratulations, you are carrying something red!")
}
Curt A. P.
22 Nov 2020, 18:32Oh, Thanks! I really can't comprehend the order of operations in expressions like this.
mrangel, thanks. If I want to use that object this should work too, I hope. However, this will take quite some changes and time to test.
To use that object I need this beforehand, or?
obj = FilterByAttribute (ScopeInventory(), "main_alias", this.main_alias)
\\\EDIT: Looks wrong :-/ This returns a list and not the object, I think.
And then I could do this?
obj = FilterByAttribute (ScopeInventory(), "main_alias", this.main_alias)
If Got(obj) {
obj.amount = obj.amount + ToInt(result)
if (this.amount < 1) {
RemoveObject (this)
}
}
.
.
Pertex, I needed a function to find that object without having its name by its attribute. Otherwise, actually, your example is similar to what I already had. Thanks though!
Curt A. P.
22 Nov 2020, 18:52Or:
if (ListCount (FilterByAttribute (ScopeInventory(), "color", this.color)) > 0) {
obj = PickOneObject (FilterByAttribute (ScopeInventory(), "color", this.color))
Or:
obj = PickOneObject (FilterByAttribute (AllObjects(), "color", this.color))
if (Got (obj)) {
mrangel
22 Nov 2020, 19:02(edited for typo)
In that case, your obj
will be a list of objects in the inventory whose main_alias
is the same as whatever object this
is.
You don't need to use Got (obj)
in this case, because we know all the objects in the list are in the inventory. If the player doesn't have a matching object, the list will be empty.
This returns a list and not the object, I think.
Yes.
If the player doesn't have the object, the list will be length zero.
If you know the player will only have one object with that main_alias
(or you only want to do one of them), you would do:
lst = FilterByAttribute (ScopeInventory(), "main_alias", this.main_alias)
If (ListCount (lst) > 0) {
obj = ObjectListItem (lst, 0)
obj.amount = obj.amount + ToInt(result)
if (this.amount < 1) {
RemoveObject (this)
}
}
If you want it to adjust the amount of all items with the same main_alias, that would be:
lst = FilterByAttribute (ScopeInventory(), "main_alias", this.main_alias)
foreach (obj, lst) {
obj.amount = obj.amount + ToInt(result)
if (this.amount < 1) {
RemoveObject (this)
}
}
(in this case you don't need the if statement, because foreach
already skips an empty list)
Curt A. P.
22 Nov 2020, 21:35If you know the player will only have one object with that main_alias
That's the case.
lst = FilterByAttribute (ScopeInventory(), "main_alias", this.main_alias)
If (ListCount (lst) > 0) {
obj = ObjectListItem (lst, 0)
obj.amount = obj.amount + ToInt(result)
if (this.amount < 1) {
RemoveObject (this)
}
}
Nice! Works like needed. I was looking for something like ObjectListItem ()...
Thanks!
I didn't have a solution to make my take and drop verbs compatible with my stacking method as inherited types.
<take type="script"><![CDATA[
if (this.amount > 1) {
msg ("How many? (max." + ToString(this.amount) + ")<br/>Type a valid number...</br>")
get input {
msg ("You typed: " + result + "</br>")
if (IsInt(result) and ToInt(result) < this.amount + 1) {
this.amount = this.amount - ToInt(result)
lst = FilterByAttribute(ScopeInventory(), "main_alias", this.main_alias)
if (ListCount (lst) > 0) {
obj = ObjectListItem (lst, 0)
obj.amount = obj.amount + ToInt(result)
msg ("You take " + result + " x " + this.main_alias + ". You have {object:" + obj.name + "}.")
if (this.amount < 1) {
RemoveObject (this)
}
}
else {
lst2 = FilterByAttribute(GetAllChildObjects(inv_vault), "main_alias", this.main_alias)
obj = ObjectListItem (lst2, 0)
obj.amount = ToInt(result)
MoveObject (obj, player)
msg ("You take " + result + " x " + this.main_alias + ". You have {object:" + obj.name + "}.")
if (this.amount < 1) {
RemoveObject (this)
}
}
}
else {
msg ("You didn't type a valid number.")
}
}
}
else {
lst = FilterByAttribute(ScopeInventory(), "main_alias", this.main_alias)
if (ListCount (lst) > 0) {
obj = ObjectListItem (lst, 0)
obj.amount = obj.amount + this.amount
msg ("You take " + this.alias + ". You have {object:" + obj.name + "}.")
RemoveObject (this)
}
else {
lst2 = FilterByAttribute(GetAllChildObjects(inv_vault), "main_alias", this.main_alias)
obj = ObjectListItem (lst2, 0)
obj.amount = ToInt(result)
msg ("You take " + this.alias + ". You have {object:" + obj.name + "}.")
MoveObject (obj, player)
RemoveObject (this)
}
}
]]></take>