GSVector

Posted By: pegamode

GSVector - 03/11/09 12:40

Some people asked me to publish my GSVector plugin.

So here it is:

GSVector.rar

It just provides a standard c++ Vector.

Sorry, there's no documentation yet.

So if you have questions just ask me.

Regards,
Pegamode.
Posted By: VeT

Re: GSVector - 03/11/09 12:55

Looking like very usefull thing.
Imho, it dont need documentation, everything is quite clear smile
Posted By: EvilSOB

Re: GSVector - 03/11/09 13:00

Some basic documentation would be nice....

Like, what does it actually do?
What could it be used for?
What datatypes can it store?

Once you answer I'll probable feel pretty dense. But I ask anyway...
Thanks
Posted By: KDuke

Re: GSVector - 03/11/09 19:38

Originally Posted By: EvilSOB
Like, what does it actually do?

It is used for dynamically resizing arrays.
With this vector class you can simply add, remove and insert new array elements into an array.

Originally Posted By: EvilSOB
What could it be used for?

I have been one of the guys who have asked for this plugin...
I personally use it for my ingame level editor.
When placing new objects into the level I'm using the vector class to simply add the new objects to my "world"-array which takes care of the positions and attributes of every object.
Furthermore it is usefull for saving memory and calculation time.
With this class you can remove objects out of memory while they are not in a specified range of the player. Things like model-, texture- and sounddata don't have to reside in memory if the player is outside of the visible and audible range to recognize the object.

Originally Posted By: EvilSOB
What datatypes can it store?

As far as I know every datatype available.

Hope this clarifies things ^^

By the way... please correct me if I'm in some way wrong with what I have written here pegamode.

greetings
KDuke
Posted By: VeT

Re: GSVector - 03/11/09 20:30

heh... where were you month ago, when i only started my editor? laugh
Posted By: EvilSOB

Re: GSVector - 03/12/09 01:42

OK.
KDuke: Thank you for the description, very consise and informative.
pegamode: Thank you greatly for a VERY useful tool.

(and I only feel a little bit dense, but Ive got a great new tool now)
Posted By: KDuke

Re: GSVector - 03/12/09 04:10

Oh my God!
I've been so busy answering EvilSOB's questions, I completely forgot to express my unimagable gratitude in mere words.

Thanky you extremely much for this plugin pegamode.
It saves me so much time, I don't know how I can make it up for this handy plugin.

greetings
KDuke
Posted By: pegamode

Re: GSVector - 03/12/09 08:05

Hi everybody,

good to hear that it's for you as useful as it is for me.

My current projekt is a point&click adventure and for that I wrote an adventure system as base and I don't know how I would have realize that without using a hashmap and a vector.

If there are any more questions ... ask them :-)

Regards,
Pegamode
Posted By: KDuke

Re: GSVector - 03/14/09 17:02

Hm...

now that I'm finally at the point to actually implement GSVector I'm at loss.
Although I've been using Vectors in C++ before I can't get this one running.

A documentation is really needed.

I already managed to insert several values though I only can use
Code:
getFromGSVector(myVector, 0);

As soon as I use another position the engine crashes.

greetings
KDuke
Posted By: pegamode

Re: GSVector - 03/14/09 17:29

A documentation follows in the next minutes ...

I'll also take a look at the getFromGSVector problem ... could also be a bug in the plugin. If so, I'll fix that.
Posted By: pegamode

Re: GSVector - 03/14/09 18:08

GSVector documentation:

First of all let's take a look at the header file and the functions you can use:

Function:

function createGSVector();

Description:

Creates a new GSVector object and returns a pointer to it that you have
to store in a VAR.

Parameters:

none

Function:

function destroyGSVector(vectorPtr);

Description:

Destroys the GSVector object.

Parameters:

vectorPtr (var): the pointer you got from the create function

Function:

function insertIntoGSVector(vectorPtr,value);

Description:

