Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Game Dev 2: referencing player inside event handler function

Hi there from Game Dev 2 Final Project!

How can I have my game spawns (like “ogre”) to locate the player?
Game spawns have .findNearestByType() method, but what is the player’s type?

I am trying to reference the player inside an event handler function. Although it is stored in a variable hero, there seem to be no effect.

What am I doing wrong?

var hero=game.spawnPlayerXY("samurai", 34, 8);
game.spawnXY("ogre-f", 39, 56);

function onSpawn(event){
    var unit=event.target;
    if (unit.type=="ogre"){
        unit.say("Good morning"); // which it does
        if (unit.distanceTo(hero) < 20){
            unit.say("Within range"); // never happens
        }
    }
}
game.setActionFor("ogre", "spawn", onSpawn);

Based on what you are showing, I think this is something like what your are looking for. In gam dev, the only antagonist to the ogres is the ‘hero’. When you spawn them, you need to set their behavior attribute, something like this (yes, this is PY…let me know if I need to convert to JS):

def spawnMunchkin(x, y):
    ogre = game.spawnXY("munchkin", x, y)
    ogre.behavior = "AttacksNearest"

Using a loop, you can make use of this function as many times as you desire.

This will make the ogre (whatever) attack as soon as the hero comes in sight.

Thank you! I am aware of behaviors, it’s just not exactly what I wanted to do. According to my design, in the game there should be other game spawns such as “archers”. They will be nearest enemies to the ogre, but I don’t want them attacked. In my plan, the player would have a quest to defeat the ogre and “free” the archers.
This way I really could use a way to distinguish the player from other allies.

1 Like

Well, this might be stretching my knowledge on CC game dev…it is certainly not a strength.

Since you already know of .behavior, I’m only saying this just in case: You can only set the attributes for a defined object. In the example above, ‘ogre = game.spawn’, the object is being defined as the very temporary variable, ogre. With this, and only within this single instance (iteration), you can change any changeable attribute.

That being said, tho a very tedious method, you can try assigning permanent variable names to both your good and bad guys. This would allow you to control them individually, but with separate code for each one…

Just a thought…I’m sure there is a better way to achieve your intent, but game dev is not my best.

Thank you, dedreous! I found my mistake! It was all about iterations. I should have had an infinite loop inside the function. This way game spawns will continuously evaluate conditions.
All my ogres reported hero “Within range” after adding a while(true) loop.

I like that it’s possible to set attributes for ogres inside the function, as unit is a defined object.
This will affect all the ogres.

var hero=game.spawnPlayerXY("samurai", 34, 8);

game.spawnXY("ogre-f", 39, 56);
game.spawnXY("ogre-f", 41, 33);
game.spawnXY("ogre-f", 12, 10);

function onSpawn(event){
    var unit=event.target;
    if (unit.type=="ogre"){
        unit.say("Good morning"); 
        unit.attackDamage = 40;   
        while(true){
            if (unit.distanceTo(hero) < 20){
                unit.say("Within range" );
            }
        }
    }
}

game.setActionFor("ogre", "spawn", onSpawn);