issues with a very basic "move and collision" script

Hi to all. forgive me but i don't find the issue in the block of script for "automatic movement" of instance in game. what i don't understand are :
1- the code are a basic code for movement that i have find on video tutorial online.
GML:
        /*velocita*/    speed = calcola_velocita_orizzontale(orspeed,bonusvelocita, malusvelocita);
        //    show_debug_message("velocitàprima :"+string(velocita));
        if place_meeting(x+speed, y, o_BaseMuro)
        {
        //    show_debug_message("velocità :"+string(velocita));
            while not place_meeting(x+speed, y, o_BaseMuro)
            {
                   x = x + speed;           
            }
            
                if (speed > 0)
                {
                    speed = -speed;
                //    show_debug_message("velocitàoltre :"+string(velocita));
                }
                else speed = -speed;       
                
        }   
        
        if (speed !=0) image_xscale=sign(speed);
2- in a previous project of game for testing codes, this specific code don't exit issues.
3- if i change name of my variables with the built-in variable "speed" the code go good for an half of this complete cycle.

any suggestions?
 

Nidoking

Member
if (speed > 0)
{
speed = -speed;
// show_debug_message("velocitàoltre :"+string(velocita));
}
else
speed = -speed;
Well, for a start, this is exactly the same as "speed = -speed;"

That suggests to me that you don't understand what this is doing. You should work that out.
 

FrostyCat

Redemption Seeker
Also, since this is manually handled speed, you should not use the name speed. That's a built-in variable that triggers automatic movement. If the code came from a tutorial, it would have had a different name such as xsp, hspd or some variation of it --- except for hspeed (which also triggers automatic movement).
 
