Skip to content

Commit 7542949

Browse files
Fix GMS2 instance depth by assigning from room layers instead of OBJT definitions
Fixes the duplicated Kris in the room_krishallway, and probably other depth issues
1 parent be7d890 commit 7542949

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

src/runner.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -567,15 +567,8 @@ void Runner_draw(Runner* runner) {
567567
runner->renderer->vtable->drawSprite(runner->renderer, tpagIndex, d->layer->xOffset, d->layer->yOffset, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0xFFFFFF, 1.0);
568568
}
569569
} else if(d->layer->type == RoomLayerType_Instances) {
570-
RoomLayerInstancesData *data = d->layer->instancesData;
571-
// TODO: This isn't the right way to do this
572-
repeat(data->instanceCount, i) {
573-
Instance* inst = hmget(runner->instancesToId, data->instanceIds[i]);
574-
if (inst == nullptr)
575-
continue;
576-
if(inst->depth == 0)
577-
inst->depth = d->layer->depth;
578-
}
570+
// Instance depth is assigned from layers during room init (initRoom).
571+
// Nothing to do here - instances are drawn from the DRAWABLE_INSTANCE path.
579572
}
580573
}
581574
}
@@ -761,6 +754,22 @@ static void initRoom(Runner* runner, int32_t roomIndex) {
761754
inst->imageAngle = (float) roomObj->rotation;
762755
}
763756

757+
// In GMS2, instances get their depth from their room layer, not the object definition.
758+
// This must happen before firing Create events so scripts like scr_depth() read the layer depth.
759+
if (runner->isGMS2) {
760+
repeat(room->layerCount, li) {
761+
RoomLayer* layer = &room->layers[li];
762+
if (layer->type != RoomLayerType_Instances || layer->instancesData == nullptr) continue;
763+
RoomLayerInstancesData* layerData = layer->instancesData;
764+
repeat(layerData->instanceCount, ii) {
765+
Instance* inst = hmget(runner->instancesToId, layerData->instanceIds[ii]);
766+
if (inst != nullptr) {
767+
inst->depth = layer->depth;
768+
}
769+
}
770+
}
771+
}
772+
764773
// Pass 2: Fire events for newly created instances (in room definition order)
765774
repeat(room->gameObjectCount, i) {
766775
RoomGameObject* roomObj = &room->gameObjects[i];

src/runner.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ typedef struct Runner {
107107
struct { char* key; int value; }* disabledObjects; // stb_ds string hashmap, nullptr = no filtering
108108
struct { int key; Instance* value; }* instancesToId;
109109
bool isGMS2;
110+
bool forceDrawDepth;
111+
int32_t forcedDepth;
110112
} Runner;
111113

112114
const char* Runner_getEventName(int32_t eventType, int32_t eventSubtype);

src/vm_builtins.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4417,6 +4417,33 @@ static RValue builtinTileLayerShift(MAYBE_UNUSED VMContext* ctx, RValue* args, M
44174417
return RValue_makeUndefined();
44184418
}
44194419

4420+
// ===[ Layer Functions ]===
4421+
4422+
static RValue builtinLayerForceDrawDepth(VMContext* ctx, RValue* args, MAYBE_UNUSED int32_t argCount) {
4423+
Runner* runner = (Runner*) ctx->runner;
4424+
runner->forceDrawDepth = RValue_toBool(args[0]);
4425+
runner->forcedDepth = RValue_toInt32(args[1]);
4426+
return RValue_makeUndefined();
4427+
}
4428+
4429+
static RValue builtinLayerIsDrawDepthForced(VMContext* ctx, MAYBE_UNUSED RValue* args, MAYBE_UNUSED int32_t argCount) {
4430+
Runner* runner = (Runner*) ctx->runner;
4431+
return RValue_makeBool(runner->forceDrawDepth);
4432+
}
4433+
4434+
static RValue builtinLayerGetForcedDepth(VMContext* ctx, MAYBE_UNUSED RValue* args, MAYBE_UNUSED int32_t argCount) {
4435+
Runner* runner = (Runner*) ctx->runner;
4436+
return RValue_makeReal((GMLReal) runner->forcedDepth);
4437+
}
4438+
4439+
// ===[ Array Functions ]===
4440+
4441+
// @@NewGMLArray@@ - GMS2 internal function to create a new empty array.
4442+
// In our VM, arrays are created implicitly on first write, so this is a no-op.
4443+
static RValue builtinNewGMLArray(MAYBE_UNUSED VMContext* ctx, MAYBE_UNUSED RValue* args, MAYBE_UNUSED int32_t argCount) {
4444+
return RValue_makeUndefined();
4445+
}
4446+
44204447
// ===[ PATH FUNCTIONS ]===
44214448

44224449
// path_start(path, speed, endaction, absolute) - HTML5: Assign_Path (yyInstance.js:2695-2743)
@@ -5080,6 +5107,14 @@ void VMBuiltins_registerAll(bool isGMS2) {
50805107
registerBuiltin("tile_layer_show", builtinTileLayerShow);
50815108
registerBuiltin("tile_layer_shift", builtinTileLayerShift);
50825109

5110+
// Layer
5111+
registerBuiltin("layer_force_draw_depth", builtinLayerForceDrawDepth);
5112+
registerBuiltin("layer_is_draw_depth_forced", builtinLayerIsDrawDepthForced);
5113+
registerBuiltin("layer_get_forced_depth", builtinLayerGetForcedDepth);
5114+
5115+
// GMS2 internal
5116+
registerBuiltin("@@NewGMLArray@@", builtinNewGMLArray);
5117+
50835118
// Path
50845119
registerBuiltin("path_start", builtinPathStart);
50855120
registerBuiltin("path_end", builtinPathEnd);

0 commit comments

Comments
 (0)