Inserts a new entry to the vector. The entry is added at the end of the list.

Parameters:

vectorPtr (var): the pointer you got from the create function
value (var): the value to add to the vector

Function:

function getFromGSVector(vectorPtr,pos);

Description:

Returns a value (var) from the vector from the given position.

Parameters:

vectorPtr (var): the pointer you got from the create function
pos (int*): the position of the wanted value in the vector

Function:

function eraseFromGSVector(vectorPtr,pos);

Description:

Erases a value from the vector at the given position.

Parameters:

vectorPtr (var): the pointer you got from the create function
pos (int*): the position of the value to erase

Function:

function sizeOfGSVector(vectorPtr);

Description:

Returns the size (var) of the vector.

Parameters:

vectorPtr (var): the pointer you got from the create function

Function:

function clearGSVector(vectorPtr);

Description:

Clears the vector.

Parameters:

vectorPtr (var): the pointer you got from the create function

Function:

function pullOutOfGSVector(vectorPtr,pos);

Description:

Returns a value from the vector and erases it.

Parameters:

vectorPtr (var): the pointer you got from the create function
pos (int*): the position of the value

===============================================================

Example:

First of all you have to include the header file in your main.c:

Code:
#include "GSVector.h"


After that declare some global vars for the vectors you'd like to
use so that you can use them from all of your scripts, for example:

Code:
var scheduler;


Then create the vector objects you'd like to use for example in
your void main():

Code:
scheduler = createGSVector();


Now you can add values to the vector like this (scheduletask is a struct I use in my project):

Code:
SCHEDULETASK* scheduleTask = (SCHEDULETASK*)malloc(sizeof(SCHEDULETASK));
scheduleTask.timePoint = int_total_secs + 60;
scheduleTask.functionName = str_create("example schedule");
insertIntoGSVector(scheduler, scheduleTask);


You can add as many values as you like.

After that you can get back your stored values like this:

Code:
int tmpTime = 0;
int i = 0;
SCHEDULETASK* scheduleTask = (SCHEDULETASK*)malloc(sizeof(SCHEDULETASK));
while(1) {
tmpTime = int_total_secs;
if(levelReadyForPlaying != 0 && levelLoaded == 1 && cutsceneHandler == 0 && sizeOfGSVector(scheduler) > 0) {			
for(i=0; i<sizeOfGSVector(scheduler); i++) {
scheduleTask = (SCHEDULETASK*)(getFromGSVector(scheduler,i));
if(tmpTime >= scheduleTask.timePoint) {
eraseFromGSVector(scheduler,i);
call_usefunction(scheduleTask.functionName);
}
}
}
wait(-1);
}


Of course you don't need to use a loop to get the values, you can access them directly by using "getFromGSVector" with the position of the value you want.

Attention: you have to use an "int*" value for the position ... don't store it in variable of type "var".

I hope this documentation helps a bit ... if there are some more questions or tips for improvement or even bugs ... just post them here or write me a PM.

Best regards,
Pegamode.

Posted By: KDuke

Re: GSVector - 03/14/09 18:29

Thank you very much for the documentation and the code examples.

Though I gotta mention one thing about the position variable!
It not only has to be of type int it HAS to be int*

EDIT: please mention that in the documentation!
Posted By: pegamode

Re: GSVector - 03/14/09 19:15

You're absolutely right.

Here's a more simple example:

Code:
var testVec = createGSVector();
insertIntoGSVector(testVec,1);
insertIntoGSVector(testVec,2);
insertIntoGSVector(testVec,3);
insertIntoGSVector(testVec,4);
	
int* pos = 2;
var check = getFromGSVector(testVec, pos);
diag("GSVectorCheck: ");
diag(str_for_num(NULL, check));



Regards,
Pegamode
Posted By: pegamode

Re: GSVector - 03/17/09 15:16

The documentation is now included in the download.
Posted By: VeT

