Inventory navigation help

GoliBroda

Member
So i have o_window, and it is a window of inv screen and the selecting box in my inventory.
now i have this code in step event.

Code:
if (global.inv_true = 3)&&(draw = true) {
with instance_create(xinv,yinv,o_Windows){image_xscale=5 image_yscale = 0.36}
draw = false

}
if (down_true = 1) && (isel <= 3){
    yinv += 25
    with (o_Windows){instance_destroy()}
    draw = true
    isel += 1
}
else
{
if (up_true = 1) && (isel >= 1){
    yinv -= 25
    with (o_Windows){instance_destroy()}
    draw = true
    isel -= 1
    }
}
This is creating my selection box on xinv and yinv that i set in create event.
By pressing down i want to move that box by 25 by creating a new box and deleting an old one.
Whats my problem?
When i press down key all works fine, the cursor goes down when theres an option under and wont move when menu ends (the isel var)
But when i press up arrow it teleports above yinv var and stucks there, i cant move it anymore.

Whats wrong with it?
 
C

Chubb1337

Guest
How I understand it, is that you want to display which inventory slot is currently selected? Is there a reason you are using an object for that? Because creating and destroying objects alot will slow you game down.

Why not simply move the object itself to the new xinv and yinv, or if an object is not strictly needed (for example you only want an image scaled sprite), why not draw a sprite on the xinv and yinv instead?

draw_sprite_ext() can handle this all in one neat function.

As far as you actual question, I do not see anything wrong with your logic, it basicly says:

If pressing down and the variable isel is still smaller than or equal to 3, go down (this means it will reach 4).

Else if pressing up and the variable isel is larger than or equal to 1, go up (this means it will reach 0). The only thing I can think of is that you might not want it to limit at 0 but at 1, ifso, use "isel > 1" instead of "isel >= 1".

P.S. you don't need to use
Code:
If (....){
....
}else{
If (...){
...
}
}
You can simply do
Code:
If (....){
....
}else if (...){
...
}
Hope this helps. If what I suspected is not the problem, it might be somewhere I don't know about, in which case it would be nice to see where else the variables isel, xinv and yinv are used/edited.
 
I also suspect the bug is not in this code part alone because I don't see it (well, just because I don't see it doesn't mean it's not there though). You probably wnat to look at more code parts that might be involved.


Here a few additional thoughts not necessarly solving your issue:

create & delete
I too don't understand why you create a new object every time and delete it again. It seems a very crude solution for what you're trying to do. Just in case you do this because you don't know how to adress an instance you created in code: you can save the instance id in a variable like this:
[variable name] = instance_create([x], [y], [object name]);

In your case something like:
create event:
i_Windows = noone;

step event:
// Conditions when you want the object to be created
if (!instance_exists(i_Windows)) {
i_Windows = instance_create(xinv, yinv, o_Windows);​
}

with (i_Windows) {
// do stuff
}

and once you don't need it anymore:
with (i_Windows) {
instance_destroy();​
}

memory leak
Also this isolated code part looks very dangerously like a memory leak. Looks like you create an instance every step global.inv_true and draw are true you create an instance of o_Windows. but only when down_true or up_true are true those instances get destroyed. So my guess is as long as the inventory is open but no key is pressed this code spams out new instances of o_Windows and only if actually down or up are pressed the mess gets cleaned up but right after it's spamming new instances.

= vs. ==
In an if-statement you should never use = but == instead.
= is to set a variable and
== is to compare a variable
As for now GM:S accepts an = as a conditional but you never know if it will in the future.

end line with ;
Always and a command with a semicolon unless it's an if, for, while, with or curly bracket. Some export modules work without those semicolons. Some export modules however will throw an error for each line you didn't end with a ;

So this:
if (down_true = 1) && (isel <= 3){
yinv += 25
with (o_Windows){instance_destroy()}
draw = true
isel += 1​
}
becomes this:
if (down_true == 1) && (isel <= 3){
yinv += 25;
with (o_Windows){instance_destroy();}
draw = true;
isel += 1; // or simply isel++
}

Hope that helped a bit.
 
Top