#Better way to track an object in code

13 messages · Page 1 of 1 (latest)

lyric knot
#

What my project is doing is spawning in objects using instance_create_depth and then I'm checking to see which is which using a switch statement, I then transfer oBattleUIPopup to that unit's x and and y.

{
    case 100010:
        oBattleUIPopup.x = unit.x;
        oBattleUIPopup.y = uiY;
    break;
    case 100011:
        oBattleUIPopup.x = unit.x;
        oBattleUIPopup.y = uiY;
        break;
    case 100012:
        oBattleUIPopup.x = unit.x;
        oBattleUIPopup.y = uiY;
        break;
    case 100013:
        oBattleUIPopup.x = unit.x;
        oBattleUIPopup.y = uiY;
    break;    
}```
This method has proven fickle and every time I add an object to a scene it breaks, because the id changes. So my question is what is a better way to track which object is which?
worthy plinth
#

instance_create_depth returns the instance id

#

so you can store it in a variable like
unit = instance_create_depth(x, y, 0 oUnit); and unit will contain the id of that instance

lyric knot
#

The problem now is that the x doesn't change depending on turn, I'm try to use this as a turn indicator, this is what I have set up now cause it has to get passed through the partyunits for the system to work. for (var i = 0; i < array_length(global.party); i++) { unit = instance_create_depth(uix+uiw*i,uiy,unitDepth,oBattleUI,global.party[i]); partyUnits[i] = unit; array_push(units, partyUnits[i]); }
Is this wrong? When I don't do it this way oBattleUIPopup goes to the default x and y.
I'm trying to mod this tutorial. https://www.youtube.com/watch?v=sq3x_3P0hBM&t=570s

▶️ Support my work: https://www.patreon.com/shaunjs
▶️ Source Code & base files: https://shaunjs.itch.io/shauns-turn-based-battle-system
▶️ EPISODE 4: (coming soon)
▶️ PLAYLIST: https://www.youtube.com/playlist?list=PLPRT_JORnIurSiSB5r7UQAdzoEv-HF24L

Part 3: Battle State flow - The main battle loop and how to go from one turn to the next.

Ful...

▶ Play video
worthy plinth
#

Can you show your code for setting the x and y as well?

lyric knot
#
uiy = room_height-sprite_get_height(sBattleUI)/2;
uiY = room_height-sprite_get_height(sBattleUI);
uiw = sprite_get_width(sBattleUI);```
worthy plinth
#

Looks like that is the same x and y every time, so it wouldn’t be affected by the unit

#

Also that code doesn’t change the x and y of the pop up regardless

lyric knot
#

It worked before, when I was just using the straight ids, but I think I misunderstood you, uix and uiy set the x and y of the ui getting spawned in using this for (var i = 0; i < array_length(global.party); i++) { unit = instance_create_depth(uix+uiw*i,uiy,unitDepth,oBattleUI,global.party[i]); partyUnits[i] = unit; array_push(units, partyUnits[i]); } but the popup sprite gets moved around using this

{
    case 100010:
        oBattleUIPopup.x = unit.x;
        oBattleUIPopup.y = uiY;
    break;
    case 100011:
        oBattleUIPopup.x = unit.x;
        oBattleUIPopup.y = uiY;
        break;
    case 100012:
        oBattleUIPopup.x = unit.x;
        oBattleUIPopup.y = uiY;
        break;
    case 100013:
        oBattleUIPopup.x = unit.x;
        oBattleUIPopup.y = uiY;
    break;    
}```
worthy plinth
#

Is that switch actually doing anything? It runs the exact same code for every case

#

looks like unit is just set to the most recent unit you created in that loop

#

or no, the most recent oBattleUI

#

Which unit are you wanting to move the popup to at any given time? The one who's turn it currently is?