Re: GSVector - 04/15/09 13:22

i got idea... add function, that add unique value to vector.
For example,
function insertIntoGSVectorUnique(vectorPtr,value);

this can check, if values isnt inside vector, then add (runs throung all elements and check)

i'd write this function by my own in lite-c, but i'd wait until you add it, as it supose to be faster
Posted By: VeT

Re: GSVector - 04/15/09 13:50

what a...

Code:
typedef struct { 
  var s;
  var i;
  var j;
  
  var step_max;
  var step_left;
  
} CHARACTER;

******

	vPlayers = createGSVector();
	CHARACTER* tempChar;
	
	tempChar.s = file_var_read(fhandle);
	tempChar.i = file_var_read(fhandle);
	tempChar.j = file_var_read(fhandle);
	
	tempChar.step_max = file_var_read(fhandle);
	tempChar.step_left = file_var_read(fhandle);
	
	insertIntoGSVector(vPlayers,tempChar);


i got a crash frown
Posted By: VeT

Re: GSVector - 04/15/09 13:52

ah.. i needed
CHARACTER* tempChar = (CHARACTER*)malloc(sizeof(CHARACTER));

i thought, Lite-c works automatically for pointers

PS: i decided not to delete this post, as it can helps another people
Posted By: VeT

Re: GSVector - 04/15/09 14:07

okay, next step...

Code:
CHARACTER* tempChar = (CHARACTER*)malloc(sizeof(CHARACTER));

very_very_temp = sizeOfGSVector(vPlayers); // debug shows size = 1, thats right


for(i=0; i<sizeOfGSVector(vPlayers); i++) {
tempChar = (CHARACTER*)(getFromGSVector(vPlayers,i));
		
very_very_temp = tempChar.s; // now its 0, but may be 1, as in file written
very_very_temp = tempChar.i;// this is also 0, may be 2
very_very_temp = tempChar.j;// 0, may be 3

Posted By: VeT

Re: GSVector - 04/15/09 14:38

1) some errors detected in eraseFromGSVector (as i have 2 components in vector, use second and then erase it, when i run through vector, only second(not first) component is inside)

2) i have a trouble with analog of yours
scheduleTask = (SCHEDULETASK*)(getFromGSVector(scheduler,i));
function craches when i run second loop (looking like we need to remove scheduleTask after first loop)
Posted By: pegamode

Re: GSVector - 04/16/09 06:13

Hi VeT,

could you please post the whole code you use?
It's hard to analyze your code snippets as they are not working standalone
to see what's happening in the code you run.

Regards,
Pegamode.
Posted By: VeT

Re: GSVector - 04/16/09 08:19

I was starting to write code, but i got idea how to fix that problem smile
code is in office, and now i'm at home... in some hours i'd be there and, is problem still exist, i'd post it here

Thanks for fast answer smile
Posted By: VeT

Re: GSVector - 04/16/09 13:50

Okay, here i am...
As i see, post is long, so please, read carefully, its not hard smile

So, i have a struct
Code:
typedef struct { 
  var s;
  var i;
  var j;
  
  var step_max;
  var step_left;
  
} CHARACTER;


Then, i have a text file, where info about CHARACTERs is stored..
For example, i have info about 2 characters:
first with s=1; i=4; j=2;,
second one with s=1; i=5; j=5;

But, as character model are not created immideately, i need to store that info in vector. So i read my file:
Code:
players_number = file_var_read(fhandle);

vPlayers = createGSVector();
CHARACTER* tempChar = (CHARACTER*)malloc(sizeof(CHARACTER));
	
var i;
for (i=0; i<players_number; i++)
{
	tempChar.s = file_var_read(fhandle);
	tempChar.i = file_var_read(fhandle);
	tempChar.j = file_var_read(fhandle);
		
	tempChar.step_max = file_var_read(fhandle);
	tempChar.step_left = file_var_read(fhandle);
		
	insertIntoGSVector(vPlayers,tempChar);
}


