Jump to content


Photo

variables


  • Please log in to reply
28 replies to this topic

#1 gIonescu

gIonescu

    GMC Member

  • New Member
  • 31 posts
  • Version:GM8

Posted 17 April 2012 - 12:00 PM

Basically what I wanna do is a simple platform game.

And i've been struggling with 1 simple thing.

When player_1 hits chest then i want a gem to appear on top of that chest.

But i have 6 chests on this level. and i'm stuck :(
and i also have different coloured gems.
  • 0

#2 graves23

graves23

    Artist Solo Queue

  • New Member
  • 35 posts
  • Version:GM8

Posted 17 April 2012 - 12:04 PM

Is it possible to provide an example file?
  • 0

#3 gIonescu

gIonescu

    GMC Member

  • New Member
  • 31 posts
  • Version:GM8

Posted 17 April 2012 - 12:04 PM

what do you mean?
  • 0

#4 graves23

graves23

    Artist Solo Queue

  • New Member
  • 35 posts
  • Version:GM8

Posted 17 April 2012 - 12:07 PM

Nothing...

anyway give me more details about your game I 'll help.
  • 0

#5 gIonescu

gIonescu

    GMC Member

  • New Member
  • 31 posts
  • Version:GM8

Posted 17 April 2012 - 12:11 PM

i basically have 1 player (George)
one object called (chest) and i set it to change its sprite when in collision with (George)
that's sorted and easy.
But i want to place a another object on top of the chest (gem) and i want it to make it visible, when George hits the chest.

but the problem is that i have more chests on the level. and only one object for it. and separate object for the gems.
  • 0

#6 TerraFriedSheep

TerraFriedSheep

    GMC Member

  • GMC Member
  • 2826 posts
  • Version:GM:Studio

Posted 17 April 2012 - 12:14 PM

When player_1 hits chest then i want a gem to appear on top of that chest.


First things first, lets think about how we can do this?

We have a simple situation where the player collides with the chest object, and we want a gem to appear above the chest. However, we only want the chest to produce 1 gem, so we need a variable to let us know if the chest has made a gem or not.

In the create event of the chest object, put:

has_gem=1;//Does this chest have a gem? 1 = yes, 0 = no

Now, in the player objects collision event with the chest, we should check if the chest has a gem in it or not, and if it does, create a gem above it:

if other.has_gem=1//If the chest we have collided with has a gem in it
{
other.has_gem=0;//It no longer has a gem in it
instance_create(other.x,other.y-32,obj_gem);//Create a gem object above the chest
}

And thats it. Each chest will now create an obj_gem above it when the player collides with it, but only once. See if you can get this working, and then we'll think about how to make each gem created be a different colour :)
  • 1

#7 gIonescu

gIonescu

    GMC Member

  • New Member
  • 31 posts
  • Version:GM8

Posted 17 April 2012 - 12:24 PM

right... i did that but look. i dnt understand it.



___________________________________________
ERROR in
action number 1
of Collision Event with object chest
for object George:

Error in code at line 4:
instance_create(other.x,other.y-32,obj_gemP);//Create a gem object above the chest
^
at position 37: Unknown variable obj_gemP

i named my gem "gemP" btw.
  • 0

#8 TerraFriedSheep

TerraFriedSheep

    GMC Member

  • GMC Member
  • 2826 posts
  • Version:GM:Studio

Posted 17 April 2012 - 12:27 PM

right... i did that but look. i dnt understand it.



___________________________________________
ERROR in
action number 1
of Collision Event with object chest
for object George:

Error in code at line 4:
instance_create(other.x,other.y-32,obj_gemP);//Create a gem object above the chest
^
at position 37: Unknown variable obj_gemP

i named my gem "gemP" btw.


It is a common naming convention used in GM (thanks to the old YYG tutorials, I believe) to start a resource name with a prefix representing what that resource is. So for example, if I made a gem object, I might call that object obj_gem, to represent that it is an object, and associate a sprite called spr_gem with it, so that I don't get confused when calling their names in code.

The arguments of the instance_create are as follows:

instance_create(x,y,object)

where x is the x position to create the object, y is the y position to create the object, and object is the name of the object you want to create.

So in your case, you want to put "gemP" where I pur "obj_gem".
  • 1

