HTML5 How to rewrite to invoke with() statement? [SOLVED]

G

GMSNormie

Guest
GMS2:
I found out that syntax has to be a little bit more strict with HTML5, and you can't call a variable from another object in the code of your current object (It makes sense because the HTML5 version of the game is crashing and it's occurring when objs with code calling for vars of other objs are active). Rather, you have to use with():

E.g. if you have

Code:
obj_ball.y = obj_ball.y + 8
it doesn't work, you have to change it to

Code:
with (obj_ball)

{

y = y +8

}
This is pretty annoying, I wish they made a built in feature to fix this but I guess for now I just have to change a ton of code like this. I was wondering if someone could show me how to change this?

This is in a bomb object that kills enemies within a radius

Code:
if (obj_player.sprite_index=spr_player_bonus)

{

obj_score.thescore += num_killed * obj_enemy_parent.points

}
Edit:
Also, something like this in the enemy obj code:

Code:
if (instance_exists(obj_player))

{

move_towards_point(obj_player.x,obj_player.y,spd);

}
 

jo-thijs

Member
I don't have HTML5 for GM:S 2, so I can't test things, but you can try:

Code:
var b;

with obj_player {
    b = sprite_index == spr_player_bonus;
}

if b {
    obj_score.thescore += num_killed * obj_enemy_parent.points
}
Code:
if instance_exists(obj_player) {
    var px, py;
    with obj_player {
        px = x;
        py = y;
    }
    move_towards_point(px,py,spd);
}
Or if instance id references work, you could try this:
Code:
var playerid, scoreid, enemyparid;

with obj_player
    playerid = id;

with obj_score
    playerid = id;

with obj_enemy_parent
    enemyparid = id;

if (playerid.sprite_index=spr_player_bonus)

{

scoreid.thescore += num_killed * enemyparid.points

}
Or something like this:
Code:
with (obj_player) {
    if (sprite_index == spr_player_bonus) {
        with (other) {
            var product = num_killed;
            with (obj_enemy_parent) {
                product *= points;
            }
            with (obj_score) {
               thescore += product;
            }
        }
}
In GM:S 2, you can also create your own scripts to get and set these values,
using the instance_variable_set and instance_variable_get functions.
 
Top