Legacy GM FIXED! - [GM8.1] This code only returns "-1-1-1-1-1-1-1-1"- why?

Momoro

Member
Hello ๐Ÿ˜„

I've been working on a small function, for fun, and I've hit a roadblock.

So, it should be adding each character from the string into the ds_list, then adding each character back into another string, in their respective order.


GML:
///box_show(string, bool, int, int, int, int)
{
   var message, displayOnTheRight, spriteIndex, c1, c2, c3;
 
   message = argument0;
 
   displayOnTheRight = argument1;
 
   spriteIndex = argument2;
 
   c1 = argument3;
 
   c2 = argument4;
 
   c3 = argument5;
 
   //initialize box
 
   var i, word, char, num, mes, out;
 
   out = "";
 
   word = ds_list_create();
 
   for(i=1; i < string_length(message); i+=1;)
   {
       char = string_char_at(message,i);
     
       ds_list_add(word,char);
     
       for(num=1; num < ds_list_size(word); num+=1;)
       {
           mes = string(ds_list_find_index(word,real(num)));
         
           out += mes;
       }
   }
 
   return show_message(out);
 
}
For some reason, when the message shows up, it just displays "-1-1-1-1-1-1-1-1"- and I can't seem to figure out why.

..don't mind the random variables you can add with the function, they're not done yet..

Could someone help me out? Thanks ๐Ÿ˜„
 

Momoro

Member
Okay, so I did a little tweaking, and now it displays text, but not correctly.

GML:
///box_show(string, bool, int, int, int, int)
{
   var message, displayOnTheRight, spriteIndex, c1, c2, c3;
 
   message = argument0;
 
   displayOnTheRight = argument1;
 
   spriteIndex = argument2;
 
   c1 = argument3;
 
   c2 = argument4;
 
   c3 = argument5;
 
   //initialize box
 
   var i, word, char, num, mes, out;
 
   out = "";
 
   word = ds_list_create();
 
   for(i=0; i != string_length(message); i+=1;)
   {
       char = string_char_at(message,i);
     
       ds_list_add(word,char);
     
       for(num=0; num < i; num+=1;)
       {
           mes = string(ds_list_find_value(word,real(num)));
         
           out += mes;
       }
   }
 
   return show_message(out);
 
}
The text is obfuscated, and not all of the characters are inserted. Still trying to find out why..
 

kburkhart84

Firehammer Games
1. Are strings 0 based or 1 based in that old version you are using? IIRC at some point they were 1 based but then they changed to zero based, but I don't know exactly which version had that change. This may be causing part of the issue.

2. if 'num' is already a number, and it is going to be an integer(because it starts at 0 and you only add 1), then why are you adding a call to real(num) there? You don't need it because it is already a number.

3. You have the second loop inside the first...this means that you are trying to loop through a list that only has one value in it. If you put that loop outside of the other loop, it may work for you. I'm pretty sure that newer versions will give actual error messages when you try to read list values that are too high or low, but this old version may instead read the memory that doesn't belong to it which could explain the output you are getting. Since you dont show what the message is, and you don't show the output, I have no idea if that is the only issue you have.

EDIT****

Strings were 1 based then, and are still 1 based according to the manual as of GMS 2.3.1.
 
Last edited:

FrostyCat

Redemption Seeker
ds_list_find_index and ds_list_find_value are not the same. Neither are the following:
GML:
for (...) {
    ...
    for (...) {
        ...
    }
}
GML:
for (...) {
    ...
}
for (...) {
    ...
}
Think over your code again and you should see what's wrong. Also, you should remember that GML strings are one-indexed, so your first loop should start as for(i = 1; i <= string_length(message); i += 1).
 

Momoro

Member
for(i = 1; i <= string_length(message); i += 1)
It works better now, but it still doesn't show the full sentence. ex: if I tell it to display "Pokemon" it displays "okemon0"

Here's the modified code:

GML:
///box_show(string, bool, int, int, int, int)
{
   var message, displayOnTheRight, spriteIndex, c1, c2, c3;
 
   message = argument0;
 
   displayOnTheRight = argument1;
 
   spriteIndex = argument2;
 
   c1 = argument3;
 
   c2 = argument4;
 
   c3 = argument5;
 
   //initialize box
 
   var i, word, char, num, mes, out;
 
   out = "";
 
   word = ds_list_create();
 
   for(i = 1; i <= string_length(message); i += 1)
   {
       char = string_char_at(message,i);
     
       ds_list_add(word,char);
   }
 
   for(num=1; num < i; num+=1;)
   {
       mes = string(ds_list_find_value(word,num));
     
       out += mes;
   }
 
   return show_message(out);
 
}
 

Momoro

Member
Since you dont show what the message is, and you don't show the output, I have no idea if that is the only issue you have.
I do know what the intended message will be, because only I will be calling it- it's a function that the game itself will use, not the user :)

The message is determined by me- the user can view the message, but not make/send one.
 

kburkhart84

Firehammer Games
I do know what the intended message will be, because only I will be calling it- it's a function that the game itself will use, not the user :)

The message is determined by me- the user can view the message, but not make/send one.
Cool....but I was referring to showing us, here, on the GMC. Since I didn't know what you were getting, I didn't know the issue. Now, as of that last post you made, you show the string "okemon0" which tells me your issue. Going by Frosty's statement that strings are 1 based(I guess I was wrong on that), and that lists are 0 based, that is why you are missing the first letter.
 

Momoro

Member
Cool....but I was referring to showing us, here, on the GMC. Since I didn't know what you were getting, I didn't know the issue. Now, as of that last post you made, you show the string "okemon0" which tells me your issue. Going by Frosty's statement that strings are 1 based(I guess I was wrong on that), and that lists are 0 based, that is why you are missing the first letter.
It works! Here's the code:

GML:
///box_show(string, bool, int, int, int, int)
{
   var message, displayOnTheRight, spriteIndex, c1, c2, c3;
  
   message = argument0;
  
   displayOnTheRight = argument1;
  
   spriteIndex = argument2;
  
   c1 = argument3;
  
   c2 = argument4;
  
   c3 = argument5;
  
   //initialize box
  
   var i, word, char, num, mes, out;
  
   out = "";
  
   word = ds_list_create();
  
   for(i = 1; i <= string_length(message); i += 1)
   {
       char = string_char_at(message,i);
      
       ds_list_add(word,char);
   }
  
   for(num=0; num < ds_list_size(word); num+=1;)
   {
       mes = string(ds_list_find_value(word,num));
      
       out += mes;
   }
  
   return show_message(out);
  

}
I didn't know lists were 0 based ๐Ÿ˜ƒ Thanks for the info everyone!
 

kburkhart84

Firehammer Games
I didn't know lists were 0 based ๐Ÿ˜ƒ Thanks for the info everyone!
In many program languages, everything is 0 based. In GML, I think everything is as well, except for strings(which I had forgotten, and was wrong earlier about them changing at some point).
 

Momoro

Member
In many program languages, everything is 0 based. In GML, I think everything is as well, except for strings(which I had forgotten, and was wrong earlier about them changing at some point).
I actually have another question-- making it display slower?

I'm testing it in a draw event, and it displays immediately. How do I make it display each character slowly?

Since the function is inside of a Script, I can't use an alarm[]. Thanks!
 

kburkhart84

Firehammer Games
Look for a tutorial on "typewriter text" and you will find what you need.

That said, you can still use alarms, they are really just other variables...you could also send a min and max time to the script and change it so it puts the amount of characters you need based on the time passed. Either way, its going to be more complicated than a single script, as you have to have something to track time/steps.
 

Momoro

Member
Look for a tutorial on "typewriter text" and you will find what you need.

That said, you can still use alarms, they are really just other variables...you could also send a min and max time to the script and change it so it puts the amount of characters you need based on the time passed. Either way, its going to be more complicated than a single script, as you have to have something to track time/steps.
Okay, thanks! ๐Ÿ˜ƒ
 
Top