#9 gIonescu

gIonescu

    GMC Member

  • New Member
  • 31 posts
  • Version:GM8

Posted 17 April 2012 - 12:33 PM

ok i did that. it doesn't come up with an error. but i think you dnt understand what i ment.

i placed obj_gem on top of the chest, its set to be invisible, so what i want is when George collides with chest i want the obj_gem to become visible.

i could create 6 different chest
and 6 different gems with different colours. Right?

so basically i need one code for one chest. and unfortunately this doesn't work.
  • 0

#10 TerraFriedSheep

TerraFriedSheep

    GMC Member

  • GMC Member
  • 2826 posts
  • Version:GM:Studio

Posted 17 April 2012 - 12:40 PM

ok i did that. it doesn't come up with an error. but i think you dnt understand what i ment.

i placed obj_gem on top of the chest, its set to be invisible, so what i want is when George collides with chest i want the obj_gem to become visible.

i could create 6 different chest
and 6 different gems with different colours. Right?

so basically i need one code for one chest. and unfortunately this doesn't work.


Ah okay, well if you are going to use the method of revealing a gem when you collide, then what you want to do is set the obj_gem visible variable to true when you collide with the chest:

obj_gem.visible=1;

  • 2

#11 gIonescu

gIonescu

    GMC Member

  • New Member
  • 31 posts
  • Version:GM8

Posted 17 April 2012 - 12:50 PM

omg i thought this would be difficult.. anyway thanks..

now that this is covered... i just thought of something.. it 's complicated. and i dnt know if it will work.

lets say that he gets the gun in level 1. and level 2 he shoots the chests.

do you know what i mean? and i want George to have a limited amount of bullets like 3 for example.
  • 0

#12 TerraFriedSheep

TerraFriedSheep

    GMC Member

  • GMC Member
  • 2826 posts
  • Version:GM:Studio

Posted 17 April 2012 - 12:58 PM

omg i thought this would be difficult.. anyway thanks..

now that this is covered... i just thought of something.. it 's complicated. and i dnt know if it will work.

lets say that he gets the gun in level 1. and level 2 he shoots the chests.

do you know what i mean? and i want George to have a limited amount of bullets like 3 for example.


Again, a variable will be our friend here, and the instance_create code I mentioned earlier.

Depending on what you want to press to create a bullet depends on where you place this code, but I'll talk you through a simple situation:

George's create event:

bullets=3;//Give George 3 bullets

//George's Spacebar pressed event
if bullets>0//If George has some bullets
{
i=instance_create(x,y,obj_bullet);//Create a bullet at George's x,y coordinates
i.speed=10;//Give the bullet a speed of 10
i.direction=direction;//Make the bullet travel in the same direction as George*
bullets-=1;//Take away 1 bullet from George's remaining bullets
}

*This will only work if you are using speed (hspeed, vspeed) to move George, as the direction automatically changes with these. If you're not, let me know and we'll discuss a different technique to tell the bullet which direction to travel.


So now we have created a bullet, the process is much the same as with George colliding with the chest. The only difference is that when the bullet hits the chest, we may want to destroy the bullet. In this case:

obj_bullet collision with chest object
obj_gem.visible=1;
instance_destroy();//Destroy the bullet

Edit: Missed a }.

Edited by Soulsnatcher, 17 April 2012 - 12:59 PM.

  • 2

#13 gIonescu

gIonescu

    GMC Member

  • New Member
  • 31 posts
  • Version:GM8

Posted 17 April 2012 - 01:16 PM

thanks in my case i dnt use vspeed and hspeed i use simple key press commands for the player. it still works.
i made my George change its sprite so he looks left if i press A and so on. and i also want the bullet to change the sprite from left to right. i created diff sprites for the bullet btw.
  • 0

#14 TerraFriedSheep

TerraFriedSheep

    GMC Member

  • GMC Member
  • 2826 posts
  • Version:GM:Studio

Posted 17 April 2012 - 01:21 PM

thanks in my case i dnt use vspeed and hspeed i use simple key press commands for the player. it still works.
i made my George change its sprite so he looks left if i press A and so on. and i also want the bullet to change the sprite from left to right. i created diff sprites for the bullet btw.