On this step everything looking okay, and debugger shows that vector vPlayers consist of 2 elements, thats right

Next, i create 2 players models (first, in coordinate s=1; i=4; j=2; , second - in s=1; i=5; j=5;) and i want to load info about their step_left from vector

So, i add function
Code:
void GetPlayerInfo(var s,var i, var j)
{
	ENTITY* tempEnt;
	int num;
	
	CHARACTER* tempChar = (CHARACTER*)malloc(sizeof(CHARACTER));
	
	while(sizeOfGSVector(vPlayers)==0) {wait(1);}
	very_very_temp = sizeOfGSVector(vPlayers);
	wait(1);	
	
	for(num=0; num<sizeOfGSVector(vPlayers); num++) {
		very_very_temp = num;
		tempChar = (CHARACTER*)(getFromGSVector(vPlayers,num));	
		
		if ((tempChar.s == s) && (tempChar.i == i) && (tempChar.j == j))
		{
			tempEnt = ReturnModelPointer (TypeModel,s,i,j);
			
			tempEnt.sPlayerStepsMax = tempChar.step_max;
			tempEnt.sPlayerStepsLeft = tempChar.step_left;
			
			very_very_temp = tempEnt.sPlayerStepsLeft;
			
		//	eraseFromGSVector(vPlayers,num);
			tempChar = NULL;
			break;
		}
		tempChar = NULL;		
	}
}


In this function i go through vector, take elements one-by-one and compare element's s,i,j with models s,i,j.
If true, then i load step_max to entity's skill, if not, i'm proceeding to next element.

Now, the problem:
If i have first entity with coordinates 1,5,5, and first element of vector has 1,5,5, then everything works okay.
BUT, if i compare first entity's 1,4,2 with first element with 1,5,5, then i come to the second vector's element (1,4,2) and in string
Code:
tempChar = (CHARACTER*)(getFromGSVector(vPlayers,num));

i got a crash

I tried to say it as simple as possible smile
Posted By: VeT

Re: GSVector - 04/16/09 13:58

some additional tests shows, that trouble right in
getFromGSVector(vPlayers,num);

i'd look deeply here
Posted By: VeT

Re: GSVector - 04/16/09 14:18

Originally Posted By: KDuke
Hm...

now that I'm finally at the point to actually implement GSVector I'm at loss.
Although I've been using Vectors in C++ before I can't get this one running.

A documentation is really needed.

I already managed to insert several values though I only can use
Code:
getFromGSVector(myVector, 0);

As soon as I use another position the engine crashes.

greetings
KDuke


mmm... yes, something like this.. int* crashes for me, looking like i need work with it
Posted By: VeT

Re: GSVector - 04/16/09 14:33

