Issue removing spawn point form available list after taking it

huenix

Member
I am trying to make it so a connected player spawns on an available starting spot (its a board game).
It currently works to place them at random points, but they CAN over lap players, i don't want that.

How do i remove the available spawn point after "ojb_player" is crated at x/y?


if (type == network_type_connect) {
//create a player, add the socket to the list
ds_list_add(sockets, sock)

l = ds_list_create()
n = 4 // set the number of spots
var i for (i=0; i<n; i+=1) ds_list_add(l, i)
ds_list_shuffle(l)

var i, px, py for (i=0; i<n; i+=1) {
switch ds_list_find_value(l, i) {
case 0: px = 64 py = 96 break
case 1: px = 1216 py = 96 break
case 2: px = 64 py = 896 break
case 3: px = 1216 py = 896 break
}

var p = instance_create_layer(px, py, "Instances", obj_player);
p.my_id = sock;

ds_map_add(clients, sock, p)

ready[sock] = false;

}
 

Biscotto

Member
You could solve it in many ways. I'll try to explain one (which is the easiest to understand and implement):
Enter your code an "Event User" so that you can call and execute it as many times as you need.
After you have cycled the ds_list and kept the coordinates of a spawn point, check and see if no instances of an obj_Player is present at that point. If no instances are present then I created the new Player instance, otherwise run the whole script again. The script will repeat until it finds a free spawn point.
Obviously you will repeat the whole script every time because a player already present on the map could move, and then free a spawn point. Otherwise if you don't want to repeat the whole script, you can simply remove a coordinate from the ds_list, and recheck the remaining ones.

If you have difficulty applying this simple method, we can try to fix the code together! ;)
 

huenix

Member
(spawns only happen at the start of the game, think like picking a starting point in a board game "Clue", in in this case it is random)

i was just working a idea... that sounds like what you are suggesting....

if not meeting at place of obj_player, spawn....
else "retry some how" ....???
 

huenix

Member
@Biscotto .... what would i put here "??????"

if (place_empty(px, px, obj_player)) {
var p = instance_create_layer(px, py, "Instances", obj_player);
p.my_id = sock;

ds_map_add(clients, sock, p)

ready[sock] = false;
}else{ ????????????????

}
 

Biscotto

Member
Ah, so the spawn check only happens once at the beginning of the game.
Then we can simplify things a bit:
GML:
var px, py;
for (var i = 0; i < ds_list_size(l); i ++) {
    switch (l[| i]) {
        case 0:
            px = 64;
            py = 96;
            break;
        case 1:
            px = 1216;
            py = 96;
            break;
        // Etc.....
        default:
            break;
    }
    if (!position_meeting(px, py, obj_player)) {
        var p = instance_create_layer(px, py, "Instances", obj_player);
        p.my_id = sock;
        ds_map_add(clients, sock, p)
        break;
    }
}
ready[sock] = false;
Obviously this is just a possible solution based on the code you wrote.
You could make the code leaner and faster, but it's up to you to choose the method you prefer.đź‘Ť
 
Last edited:
Top