Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/05/23 14:22
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
2 registered members (3run, AndrewAMD), 623 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
String array trouble that doesn't make any sense #345095
10/23/10 08:16
10/23/10 08:16
Joined: Oct 2010
Posts: 5
M
maddoctor Offline OP
Newbie
maddoctor  Offline OP
Newbie
M

Joined: Oct 2010
Posts: 5
I'm rather new in Lite-C programming and I'm trying to create my first game.

This is my current problem:
I want to create a list of strings that will store commands given by my players while they play my game. It's a turn-based game currently played on the SAME computer, no multiplayer implementation yet.
The list will display the last 2 commands only and using two small arrows the player will be able to scroll through the command list to see his past commands.
I decided to implement this using 2 string arrays and the arrows will change the array index to display command [index] and [index-1]. It works very well except the strings inside my arrays are not stored properly.
The code below is a good example of the problem I'm facing (I decided to omit the rest of the game functions):

#include <acknex.h>
STRING* player1_str[100]; // history for player1
STRING* player2_str[100]; // history for player2
STRING* input_str = "command 10"; //first command given by player1

function main()
{
initialize_history(); //this initializes the 2 arrays into empty strings

player1_str[0] = _chr(input_str); //store current command
printf(_chr(player1_str[0])); //"command = 10" is stored
wait(1);
str_cpy(input_str, "command = 20"); //
player2_str[0] = _chr(input_str);
printf(_chr(player1_str[0])); //should still be "command = 10" but unfortunately it's "command = 20" which doesn't make any sense to me, when did I change the contents of player1_str array?
}

In other words when I change the contents of input_str, the contents on BOTH arrays change and it's really troubling me.

switching
str_cpy(input_str, "command = 20");
with
input_str = "command = 20";
player2_str[0] = input_str;
solved the problem, the contents of player1_str don't change, but I can't use this option because in the real game input_str takes input from the keyboard with a inkey(input_str) command so no input_str = "" will work.

PS: Before anyone mention it, I've read about chat boxes in other posts and I've seen workshops about them but unfortunately they store and display all commands in one big string. That option is a no no for me as I want to be able to scroll through the list of commands, because only 2 will be displayed at any given time.


Last edited by maddoctor; 10/23/10 08:18.
Re: String array trouble that doesn't make any sense [Re: maddoctor] #345096
10/23/10 08:19
10/23/10 08:19
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
The problem is that you don't copy the content of the input string into the other string, but you set the pointer of the string to the input string.
str_cpy() is the command you are looking for.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: String array trouble that doesn't make any sense [Re: WretchedSid] #345098
10/23/10 08:24
10/23/10 08:24
Joined: Oct 2010
Posts: 5
M
maddoctor Offline OP
Newbie
maddoctor  Offline OP
Newbie
M

Joined: Oct 2010
Posts: 5
You mean:
str_cpy(player2_str[0], _chr(input_str));
If I give this command I get a runtime error saying "Invalid Arguments". I suspect there is a problem with using string array contents in functions. Any ideas how the above could work?

PS: Thanks for the very quick reply laugh

Last edited by maddoctor; 10/23/10 08:24.
Re: String array trouble that doesn't make any sense [Re: maddoctor] #345099
10/23/10 08:33
10/23/10 08:33
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Yes, this is normal because str_cpy() expects a pointer to an STRING object and you gave it just an STRING.
I don't think that you want 100 player1 strings, so you can remove the [0]. (The Lite-C STRING datatype is an opaque layer over the actual CString array and you don't need one STRING object to store one character but can user one STRING object to hold an unlimited* amount of characters).

However, of you want 100 player1 and 2 strings, you need 100 pointer to 100 STRING objects instead of one pointer to a memory area that is able to hold 100 STRING objects.

This would be the way to go:
Code:
STRING **player1_str[100];
STRING **player2_str[100];



And then in your code:
Code:
str_cpy((player1_str)[0], input_str); // No need to pass an CString as second argument, str_cpy is able to handle STRINGs!



As you can see, I wrapped those () around the player1_str, this is because the Lite-C compiler can't handle those references itself but needs some kind of "hint". You have to adopt this in your init code, otherwise it won't compile.


Edit: Another way:
Code:
str_cpy(&player1_str[0], input_str); // Just pass a pointer to the memory area



*unlimited = until you run out of memory of course.

Last edited by JustSid; 10/23/10 08:34.

Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: String array trouble that doesn't make any sense [Re: WretchedSid] #345100
10/23/10 08:45
10/23/10 08:45
Joined: Oct 2010
Posts: 5
M
maddoctor Offline OP
Newbie
maddoctor  Offline OP
Newbie
M

Joined: Oct 2010
Posts: 5
But if I use only one string (which is surely better memory-wise) how can I use the "scroll" function on my buttons? I was going to change the array index with every click of the arrow buttons and display the contents, is there a way to do it with 1 string? (change the "index" of a single string)

Unfortunately
STRING **player1_str[100];
str_cpy((player1_str)[0], input_str);

This still gives a runtime error: Error E1515 ("Invalid arguments in main").
I'm using the A8 free version and SED version: 7.36.1

Re: String array trouble that doesn't make any sense [Re: maddoctor] #345102
10/23/10 08:54
10/23/10 08:54
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Ah okay, I see what you want, sorry I thought this would be something like the name or so. My bad.
100 Strings shouldn't be a problem, I don't know how much memory they consume but I guess it isn't more than 30 bytes for an empty string (which would be really much for such an simple object).
However, if you still have concerns about your memory, you could use an dynamic array that grows with your input. So if the user just types eg. 50 messages(?!), you only allocate memory for 50 Strings instead of the 100.

About the error: Sorry, I can't see what the compiler is complaining about and I don't have access to an GameStudio installation to test it atm. But I will give it a try later this day if no one else can find the error in the meantime.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: String array trouble that doesn't make any sense [Re: WretchedSid] #345108
10/23/10 09:57
10/23/10 09:57
Joined: Oct 2010
Posts: 5
M
maddoctor Offline OP
Newbie
maddoctor  Offline OP
Newbie
M

Joined: Oct 2010
Posts: 5
A new question: How can I create a dynamic array in lite-c?

And: Is there a way to make a scrolling textbox without using my String arrays?


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1