Code:
	int num;
	
	for(num=0; num<sizeOfGSVector(vPlayers); num++) {
		int* pnum;
		pnum = num;
		very_very_temp = pnum;
		wait(1);
		tempChar = (CHARACTER*)(getFromGSVector(vPlayers,pnum));


at least this not crash smile
damned this half-automatically pointer recognition ability of the engine... mad smile
Posted By: pegamode

Re: GSVector - 04/16/09 19:56

Hi VeT,

using a code similiar to this:

Code:
for(i=0; i<sizeOfGSVector(scheduler); i++) {
    scheduleTask = (SCHEDULETASK*)(getFromGSVector(scheduler,i));
}


works fine in several projects of mine.
Just when I directly access a position in the vector I need to use an int* as it is described in the documentation.

But are you sure to store your values like that in a vector?
In worst case you have to run through the whole vector to get back your values.

Wouldn't it be better to store your struct in a hashmap and then access it directly with a unique key?

In my current projekt (point&click adventure) I have hundreds of useable objects and each of them has additional data stored in structs ... I store those structs in a hashmap and as a key I use a unique ID that is written in string1 of the entity. So if I have the entity I can directly access all additional data by using the ID to get the struct out of the hashmap.

Regards,
Pegamode.
Posted By: VeT

Re: GSVector - 04/16/09 20:35

//using a code similiar to this:
In your case, i is integer or var?

You see, i save all info about land parts (name of model and angle) and models (name of model, angle and HP) in different matrixes in *.lvl file (just text file). Really, i'm able to add matrix with steps_max, steps_current, types of weapons and so on... but its looking like better to save info about charackers in strings after matrixes.
Posted By: pegamode

Re: GSVector - 04/17/09 05:59

In my case i is an int ...

To store additional info for models I use some kind of csv-file.
Each line contains a referenceID to the model and about 30 additional data for each model.
I read those data into struct objects and store those objects in a hashmap using the referenceID as key.
So I can directly access those struct objects for each model. I can even store states like "door is open" during the whole game. So when I leave the room and load the next and then come back later to the same room, the door is still open.
Posted By: VeT

Re: GSVector - 04/17/09 08:41

Hm.. so, you read/write text from file dynamically?
F.e, you read door statement, then player change it, and you write it in a file again?

I chose another way: i read one time info about models from matrixes and store(and change) it there.. while level saved, i save matrixes into file one time.

But between reading info and creating models i need to store info about players somewhere else, like in your vector.


//In my case i is an int ...
But what about KDuce's note
Quote:

Thank you very much for the documentation and the code examples.

Though I gotta mention one thing about the position variable!
It not only has to be of type int it HAS to be int*

EDIT: please mention that in the documentation!



I tried this way:
Code:
void GetPlayerInfo(var s,var i, var j)
{	
	CHARACTER* tempChar = (CHARACTER*)malloc(sizeof(CHARACTER));
	while(sizeOfGSVector(vPlayers)==0) {wait(1);}
	diag("\n[!!!] Vector size:");
	diag_var(" size: %6.3f",sizeOfGSVector(vPlayers));
	
	int num;	
	for(num=0; num<sizeOfGSVector(vPlayers); num++) {
		tempChar = (CHARACTER*)(getFromGSVector(vPlayers,num));
		
		diag("\n[!!!] Vector:");
		diag_var(" s: %6.3f",tempChar.s);
		diag_var(" i: %6.3f",tempChar.i);
		diag_var(" j: %6.3f",tempChar.j);
	}
}

and this gives me

[!!!] Vector size: size: 2.000
[!!!] Vector: s: 1.000 i: 5.000 j: 5.000
Error E1513: Crash in GetPlayerInfo
Program aborted

Now, if i change order of storing values in vector, it returns
[!!!] Vector size: size: 2.000
[!!!] Vector: s: 1.000 i: 4.000 j: 2.000
Error E1513: Crash in GetPlayerInfo
Program aborted


So, from all this i can say that insertIntoGSVector works fine, but i may deal with getFromGSVector

Please, can you gibe more info about getting values from vector?
Posted By: KDuke

Re: GSVector - 04/17/09 09:31

Hi VeT!

In the meantime I've encountered problems with simply using int* as well.
I had the same trouble as you have. Tried different things until I came (I think at least so, I don't have my source at hand right now) to a point where I noticed that it is working when I use a global int for the getFromGSVector index.
If you try to use a local variable getFromGSVector it needs to be of datatype byte* because if you use
Code:
int* example = 0;
example ++;

the value of example will be 4 as an int is 4 bytes big and therefore the pointer is increased in multitudes of four. Thats how it is working for me right now. But maybe pegamode just should check his source for getFromGSVector. It indeed behaves strange in some situations.

greetings
K-Duke
Posted By: pegamode

Re: GSVector - 04/17/09 11:04

Hi VeT,

No, I don't write in a file ... I just read from a file.
If the state is changed it is set in the struct object.

I only write a file when during game save.

---------

The source of "getFromGSVector" isn't that special:

DLLFUNC var getFromGSVector(container &cont, int pos)
{
return cont.at(pos);
}

I'd never had any problem using the vector.
Maybe you have to check your pointers ... you have to be sure that the address is still correct when you use it with the vector.
A non-static variable gets a new address with every wait. Maybe you have to check this. Don't know.

Regards,
Pegamode.
Posted By: VeT

Re: GSVector - 04/17/09 13:53

I'd try to work with static.... and i offer you to make new version of your dll(probably you wouldnt use it, as you use your version many times in your projects, but it may be more friendly for users):

1a) with the help of
inline int _INT(var x) { return x>>10; }
convert second parameter to var, so we'd be able easily to get ascess to
any element of vector in loop without any troubles smile

1b) as variant, add new function getFromGSVectorElement, for example(just cant find better name), that would work like 1a)