i'm from Italy.
the intention is create an instance "enemy" wandering in the room colliding walls or empty spaces and then changes direction.
the code that satisfy this request are this :
GML:
switch (movimento)
{
    case movimento.zero:
    {   
        muroadiacente = sprite_width;
        pavimentoadiacente = sprite_height;
        if sprite_width > 32  muroadiacente = 32; //non so il motivo ancora ma se imposto un valore maggiore
        //l'oggetto scende dalla piattaforma.
        if sprite_height > 32 pavimentoadiacente = 32; //idem come sopra
        
        if stato=="riposo"
        {
                if (place_meeting(x+sign(speed),y,oFloor)) //se sbatte contro un ostacolo muro torna indietro
                {
                    while (!place_meeting(x+sign(speed),y,oFloor))
                    {
                        x = x+sign(speed);
                    }
    
                    if (speed > 0)
                    {
                        speed = -speed;
                    }
                }
                
                    spostamentoverticale = spostamentoverticale + accelerazionedigrav; // di base il movimento verso il basso
                    // è dato solo dall'accelerazione di gravità impostata da me
                    
                if (place_meeting(x,y+spostamentoverticale,oFloor)) //se sbatte contro un ostacolo muro torna indietro
                {
                    while (!place_meeting(x,y+sign(spostamentoverticale),oFloor))
                    {
                        y = y+sign(spostamentoverticale);
                    }
                    spostamentoverticale = 0;
                    
                }               
                
                    y = y + spostamentoverticale;   

        if (!place_meeting((x+muroadiacente),(y+pavimentoadiacente),oFloor)) //in pratica controlla se c'è del vuoto nello spazio
//adiacente a sè stesso e torna in automatico indietro. se fosse impostato un valore
        {
            // code here
            if (speed > 0)
            {
                speed = -speed;
            }
        }   
    }

    }
    break;
in this code i've adding a condition in order to "collide" with empty spaces. this code have no issues and i've seen in a video of Shawn Spalding.
 
Also, since this is manually handled speed, you should not use the name speed. That's a built-in variable that triggers automatic movement. If the code came from a tutorial, it would have had a different name such as xsp, hspd or some variation of it --- except for hspeed (which also triggers automatic movement).
no, in beginning i wanted to use local variable var "_velocita" or an instance variable "velocita", but the result i got was that object "enemy" run to collide with the first wall instance and did not turn back OR didn't move at all. u can see it in video i linked
 

Roleybob

Member
Spiacente ma per mi il tuo codice non ha senso.

Qual è il comportamento atteso con il nemico? E dov'è il tutorial completo da Shawn Spalding (in inglese per favore)?

Sono stanco, spero tornare domani
 
Spiacente ma per mi il tuo codice non ha senso.

Qual è il comportamento atteso con il nemico? E dov'è il tutorial completo da Shawn Spalding (in inglese per favore)?

Sono stanco, spero tornare domani
maybe it' isn't Spalding but the example are this video
Gnight
 

Roleybob

Member
Come diceva FrostyCat, non dovresti usare "speed".

Per che non copi il codice nel tutorial?

Una problema e' che hai scritto

while not place_meeting(x+speed, y, o_BaseMuro)
{
x = x + speed;
}


ma dovrebbe essere

while not place_meeting(x+sign(speed), y, o_BaseMuro)
{
x = x + sign(speed);
}

penso che e' meglio:


GML:
h_spd = sign(h_spd) * (calcola_velocita_orizzontale(orspeed,bonusvelocita, malusvelocita));

if place_meeting(x + h_spd, y, o_BaseMuro)
{
    while !place_meeting(x + sign(h_spd), y, o_BaseMuro)
    {
        x += sign(h_spd)
    }
    h_spd *= -1;
    image_xscale *= -1;
    exit;
}
else
x += h_spd;
Se orspeed, bonusveolcita e malusvelocita possono già avere valori negativi, non è necessario inserire il segno (h_spd) * nella prima riga
 

Nidoking

Member
We seem to have abandoned the English language some time ago, but if you're saying that allowing a speed variable to be negative means that you don't need to keep multiplying by a negative number, then yes, that is how math works in any language.
 

Roleybob

Member
Would you prefer English?

I am saying that if the variables orspeed, bonusveolcita and malusvelocita are only given positive values, then you need to multiply by -1 or 1 to ensure that the instance moves in the correct direction. If those variables are given negative values for moving left and positive values for moving right then you should not multiply by -1 or 1:

h_spd = (calcola_velocita_orizzontale(orspeed,bonusvelocita, malusvelocita));

If you want to declare h_spd as a local variable (you should use local variables whenever possible) but you do want to multiply by -1 or 1, you could use image_xscale instead:

var h_spd;
h_spd = sign(image_xscale) * (calcola_velocita_orizzontale(orspeed,bonusvelocita, malusvelocita));
 
ok thnks to U (both) and to Frostycat.
the script "(calcola_velocita_orizzontale(orspeed,bonusvelocita, malusvelocita))" cannot return negative values or value <= 0. the minimum value possible it's 1 by script. the code is below:
GML:
///@arg normale
///@arg bonus
///@arg malus

var velocita = (argument0+argument1)-argument2;
    if velocita <= 0
        return 1;
    else
        return velocita;
reading the code post by Roleybob maybe i've forget an "exit;" instruction in my code ?

ok for now i've trying to add as a mathematical sign modifier the built-in variable "image_xscale" because ...i wanted to do so :D thnks to your "input" Roleybob. finally the automatic movement is such as i want

and here the code :
Code:
    case azione.alpasso:
    #region movimento standard
   
            sprite_index = SpriteCammina;
            image_speed = 1.5;  
            //statoazione = azione.corsa;
            velocita = image_xscale*calcola_velocita_orizzontale(orspeed,bonusvelocita, malusvelocita);
        //    show_debug_message("velocitàprima :"+string(velocita));
        if place_meeting(x+velocita, y, o_BaseMuro)
        {
        //    show_debug_message("velocitàqui :"+string(velocita));
            while not place_meeting(x+sign(velocita), y, o_BaseMuro)
            {
                   x = x + sign(velocita);          
            }          
            image_xscale *=-1;          
        }  
                    x = x + velocita;
        //            show_debug_message("velocità :"+string(velocita));

        if not place_meeting(x, y+1, o_BaseMuro)
        {
            verspeed = verspeed + forzadigravita;
            y += verspeed;
        }  
               
    #endregion
    break;
thanks a lot to everybody.
p.s.: i've forgot anything ?!
 
Top