• 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!

Variable not working with sprite_index

C

ccaalleebbg

Guest
I've been playing with this for like an hour and can't figure out why it isn't working. I'm working on a grid-based inventory system using arrays for a project. Dragging and dropping an item (obj_invItem) around the grid works flawlessly. Taking information from an array called global.invBackpack the obj_invItem changes everything else I want it to just fine, however when I try to reference a variable to set the sprite_index of the obj_invItem it refuses to work.

This code should switch the stored string "invItem01" gathered from the array (in "contents[0]") to "spr_invItem01," allowing me to switch the appearance of the object:

In step event of obj_invItem:
Code:
var spr = string_insert(contents[0], "spr_", 5);
sprite_index = spr;
I tested it with draw_text and it's formatted perfectly using that conversion but the sprite wont change. I figured out that it wasn't finding the variable until after the create even but even in the step event it doesn't work.

For reference here's the test code that creates the object I threw in my controller object just to test it:

Code:
if keyboard_check_pressed(vk_space) {
    var k = global.invBackpack[1,1];
    with instance_create(k.x,k.y,obj_invItem) {
        contents[0] = "invItem01";
        contents[1] = 5;
    }
idgi. Any help would be very appreciated. Cheers.
 
T

Taddio

Guest
Change the last line in the first block of code to this:

sprite_index = asset_get_index(spr);

You need that function to convert the string to the actual pointer, otherwise it's just a string. Hope that helps.
 
Top