2) add function insertIntoGSVectorUnique that will check, if the same element is already exist in vector
Posted By: VeT

Re: GSVector - 04/17/09 14:02

//No, I don't write in a file ... I just read from a file.If the state is changed it is set in the struct object.

But... if you have 100 records and 100 doors, how are you telling the doors their states?
Imho, you need OR load state of the door in every action of door (read all file and search needed state), OR store all records in operative memory, and spread doors/states from here
Posted By: VeT

Re: GSVector - 04/17/09 14:20

okay, i make
static byte* pnum = 0;
***
pnum++;

and debug shows me, that pnum = 0.001
Posted By: pegamode

Re: GSVector - 04/17/09 14:33

Hi VeT,

all struct objects are in memory (build on game startup by reading the file).
All useable objects have an action that get it's correspondenting struct object and sets its own pointer. So I can access the entity when I have the struct object and I can also access the struct object when I have the entity.
Posted By: VeT

Re: GSVector - 04/17/09 14:42

But where are you store all structures?
Okay, thats individual smile

So, what about second version of your dll?
(i'm afraid to ask sources laugh )
Posted By: VeT

Re: GSVector - 04/17/09 14:47

well, at least
Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>

#include <GSVector.h>
///////////////////////////////

function main()
{
	level_load("");
	wait(2);	
	
	var testVec = createGSVector();
	insertIntoGSVector(testVec,1);
	insertIntoGSVector(testVec,2);
	insertIntoGSVector(testVec,3);
	insertIntoGSVector(testVec,4);
	
	int* pos = 0;
	var check = getFromGSVector(testVec, pos);
	diag("GSVectorCheck: ");
	diag(str_for_num(NULL, check));
}

this works smile
Posted By: VeT

Re: GSVector - 04/17/09 15:03

looking like you write in Legasy mode smile

Code:
for(i=0; i<sizeOfGSVector(scheduler); i++) {
scheduleTask = (SCHEDULETASK*)(getFromGSVector(scheduler,i));

this crashes in any case

i tried
&pos = testpos;
but this crashes, as it may be, but... smile

how do you use for-loop to get elements?
Posted By: VeT

Re: GSVector - 04/17/09 15:15

2KDuke

i dont know, how you comiled your code smile
Code:
int* pos = 0;
pos += 1; // or pos ++; or pos = pos + 1;

crash to me... only variant is to use
Code:
pos = 2;

but
Code:
pos = num; // or &pos = num; or even pos = &num;(i'm going crazy :))) 

dont work

you want to say that i may use
Code:
int* firstpos = 0;
int* secondpos = 1;
int* thirdpos = 2;

***
pos = firstpos; // this works

? laugh
Posted By: pegamode

Re: GSVector - 04/18/09 04:44

Hi VeT,

after 14 hours at work I didn't feel like changing the GSVector.

I think I could build a special version with var parameters instead of int this weekend, but I'm not sure if that helps you to get rid of assuring that your pointers are valid. But let's give it a try.

Regards,
Pegamode.
Posted By: VeT

Re: GSVector - 04/18/09 07:22

That would be very nice smile
You can just add new function, so your new version would compatible with old one.

Anyway, if vector works good for you, can you show, how to incriment pointer on vector's position?
Posted By: pegamode

Re: GSVector - 04/20/09 19:46

Sorry, I didn't find the time for the new version yet (but it will come).

But, I can show you how I would increment a counter variable:

Code:
        var test = createGSVector();
	
	insertIntoGSVector(test, 0);
	insertIntoGSVector(test, 1);
	insertIntoGSVector(test, 2);
	insertIntoGSVector(test, 3);
	insertIntoGSVector(test, 4);
	
	int i = 0;
	
	diag("\ngetFromGSVector:");
	diag(str_for_num(NULL,getFromGSVector(test,(int*)i)));
	i++;
	diag("\ngetFromGSVector:");
	diag(str_for_num(NULL,getFromGSVector(test,(int*)i)));
	i++;
	diag("\ngetFromGSVector:");
	diag(str_for_num(NULL,getFromGSVector(test,(int*)i)));
	i++;
	diag("\ngetFromGSVector:");
	diag(str_for_num(NULL,getFromGSVector(test,(int*)i)));
	i++;
	diag("\ngetFromGSVector:");
	diag(str_for_num(NULL,getFromGSVector(test,(int*)i)));


Regards,
Pegamode.
Posted By: VeT

Re: GSVector - 04/20/09 19:59

Ahh... really ^^
Thank you, looking like this may work, tomorrow i'd check it smile
Posted By: VeT

Re: GSVector - 04/21/09 13:07

i'm gonna crazy...

Code:
for(num=0; num<sizeOfGSVector(vPlayers); num++) {			
		tempChar = (CHARACTER*)(getFromGSVector(vPlayers,(int*)num));
		
		diag("\n[!!!] CHARACTER:");
		diag_var(" n: %6.3f",num);
		diag_var(" s: %6.3f",tempChar.s);
		diag_var(" i: %6.3f",tempChar.i);
		diag_var(" j: %6.3f",tempChar.j);		
	}


return
[!!!] CHARACTER: n: 0.000 s: 1.000 i: 5.000 j: 5.000
[!!!] CHARACTER: n: 1.000 s: 1.000 i: 5.000 j: 5.000

but the second string must be n: 1.000 s: 1.000 i: 4.000 j: 2.000
Posted By: pegamode

Re: GSVector - 04/21/09 13:30

It's hard to comment your code as you just posted the get-part, but not where the values are set.

It would be more helpful if you could post a code that could be run standalone.

Regards,
Pegamode.
Posted By: VeT

Re: GSVector - 04/21/09 13:45

looking like it works smile
While filling array, you may use

correct
Code:
vPlayers = createGSVector();
	
	var i;
	for (i=0; i<players_number; i++)
	{
		CHARACTER* tempChar = (CHARACTER*)malloc(sizeof(CHARACTER));
		
		tempChar.s = file_var_read(fhandle);
		tempChar.i = file_var_read(fhandle);
		tempChar.j = file_var_read(fhandle);
		
		tempChar.step_max = file_var_read(fhandle);
		tempChar.step_left = file_var_read(fhandle);
		
		insertIntoGSVector(vPlayers,tempChar);
	}


but not
wrong
Code:
vPlayers = createGSVector();
	
	var i;
CHARACTER* tempChar = (CHARACTER*)malloc(sizeof(CHARACTER));
	for (i=0; i<players_number; i++)
	{		
		tempChar.s = file_var_read(fhandle);
		tempChar.i = file_var_read(fhandle);
		tempChar.j = file_var_read(fhandle);
		
		tempChar.step_max = file_var_read(fhandle);
		tempChar.step_left = file_var_read(fhandle);
		
		insertIntoGSVector(vPlayers,tempChar);
	}

Posted By: pegamode

Re: GSVector - 04/21/09 13:47

Hi VeT,

my current guess is that your mistake is not when reading from the vector, but when creating your struct objects.

Some posts before you posted the following code:

Code:
players_number = file_var_read(fhandle);

vPlayers = createGSVector();
CHARACTER* tempChar = (CHARACTER*)malloc(sizeof(CHARACTER));
	
var i;
for (i=0; i<players_number; i++)
{
	tempChar.s = file_var_read(fhandle);
	tempChar.i = file_var_read(fhandle);
	tempChar.j = file_var_read(fhandle);
		
	tempChar.step_max = file_var_read(fhandle);
	tempChar.step_left = file_var_read(fhandle);
		
	insertIntoGSVector(vPlayers,tempChar);
}


You can't do it that way, because you have to create an object for each player.
So you have to move the malloc into the loop ... this way:

Code:
players_number = file_var_read(fhandle);

vPlayers = createGSVector();
	
var i;
for (i=0; i<players_number; i++)
{
        CHARACTER* tempChar = (CHARACTER*)malloc(sizeof(CHARACTER));
	tempChar.s = file_var_read(fhandle);
	tempChar.i = file_var_read(fhandle);
	tempChar.j = file_var_read(fhandle);
		
	tempChar.step_max = file_var_read(fhandle);
	tempChar.step_left = file_var_read(fhandle);
		
	insertIntoGSVector(vPlayers,tempChar);
}


Regards,
Pegamode
Posted By: VeT

Re: GSVector - 04/21/09 13:47

//It would be more helpful if you could post a code that could be run standalone.

Yes, i just made in this way
Thanks, now i update project, hope this is the last problem smile
Posted By: VeT

Re: GSVector - 04/21/09 13:48

laugh laugh laugh Thats it smile
Posted By: pegamode

Re: GSVector - 04/21/09 13:49

Ok ... it seems like we wrote our posts sametime.

So, I was right with my guess.

It's quite sure that you have to create an object for each player.
Posted By: VeT

Re: GSVector - 04/21/09 13:51

Yes, you're right smile
I thought that i can create one object and then change its settings, throw them into vector. This because i didnt know, how your vector works... So, now you can update manual for it.

Btw, what about insertIntoGSVectorUnique function? Are you going to release second version of your vector?
Posted By: pegamode

Re: GSVector - 04/21/09 14:01

I'm not sure what I shall update in the manual?

Always remember it's just a pointer that is stored in the vector !!!

I can still release a new version of the vector, but I'm really not sure if it would make a big difference. In the current version you have to use an int* for the position parameter and in the new version you would have to use a var. Not that much difference.

Regards,
Pegamode.
Posted By: VeT

Re: GSVector - 04/21/09 14:23

Updating manual: i'm speaking about this
Code:
int i = 0;
	
	diag("\ngetFromGSVector:");
	diag(str_for_num(NULL,getFromGSVector(test,(int*)i)));


But generaly, i'm talking about insertIntoGSVectorUnique function, wich would check, if elements isnt stored in vector

F.e. you have vector 1,5,2,4,3 and you want to add 2, so
insertIntoGSVector would create 1,5,2,4,3,2
and
insertIntoGSVectorUnique would leave 1,5,2,4,3 as 2 is already stored in vector
I think, that searching through vector in dll(with the help of insertIntoGSVectorUnique function, that stored in the dll) would be faster than in lite-c
Posted By: pegamode

Re: GSVector - 04/21/09 16:10

You think in a too simple way with "insertIntoGSVectorUnique".

It's just a pointer that is stored in the vector and not a number.
The vector does not know what kind of object is behind the pointer so it's not possible to compare those.

I think in such a case a hashmap is the better choice than a vector.

Regards,
Pegamode.
Posted By: VeT

Re: GSVector - 04/21/09 18:28

ah, okay.. so, looking like you needn't second version ^_^
© 2024 lite-C Forums