Ok, so lets say that when George is looking left, his sprite index is spr_left, and when he is looking right, his sprite_index is spr_right. Also say that there are 2 bullet sprites: spr_bullet_left and spr_bullet_right. When we create the bullet, we can put this:

if bullets>0//If George has some bullets
{
i=instance_create(x,y,obj_bullet);//Create a bullet at George's x,y coordinates
i.speed=10;//Give the bullet a speed of 10
    if sprite_index=spr_left//If George is facing left
    {
    i.direction=180;//Set the bullets direction to left
    i.sprite_index=spr_bullet_left;//Set the bullets sprite to the left facing bullet
    }
    else
    if sprite_index=spr_right//else if George is facing right
    {
    i.direction=0;//Set the bullets direction to right
    i.sprite_index=spr_bullet_right;//Set the bullets sprite to the right facing bullet
    }
bullets-=1;//Take away 1 bullet from George's remaining bullets
}

Just replace spr_left, spr_right, spr_bullet_left and spr_bullet_right with your respective sprites, and this should work nicely :)

Edit: Missed a "right".

Edited by Soulsnatcher, 17 April 2012 - 01:22 PM.

  • 2

#15 gIonescu

gIonescu

    GMC Member

  • New Member
  • 31 posts
  • Version:GM8

Posted 17 April 2012 - 01:37 PM

will there be an easier way of changing the sprite of the gem? but for different chests.. instead of creating 6 different chests for 6 different gems.
  • 0

#16 TerraFriedSheep

TerraFriedSheep

    GMC Member

  • GMC Member
  • 2826 posts
  • Version:GM:Studio

Posted 17 April 2012 - 01:50 PM

will there be an easier way of changing the sprite of the gem? but for different chests.. instead of creating 6 different chests for 6 different gems.


So do you mean you only have 1 chest object of which you place 6 instance of that object in the room? And likewise, have 1 gem object of which you place 6 of in the room above the chests?

There is a method you could use.

At the point the chest opens (that is, when the player touches it, or the bullet hits it), instead of revealing the specific gem to that chest, you can reveal the nearest gem to it:

//chest opening code
with instance_nearest(x,y,obj_gem)
{
visible=1;//Set the nearest gem to this chest to be visible
}

Now you have 6 instance of obj_gem placed in the room, but you want each of these to be a different sprite. In the room editor, if you right click on an obj_gem, you will see a pop-up menu appear. In that menu, click "Creation code...". This opens up a code window which will run when that specific instance is created.

So in each obj_gem you place in the room, you want to give each one a different sprite_index. So in that "creation code" in the room editor, place this:

sprite_index=spr_gem_red;//Make this gem's sprite be the red gem sprite

Repeat this for each gem in the room, such that each one is given a different sprite, and hence a different colour.

Now when a chest opens, it will only reveal the nearest gem to it, and you only need to have 1 chest object and 1 gem object in the game.
  • 1

#17 gIonescu

gIonescu

    GMC Member

  • New Member
  • 31 posts
  • Version:GM8

Posted 17 April 2012 - 02:01 PM

when the bullet hits a chest all the gems become visible.
  • 0

#18 TerraFriedSheep

TerraFriedSheep

    GMC Member

  • GMC Member
  • 2826 posts
  • Version:GM:Studio

Posted 17 April 2012 - 02:03 PM

when the bullet hits a chest all the gems become visible.


Does it work for when the player touches the chests?
  • 1

#19 gIonescu

gIonescu

    GMC Member

  • New Member
  • 31 posts
  • Version:GM8

Posted 17 April 2012 - 02:07 PM

yeah :)

i placed the code in the collision with the player and it worked. but it doesn't work for where i placed the code in the collision with the bullet
  • 0

#20 TerraFriedSheep

TerraFriedSheep

    GMC Member

  • GMC Member
  • 2826 posts
  • Version:GM:Studio

Posted 17 April 2012 - 02:07 PM

yeah :)


Ok, so the code we had before when the bullet hit the chest was:

obj_gem.visible=1;
instance_destroy();//Destroy the bullet

Did you change this to become:

with instance_nearest(x,y,obj_gem)
{
visible=1;
}
instance_destroy();//Destroy the bullet

Edit: Missed a /code.

Edited by Soulsnatcher, 17 April 2012 - 02:08 PM.

  • 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users