• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED ds_grid data not updating after being changed.

flyinian

Member
so, after several hours of trial and error. It appears that I got a system working.

It will randomly find a target and apply damage to it and update the grid with the new information.

i'm sure ill run into more problems.

If you see a problem with the code, please let me know.

Thank you.


GML:
var damage = 1;

var range = irandom_range(1,10);  // max range would be grid height. havent implemented yet.


var target = ds_grid_get(enemy,1,range);  // uses the range value to find a target from the grid's column.



    if (target >= damage)
    {
    target -= 1; // apply damage to target
   
    ds_grid_set(enemy,1,range,target); // update the value in the grid with new value.

    }
    else
    {
        // less health than damage
    };
   
    if (damage <= 0)
    {
        // if there is no more damage.
    };

};






The original issue I had wasn't what I thought it was.

Here is my target selection and apply damage code.

The issue I have is that the grid value isn't being updated when changed.

The damage is still being applied but the new value is being discarded and not updating the grid position.

GML:
find = "no target";
show_debug_message("find: " + string(find));

var find = choose(enemy[# 1,1],enemy[# 1,2],enemy[# 1,3],enemy[# 1,4],enemy[# 1,5],enemy[# 1,6],enemy[# 1,7],enemy[# 1,8],enemy[# 1,9],enemy[# 1,10]);
  
show_debug_message("find: " + string(find));

show_debug_message("target health: " + string(find));


var damage = 1;


    if (find >= damage)
    {

// location that I am having the issue at.
    find += damage;   // This way doesn't work.
    enemy[# 1,1] += 10;  // this way works.



    show_debug_message("target health after: " + string(find));
    }
    else
    {
        // less health than damage
    };
  
    if (damage <= 0)
    {
        // if there is no more damage.
    }
;





I pulled data from a ds_grid and stored it into a ds_list so, I can shuffle and change the values stored in said list.

Now, since I am done shuffling and changing the values within the said list, I want to update the grid with the new data in the list.

How would I achieve this?

either code example or a source should do.

Thank you.






off topic question: could I avoid using a list and shuffle a specific column in a grid? I saw there is a function to shuffle the entire grid but, that's not what I want. Maybe define the height of a column to be shuffled and use irandom?
 
Last edited:

Let's Clone

Member
You could write your own shuffle. Nested loops allow you to look at every cell in the grid. With that you can look at a specific row or column and do with it whatever you need.

If you're more specific about what you need then I can look into writing out a more complete solution for you. But it will likely be more pseudo-code, since it wouldn't do much for learning if it were just copy-paste =P
 

flyinian

Member
You could write your own shuffle. Nested loops allow you to look at every cell in the grid. With that you can look at a specific row or column and do with it whatever you need.

If you're more specific about what you need then I can look into writing out a more complete solution for you. But it will likely be more pseudo-code, since it wouldn't do much for learning if it were just copy-paste =P
Would this be something you were thinking of ? or am I on the right track at least?

Here is my attempt at using a grid for storing and randomizing data.

Its a code snippet from the rest of my combat system.

I stuck "choose" into the code and it appears it randomizes the data in the grid height.

Haven't done much testing with it yet.

I still need to implement a system that I can control how many targets it apply damage to.

Code:
var gridsize = ds_grid_height(enemy) -1;

for (var aa = 1;aa < gridsize; aa++)
{
    show_debug_message(aa);
  
var    target = ds_grid_get(enemy, 1, choose(aa));
var damage = 10;
    show_debug_message("target: " + string(target));
    if (target >= damage)
    {
    target -= damage; 
    };
  
    show_debug_message("target after: " + string(target));
};
 
Last edited:

FrostyCat

Redemption Seeker
This is an easy two-step process that Let's Clone has already laid out for you:
  1. Create a script that takes a grid and two row numbers as arguments, that swaps the contents of the two specified rows.
  2. Implement the linear shuffle algorithm, reusing the row-swap script from the first step.
Didn't you say you wanted exercises in for loops? Now is the time for you to put your money where your mouth is. Each of these is a single for loop.
 

flyinian

Member
This is an easy two-step process that Let's Clone has already laid out for you:
  1. Create a script that takes a grid and two row numbers as arguments, that swaps the contents of the two specified rows.
  2. Implement the linear shuffle algorithm, reusing the row-swap script from the first step.
Didn't you say you wanted exercises in for loops? Now is the time for you to put your money where your mouth is. Each of these is a single for loop.
This looks like a challenge and a half. I'll keep this in mind when I do the assignments I've been given.

I got a shuffle system in place and it appears to work as desired. My issue is that I can't figure out how to update the grid with new data that is stored in a ds list.

I'll update op tomorrow with current code.
 

flyinian

Member
The original issue I had wasn't what I thought it was.

Here is my target selection and apply damage code.

The issue I have is that the grid value isn't being updated when changed.

The damage is still being applied but the new value is being discarded and not updating the grid position.

GML:
find = "no target";
show_debug_message("find: " + string(find));

var find = choose(enemy[# 1,1],enemy[# 1,2],enemy[# 1,3],enemy[# 1,4],enemy[# 1,5],enemy[# 1,6],enemy[# 1,7],enemy[# 1,8],enemy[# 1,9],enemy[# 1,10]);
    
show_debug_message("find: " + string(find));

show_debug_message("target health: " + string(find));


var damage = 1;


    if (find >= damage)
    {

// location that I am having the issue at.
    find += damage;   // This way doesn't work.
    enemy[# 1,1] += 10;  // this way works.



    show_debug_message("target health after: " + string(find));
    }
    else
    {
        // less health than damage
    };
    
    if (damage <= 0)
    {
        // if there is no more damage.
    }
;
 

Nidoking

Member
There is no such thing as reference variables in GML. "find" has no idea where its value came from. You didn't give it a grid position - you gave it a number. Nothing you do to that number will ever have any effect on the original grid.
 

flyinian

Member
There is no such thing as reference variables in GML. "find" has no idea where its value came from. You didn't give it a grid position - you gave it a number. Nothing you do to that number will ever have any effect on the original grid.
I updated the op with the current status of the code.

btw, Thank you for the help.
 
Top