Author Topic: I'm trying to do in Attract-Mode (Help Needed!) Please  (Read 371 times)

gamesmame

  • Sr. Member
  • ****
  • Posts: 101
    • View Profile
I'm trying to do in Attract-Mode (Help Needed!) Please
« on: April 16, 2025, 05:24:19 PM »
## 🧵 What I'm trying to do in Attract-Mode (Help Needed!)

Hello everyone, I need some help with a layout I'm creating in Attract-Mode.

I'm trying to load custom artwork positions (x, y, width, height) from a per-game `.txt` file and apply them dynamically when the layout is loaded.

---
### ✅ What I have so far

Each game has a corresponding text file like this, stored in:
```
M:/Media/Nintendo Entertainment System/LayoutsData/
```

Example file: `Abadox - The Deadly Inner War (USA).txt`
```
snap_x=100
snap_y=120
snap_w=900
snap_h=680
art3_x=0
art3_y=0
art3_w=1920
art3_h=1080
```

---
### 🧠 My layout.nut logic

I'm using this Squirrel code in `layout.nut`:

```nut
fe.layout.width = 1920;
fe.layout.height = 1080;

function read_config(path)
{
    local config = {};
    try {
        local f = file(path, "r");
        if (typeof f.read_line != "function") {
            print("Invalid file: " + path + "\n");
            return config;
        }

        while (!f.eos())
        {
            local line = f.read_line().strip();
            if (line == "" || line.find("=") == null) continue;
            local parts = line.split("=");
            if (parts.len() >= 2)
            {
                local key = parts[0].strip();
                local value = parts[1].tointeger();
                config[key] <- value;
            }
        }
        f.close();
    } catch(e) {
        print("Error reading config: " + path + "\n");
    }
    return config;
}

local game_name = fe.game_info(Info.Name);
local config_path = "M:/Media/Nintendo Entertainment System/LayoutsData/" + game_name + ".txt";
local cfg = read_config(config_path);

// Default values if not found
local snap_x = cfg.rawin("snap_x") ? cfg["snap_x"] : 100;
local snap_y = cfg.rawin("snap_y") ? cfg["snap_y"] : 100;
local snap_w = cfg.rawin("snap_w") ? cfg["snap_w"] : 640;
local snap_h = cfg.rawin("snap_h") ? cfg["snap_h"] : 480;

local art3_x = cfg.rawin("art3_x") ? cfg["art3_x"] : 0;
local art3_y = cfg.rawin("art3_y") ? cfg["art3_y"] : 0;
local art3_w = cfg.rawin("art3_w") ? cfg["art3_w"] : fe.layout.width;
local art3_h = cfg.rawin("art3_h") ? cfg["art3_h"] : fe.layout.height;

local bg = fe.add_artwork("background", 0, 0, fe.layout.width, fe.layout.height);
bg.preserve_aspect_ratio = false;

local snap = fe.add_artwork("snap", snap_x, snap_y, snap_w, snap_h);
snap.preserve_aspect_ratio = true;

local art3 = fe.add_artwork("artwork3", art3_x, art3_y, art3_w, art3_h);
art3.preserve_aspect_ratio = true;
```

---
### ❌ Issue I'm facing

The layout loads the background and artworks, **but it seems to ignore the values from the config files.** No error appears, but the coordinates from the `.txt` file aren’t being applied.

If I hardcode the values, it works. If I try to apply them dynamically via `.x`, `.y`, `.width`, `.height` after `add_artwork`, then nothing shows.

---
### 🔍 What I tried that **didn’t work**:

```nut
local snap = fe.add_artwork("snap", 0, 0, 640, 480);
if ("snap_x" in cfg) snap.x = cfg["snap_x"];
if ("snap_y" in cfg) snap.y = cfg["snap_y"];
if ("snap_w" in cfg) snap.width = cfg["snap_w"];
if ("snap_h" in cfg) snap.height = cfg["snap_h"];
```

Same thing for `art3`. Nothing is rendered.

---
### 💬 What I'm looking for

I just want to read position values from `.txt` files per game and dynamically apply them to `add_artwork` objects. If someone has done something similar or sees what's wrong, I’d really appreciate any help or working example.

Thanks a lot!