TamirGali
TamirGali

Reputation: 1

This code in game maker won't work properly:


This code does destroy the object when its health reaches 0 but it won't add 5/7 to the global.xp variable.

if rotem_hp > 1 and shop_13 = 0
{   
rotem_hp = rotem_hp -1
}
else
{
if rotem_hp > 1 and shop_13 = 1 rotem_hp = rotem_hp -1.5
if rotem_hp < 1 and shop_4 = 0 global.xp = global.xp + 5 instance_destroy()
if rotem_hp < 1 and shop_4 = 1 global.xp = global.xp + 7 instance_destroy()
}

This wont work either

if (rotem_hp > 1 and global.shop_13 = 0)
{   
rotem_hp = rotem_hp -1
}
else if (rotem_hp > 1 and global.shop_13 = 1) 
{
rotem_hp = rotem_hp -1.5
}
else if (rotem_hp < 1 and global.shop_4 = 0) 
{
global.xp = global.xp +5 
instance_destroy()
}
else if (rotem_hp < 1 and global.shop_4 = 1)
{
global.xp = global.xp +7 
instance_destroy()
}
else
{
//do nothing
}

This wont destroy the object (btw in create event i have (rotem_hp = 5)

if rotem_hp > 1 and global.shop_13 = 0
{
rotem_hp = rotem_hp -1 
}

if rotem_hp > 1 and global.shop_13 = 1
{
rotem_hp = rotem_hp -1.5
}

if rotem_hp < 1 and global.shop_4 = 0
{
global.xp = global.xp +5 
instance_destroy()
}

if rotem_hp < 1 and global.shop_4 = 1
{
global.xp = global.xp +7
instance_destroy()
}

I will appreciate any efforts to answer my question.

Upvotes: 0

Views: 201

Answers (2)

TamirGali
TamirGali

Reputation: 1

Okay, for everyone who encountered the same problem as me:

I finally managed to solve the issue, the problem was that I used:

if rotem_hp > 1
{   
rotem_hp = rotem_hp -1
}

Instead of:

if rotem_hp >= 1
{   
rotem_hp = rotem_hp -1
}

So when "rotem"'s health reachd exactly 1, the code didnt know what to do since I told it to do stuff when >1 and <1, this was such a stupid problem and I cant believe I wasted more than a few minutes to solve it, I will now go hide in the corner of my room full with shame. goodbye.

Upvotes: 0

Dmi7ry
Dmi7ry

Reputation: 1777

When you write

if rotem_hp < 1 and shop_4 = 0 global.xp = global.xp + 5 instance_destroy()
if rotem_hp < 1 and shop_4 = 1 global.xp = global.xp + 7 instance_destroy()

it means

if rotem_hp < 1 and shop_4 = 0
{
    global.xp = global.xp + 5
}
instance_destroy()

if rotem_hp < 1 and shop_4 = 1 
{
    global.xp = global.xp + 7
}
instance_destroy()

so last if will newer checked bacause the object will be already destroyed. You need use curve braces for define if scopes.

You can write like this:

if rotem_hp < 1 and shop_4 = 0
{
    global.xp += 5
    instance_destroy()
}

if rotem_hp < 1 and shop_4 = 1 
{
    global.xp += 7
    instance_destroy()
}

or if you want only one line for one 'if'

if rotem_hp < 1 and shop_4 = 0 { global.xp += 5; instance_destroy(); }
if rotem_hp < 1 and shop_4 = 1 { global.xp += 7; instance_destroy(); }

Upvotes: 2

Related Questions