Is it possible to distinguish between coin types?

When using self.findItems()

Is there a way to distinguish between gold, silver and bronze coins, and how do you find that information?

2 Likes

yes. one way is like this. (javascript, not sure about python)

var items = this.findItems();
        
for(var i = 0; i < items.length; ++i) {
    var item = items[i];
    
    if (String(item).match(/^Gold Coin.*$/g)) {
        // gold coin, do something
    }
    if (String(item).match(/^Silver Coin.*$/g)) {
        // silver coin, do something
    }
    if (String(item).match(/^Bronze Coin.*$/g)) {
        // bronze coin, do something
    }
}
3 Likes

Hehe, surprised that works. The best way to do this is to check the bountyGold property of the item, which is a number indicating how much the coin is worth.

4 Likes

Is there any documentation on these functions? Is there a list of them or something?

That function looks like it is exactly what I need… now I just have to figure out how to use arrays… —subtle hint for a new level—

;p

2 Likes

nice. i didn’t know that property existed :slight_smile: thanks! and yes indeed the code i posted works great!

-BunchofCrooks - if you mouseover the function findItems() in the interface, or click it, it shows sample code how to loop over an array.

2 Likes

Some of the levels let you click on an item and see what properties they have. I think treasure hunt in multiplayer does.

1 Like

Oh cool. If the debugger were enabled, i imagine you could inspect any item. … can hardly wait for that :slight_smile:

1 Like

— What do we want?
— Debugger enabled!
— When do we want it?
— Right new Date() !

2 Likes

items = self.findItems()
for item in items:

Do something with each item here

self.move(item.pos) # Example

I’m still a newbie at programming, 100% of what I know is from playing the single player game.

Don’t I need to establish the variable item somewhere first?
So in line 3, I could put something like

if item.bountygold > 3:
gold = item
else:
self.move(item.pos)

if gold:
self.move(gold.pos)

I am trying to prioritize gold over all other types of coin.

off topic I work in a high needs title 1 school. I have around half my 6th graders ( I teach about 100) playing code combat on their own time now. I caught one girl sneaking it in while she was supposed to be working on another assignment. THAT is educational victory.

1 Like

this line establishes the variable item. it’s basically saying loop over the items array putting each element into the item variable

Thank you sir! Going to try it soon!

1 Like

Does bountyGold need an item to look at?

1 Like

bountyGold is a property on an item object if i’m not mistaken.

so when you call findNearestItem() the object thats returned should have that property. item.bountyGold

I haven’t gotten around to confirming that yet, but it should be there.

1 Like

Right. I’m going to change it to just gold soon, I think.

1 Like

I have learned a ton since posting this, but I am still having trouble with arrays:

When running this for example: (gold-rush, or treasure-grove, or any similar level)

items = self.getItems()
if items:
    for item in items:
        if item.bountyGold > 3:
            self.move(item.pos)

It seems to be working, but the character will change his mind about his current target mid stream, before completing the move action, and I get an error:

“only the last action set in chooseAction() will be applied”

Huge amounts of lag happen here as well. I am guessing that it is searching the array infinitely, and runs out of memory?

How can you stop searching through the array once you find a given piece of data? (If that is even what is happening)

2 Likes

yeah. bountyGold 3 is a gold coin. using just that code if there’s not a gold coin nearby you it will do nothing just sit there. You should definately add in logic for going after silver and bronze coins also, giving the greater bountyGold value items priority over the lesser ones. You also need to factor in the distance to each coin so you don’t try to go after one across the map when there is one right next to you.

I’m not sure about python but in javascript a simple

break;

will take you out of a for loop.

1 Like
items = self.getItems()
if items:
    for item in items:
        if item.bountyGold > 3:
            pos=item.pos
            x=pos.x
            y=pos.y
            self.moveXY(x, y)
            break

Works perfectly! You rock!

1 Like

is it possible de find the value of a group of coin in order to tell the hero to prefer a destination ?

1 Like

I thought about writing something like this once. I think the problem i ran into was trying to use “distanceTo” from a coin object to another coin object didn’t work. I think it said something about not having access to that method. If that has been fixed or changed, it should be very possible.

Loop over the coins, then find nearby coins to that coin and compare distances. Another method that would help greatly in this would be a method that takes an object (item or enemy or friend) and returns all other nearby items with a passed in distance. example

nearby_coins = coin.findItems(5);

This is completely made up and not in the game. But essentially to be able to take any arbitrary coin object and call a method that returns an array of nearby items within x distance would be awesome.

or even if it just returned a count. that might be a lot more simple

num_nearby_coins = coin.nearbyItems(5);
1 Like

I don’t know if this is because my understanding of english … or of the code, however, i don’t understand nothing about the explaination or the way to code it =)

1 Like