• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

Spawning an object near instances of same objects

Bish0p

Member
I'm using this code :

GML:
with(obj_water)
{
    if(keyboard_check_pressed(vk_space) && place_meeting(obj_player.x,obj_player.y, obj_water))
    {
        if(instance_exists(obj_raftex))
        {
            instance_destroy(obj_raftex)
        }
        else
        {
            instance_create_layer(obj_water.x, obj_water.y, "Instances", obj_raftex)
        }
    }
}
I want to spawn the Raftex object to the touched instance of water object. i don't know how to spawn it to the instances x and y
For now the spawn is available for only one water instance and it spawns the raft to another instance
 

Binsk

Member
place_meeting lets you know if there was an instance at a place. instance_place lets you know WHAT instance was at a place.

What you want to do is call instance_place and store the result in a variable. When you create your object you use that variable in place of the object name and it will create it at that exact object.

For example, you code would be:
GML:
with(obj_water)
{
    // Store the object at the specified location.
    // If no object is there, the function returns the keyword noone
    var instance = instance_place(obj_player.x,obj_player.y, obj_water)
    if(keyboard_check_pressed(vk_space) && instance != noone)
    {
        if(instance_exists(obj_raftex))
        {
            instance_destroy(obj_raftex)
        }
        else
        {
            // Spawn at our stored instance's location:
            instance_create_layer(instance.x, instance.y, "Instances", obj_raftex)
        }
    }
}
 

Nidoking

Member
with(obj_water)
You don't want this. If you're using instance_place, then you don't want to do this once for every obj_water in the room. If you use the with, then you want place_meeting, but you use the instance's own x and y. If the phrase "obj_water.x" or "obj_water.y" appears anywhere in your code, you're doing something wrong and need to learn the difference between objects and instances.

Also, are you really intending to check whether the obj_water meets an obj_water when it's placed where the obj_player is? Or did you want to know whether the obj_player meets an obj_water? Because you're doing the first thing, in both cases.
 

Bish0p

Member
Actually it works better with the with(obj_water). but it doesn't work for the first instance of the object. the 1st one is considered object,but not instance?
without the with(obj_water) it is working for some of the instances, but the character have to be in a specific direction
 

ParodyKnaveBob

The Laughing Rogue
I can't help but ask...
Bish0p, did you read the tutorial on the difference between objects and instances that Nidoking linked? (It sounds like you didn't, but ~shrug~ ...)
 

Nidoking

Member
Actually it works better with the with(obj_water). but it doesn't work for the first instance of the object. the 1st one is considered object,but not instance?
without the with(obj_water) it is working for some of the instances, but the character have to be in a specific direction
Actually, it doesn't work better - you're creating a false equivalence between functionality that's coincidentally closer to what you want and a specific mistake. When you've written this correctly, then it will work better.
 
Top