Help with Type

Previous topic - Next topic

Kyo

Hi,
I explain the problem.

I have a Type with a lot of information:

Code (glbasic) Select

TYPE tChar
Nome_Char$
Num_Anim%
Nome_Anim$
Num_Sprite%
Tot_Frame
X#
Y#
Flip
Scale#
//__________
start_image
id_sprite%
x_c%
y_c%
w_c%
h_c%
//__________
piv_x
piv_y
//__________
col_act_1%
col_x_1%
col_y_1%
col_w_1%
col_h_1%
//__________
col_act_2%
col_x_2%
col_y_2%
col_w_2%
col_h_2%
//__________
move_x#
move_y#
timer%
active_scia
//__________
frame_adv
start_anim
timer_frame
end_anim
old_anim
tipo_anim$
anim_locked
old_anim_locked
Key_press
ENDTYPE

GLOBAL Chars[] AS tChar
DIM Chars[5][50][23]



In this Type I load all the information of my Characters.

Now the problem is that I need to clone all the information to create more Characters!

To be more precise I get this:



What is the best way, without having to copy all the information into another type?

Kitty Hello

Store an index to the list?

doimus

#2
Quote from: diego on 2013-Feb-22

Now the problem is that I need to clone all the information to create more Characters!

What is the best way, without having to copy all the information into another type?

Even if you do need to have a clone, that clone will have at least different x,y position variables. But then it's not exactly a clone and data is not exactly the same, right?

Maybe organize the data differently. Why do you need 3 dimensional array for characters, couldn't all that data be stored inside one-dim array of types? And then just DIMPUSH or DELETE them all over the place?

edit: Or do you need some sort of parent-child relation between them? If so:
Code (glbasic) Select

Character[42].hasParent = TRUE
Character[42].parentId   = 12


and/or :

Code (glbasic) Select
Character[12].child[10] = 42


This way both the parent and child are of same type, and are aware of each other.

Kyo

I load data from a TXT File with this system:




There are many data for each animation (Loop, Stop, etc.) and there are a lot of data for each Sprite (Pivot Position, Rect Collision Zone, etc.)

When I load the 3 Characters:
- Chars[0]
- Chars[1]
- Chars[2]

How can I create the Clone (for example of Chars [1]) without having to copy all the data in another Data Type?

Kyo

ANY HELP?   :help: :nw:

Slydog

#5
GLBasic doesn't allow you to store a reference, only the values, so copying is the only method I can think of.
But, I have a couple of suggestions to make this simpler and cleaner.

First, create more utility TYPEs, such as:

Code (glbasic) Select
TYPE TPoint
x%
y%
ENDTYPE

TYPE TRect
x%
y%
w%
h%
ENDTYPE


Then, your other TYPE could be rewritten as such:
Code (glbasic) Select
TYPE tChar
Nome_Char$
Num_Anim%
Nome_Anim$
Num_Sprite%
Tot_Frame

position AS TPoint  // Replaces 'X', and 'Y'

Flip
Scale#
//__________
start_image
id_sprite%
c AS TRect // Replaces 'x_c%', 'y_c%', 'w_c%', 'h_c%'
//__________
pivot AS TPoint // Replaces 'piv_x', 'piv_y'
//__________
col_act_1%
col AS TRect // Replaces 'col_x_1%', 'col_y_1%', 'col_w_1%', 'col_h_1%'
//__________
col_act_2%
col2 AS TRect // Replaces 'col_x_2%', 'col_y_2%', 'col_w_2%', 'col_h_2%'
//__________
move AS TPoint // Replaces 'move_x#', 'move_y#'
timer%
active_scia
//__________
frame_adv
start_anim
timer_frame
end_anim
old_anim
tipo_anim$
anim_locked
old_anim_locked
Key_press
ENDTYPE


Then in your other code (inside your 2 FOR loops), you can do:
Code (glbasic) Select
Chars[plr][i][x].col = col
Chars[plr][i][x].col2 = col2
Chars[plr][i][x].pivot = pivot


You could take this further and create more specialized sub TYPEs, but I don't know what exactly you are doing, so I will leave that up to you.

Don't forget TYPEs can have functions, so if you can think of a way to simplify your main code and pass it off to your TYPE, by all means do so.  (Such as a specialized 'Copy()' function to copy one instance of a TYPE into anther)
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Kyo

#6
Thanks for reply Slydog  :booze:

The problem is that your reading system is almost like mine!

I use 2 FOR NEXT cycles because are many data to read, and use them only at the beginning to load data from the Characters:

Example data txt File (The txt is generated by an editor that I created with. Net):
Code (glbasic) Select

Soldier //Name of Character
120 // Start X
280 // Start Y
/////////
0 // First Animation
Stand // Name ANimation
A_L // Type Animation (A_L = Animation_Loop, A_S = Animation_Stop, A_R = Animation_Reverse, ecc)
0  // 0 = Animation_No_locked (if you press a Key the Character change Animation), 1 = (the animation is played to the end and keys have no effect - Death, Win, Super ecc)
3 // Number of Frame in animation
----------
1|41*67|50|72|159*106|210*1|82|53|50|155*0|0|0|50|50*0|0|0|50|50*0|0|0|50|50*0|0|0|0*0|0|2*
2|52*67|50|72|159*106|210*1|78|53|50|150*0|0|0|50|50*0|0|0|50|50*0|0|0|50|50*0|0|0|0*0|0|2*
3|53*67|50|72|160*106|210*1|78|51|50|151*0|0|0|50|50*0|0|0|50|50*0|0|0|50|50*0|0|0|0*0|0|2*
/////////
1
Walk_L
A_L
0
2
----------
1|149*70|50|73|157*106|207*1|84|54|50|146*0|0|0|50|50*0|0|0|50|50*0|0|0|50|50*0|0|0|0*-3|0|2*
2|150*70|50|71|156*106|206*1|82|54|50|141*0|0|0|50|50*0|0|0|50|50*0|0|0|50|50*0|0|0|0*-3|0|2*
/////////

Bla Bla.....
Bla Bla.....

/////////
50
Death
A_S
1
4
----------
1|210*70|50|73|157*106|207*1|84|54|50|146*0|0|0|50|50*0|0|0|50|50*0|0|0|50|50*0|0|0|0*-3|0|2*
2|211*70|50|71|156*106|206*1|82|54|50|141*0|0|0|50|50*0|0|0|50|50*0|0|0|50|50*0|0|0|0*-3|0|2*
3|212*70|50|71|156*106|206*1|82|54|50|141*0|0|0|50|50*0|0|0|50|50*0|0|0|50|50*0|0|0|0*-3|0|2*
4|213*70|50|71|156*106|206*1|82|54|50|141*0|0|0|50|50*0|0|0|50|50*0|0|0|50|50*0|0|0|0*-3|0|2*
/////////


Code (glbasic) Select

1|41*67|50|72|159*106|210*1|82|53|50|155*0|0|0|50|50*0|0|0|50|50*0|0|0|50|50*0|0|0|0*4|5|2*
1 = id_Sprite
41 = id image store in Memory
67|50|72|159 = X,Y,W,H of the sprite for POLYVECTOR
106|210 = XY Pivot
1| = 0 Rect1 OFF  - 1 Rect1 On
82|53|50|155  = X,Y,W,H of the Rect 1 for POLYVECTOR
0| = 0 Rect2 OFF  - 1 Rect2 On
0|0|50|50 = X,Y,W,H of the Rect 2 for POLYVECTOR
Same for Rect 3 and Rect 4
4| = Vel X
5| = Vel Y
2! = Timer


After loading the data from the Character I do not use any loop FOR... NEXT, because I have direct access to animations!

Example:
Char[0][3][1].id_Sprite
[ 0] = Character 0
[ 3] = Animation id (Walk Left)
[ 1].id_Sprite = First Sprite to Draw

The game is a Beet'M'UP (Style Double Dragon), and there are many enemies divided into 5 categories!
I want to load the data only once per category, and then clone it!

The Editor:

mentalthink

And make a classe for the charcters?¿, whit this method you have all the enemies you want whit your type whit out too much problems... and you can make anyone different of the others whit a machine State...

Kyo

Quote from: Ocean
you might want to think about de-coupling the characters and their properties, for instance by having an 'array of characters' and an 'array of properties'.  Any object in the 'array of characters' would contain an index value (as KungPhoo suggeted) that points to the proper record in the 'array of properties'.
As you stated that you only have 5 categories (sets of character properties), these 5 would only be stored once in the small 'array of properties'. This reduces data and offers easy extensibility too in case you need additional categories in the future, as in that case you don't need to change the datastructure for your 'array of characters'.


Quote from: mentalthink on 2013-Feb-23
And make a classe for the charcters?¿, whit this method you have all the enemies you want whit your type whit out too much problems... and you can make anyone different of the others whit a machine State...

Thanks for the help mentalthink and Ocean  :booze:

Could you explain with a small example?  =D

mentalthink

Yes Diego, something like this (If you speak Spaninsh, comment me , I think I can be more clear in my language)

Somthing very easy whit 2 prints, ok?¿...

In some part of your code, normally it's when you declare the arrays, you have to do something like this, it's the typicall.



global stars[] as TSTAR
dim   stars[any_Number] Ej. 5


Now go to new GLbas, press new GLbas file, and add to your project.

This have to start in this mode.

TYPE TSTAR
id;
x;
y;
speed;
// Here normally you close the Type, but for make a Class, you leave open and now we put some functions into this type, ok?¿.

function initialize:
if key(57)
   self.x = 100
   self.y = 100
  self.speed = rnd(5)
endif
endfunction

function move:
   inc self.x self.speed
   inc self.y self.speed
endfunction

function draw:
  print "*" , self.x, self.y
endfunction

endtype    // We are finished all we want do whit this array what have functions into it.

Here we have to do something for call from our main code, to all this code, the functions into the Type TSTAR

function do_all_Classe1:

foreach item in stars[]
   item.initialize()
   item.move   ()
   item.draw    ()
Next

endfunction
// If you put more things, only it's in this case item like varible and put the name of the function it's beetwen  Type and //EndType

Now whit this last functiobn you can call from anywhere of your code this this-- it's something like a class in C++, the good thing about this it's a lot of things...

1st... Now whe have to look 5 "*" in the screen, but if in the array declaration you put 100, automatically you will have 100 "*" in the screen
2nd, Well I want modify somthing... only add functions, modify somefunctions and each item into the array will do automatically waht you want.
3rd add a machine state.... This I put a simple example... modifing the first function...

function initialize:
if self.id < 3
    self. x= 10
    self. y=10
else
   self.x =300
   self.y = 300
endif
endfunction

This it's a very simple, but you can look only put a few lines, we change a lot the behaviour, now the first 0,1,2 "*" appears" in the left corner upper screen, and the anothers in the inverse corner... But you can do anything you want make State machines... I'm not sure if in english it's called like this... but only are if, and standard things not are nothing from the outer Space, when I ear the name for first time, I thinked in something like 10001 or very complex method  :D :D

Really now you can say me... what it's this all the time saying self.  well... self it's imagine I have an array[1], only 1 element, ok?¿, self.x it's somthing like element1.x , but the good thing it's I have an array[1000] elements...
and self.x it's equal than self.x but for 1000 elements... but I don't have to take care about one in one, any one it's smart for do what I want he do... (I don't know if this it's very comprensible, but try to make 2 simple exercices, invent something and try it, you will find very easy)


And this it's the Classes or something similar to Classes in C++, beacuse are a bit different, for the private and public part... but this kind of pick code, really, make your code hyper easy and you will not waste too much time picking code... (In the forum some people explain this whit a more deep, in somepart)

PS: If the code don't works, try to review it  a bit, I pick directly in the post, and I'm not sure If I'm wrong in some word... but basically it's the way for do the classe...

Regards Diego.




ENDTYPE

Kyo

My name is Diego but I'm not Spanish but Italian  :D

Monday I will try to use your system that is much more clean and efficient!

But the problem remains:

If in the function initialize I insert the code to read data from txt file, every time I create a new character  the program must read from TXT File, and I want to read from the file only when the program starts, and then use the stored data to be passed to a new character! (or maybe I did not understand the code that you have written!)  ::) :help:

Thank you for helping me!  :good: :booze:

mentalthink

I Diego well Spaninsh and Italiand, some people says We are cousins... Que pasa Primoooo!!!!  :D :D

Well about you tell me the data read from TXT, doens't too much problem, in example you can load all you data into another array, and the pass what part from this array to this Classe.

Initialize only really it's for a only One time... but in this case I put when you press the space bar, reset all and start again the Classe... only was for this... but you can do anything... I put this like example, but youcan think in anything to put betwenn the Type and endtype and make very very complex and cool and fun things...

Diego , if you need more help, comment me... I'm sure someone comment more, a really programmer, I'm only a hobbiest and I don't have too much knoledges about programming...

Regards,Primo  :D :D =D

doimus

#12
Quote from: diego on 2013-Feb-24

If in the function initialize I insert the code to read data from txt file, every time I create a new character  the program must read from TXT File, and I want to read from the file only when the program starts, and then use the stored data to be passed to a new character!

That means you need to simplify things. There's a saying: "Perfection is not when you have nothing more to add, but when you have nothing left to remove." which works perfectly for programming. Slice everything in manageable and usable chunks.
Like you had a small company whose job is to run the game.

For example, in your code "character" really shouldn't do any loading from file. That should be the job for a "data loader". And then "data loader" should pass relevant data to "characters".
"Character" should only think about where he stands and what he is doing (ie jumping, high-kick, punch etc...) and then he should have a "sprite manager" who takes care of his sprites. So, "sprite manager" thinks: yeah, now he is jumping while punching, that means I have to draw jump-punch animation at his position. But what is his position?? And what is the current frame of jump-kick animation??
So "sprite manager" asks "character" about position (x:10,y:42 for example) and "animation manager" about current frame or sprite_id (123 or whatever).
Then character's "sprite manager" sends that data ( x:10, y:42, sprite:123) to the "draw manager" who actually gets all data from all the characters (he's like the boss of the company), sorts it into a drawing list(an array) of sprites and then draws it all on screen in one tiny function like:

FOREACH n in drawlist[]
    DRAWSPRITE n.x, n.y, n.sprite
NEXT


And ultimate goal is to have the entire game organized in these small functions, types, objects... whatever we decide to call them. Then it's very easy to read and modify the code even months from now. If you keep everything monolithic, nested together, it will be a mess to return to it in the future when design mechanism disappears from your mind. Ask me how I know that!  :-[

Kyo

Quote from: mentalthink on 2013-Feb-24
I Diego well Spaninsh and Italiand, some people says We are cousins... Que pasa Primoooo!!!!  :D :D

Well about you tell me the data read from TXT, doens't too much problem, in example you can load all you data into another array, and the pass what part from this array to this Classe.

Initialize only really it's for a only One time... but in this case I put when you press the space bar, reset all and start again the Classe... only was for this... but you can do anything... I put this like example, but youcan think in anything to put betwenn the Type and endtype and make very very complex and cool and fun things...

Diego , if you need more help, comment me... I'm sure someone comment more, a really programmer, I'm only a hobbiest and I don't have too much knoledges about programming...

Regards,Primo  :D :D =D

Hola hermano   :booze:
From tomorrow I start fixing the code, step by step, let's fix everything.

doimus I do not know if I understand everything you've written (I do not speak English well)  :P
But my system, if I use it in 1 VS 1 (Street Fighter), the code works perfectly!

All states (Stand, Walk, Jump, Crouch, etc.) are handled perfectly and even state changes:

Ready Stance -> hit the top -> Damage_Up _ Low
Ready Stance -> hit in the middle -> Damage_Up _ High

I'm not doing 1 VS 1 only because it requires too many graphics (700 Sprites for Characters).

I'm not sure I understand what you meant, I'm sorry for my bad English.  :whistle:

Kyo

I have a problem:


Code (glbasic) Select

FOREACH t IN Enemy[]
FOREACH s IN Enemy[]
IF Enemy[t].Y > Enemy[s].Y

rendering_enemy(en)
BREAK
ENDIF
NEXT
NEXT

ERROR:

compiling:
C:\Users\USER~1\AppData\Local\Temp\glbasic\gpc_temp1.cpp: In function `DGInt __GLBASIC__::re_order_enemy_rendering()':
C:\Users\USER~1\AppData\Local\Temp\glbasic\gpc_temp1.cpp:705: error: no match for call to `(__GLBASIC__::DGArray<__GLBASIC__::tChar>) (__GLBASIC__::tChar&)'
C:/Program Files (x86)/GLBasic_v11/Compiler/platform/Include/glb.h:631: note: candidates are: T& __GLBASIC__::DGArray<T>::operator()(int, int, int) [with T = __GLBASIC__::tChar]
C:/Program Files (x86)/GLBasic_v11/Compiler/platform/Include/glb.h:694: note:                 __GLBASIC__::DGArray<T>& __GLBASIC__::DGArray<T>::operator()() [with T = __GLBASIC__::tChar]
C:\Users\USER~1\AppData\Local\Temp\glbasic\gpc_temp1.cpp:705: error: no match for call to `(__GLBASIC__::DGArray<__GLBASIC__::tChar>) (__GLBASIC__::tChar&)'
C:/Program Files (x86)/GLBasic_v11/Compiler/platform/Include/glb.h:631: note: candidates are: T& __GLBASIC__::DGArray<T>::operator()(int, int, int) [with T = __GLBASIC__::tChar]
C:/Program Files (x86)/GLBasic_v11/Compiler/platform/Include/glb.h:694: note:                 __GLBASIC__::DGArray<T>& __GLBASIC__::DGArray<T>::operator()() [with T = __GLBASIC__::tChar]
*** FATAL ERROR - Please post this output in the forum


but if I enter the value t, I have this error:

Code (glbasic) Select

FOREACH t IN Enemy[]
FOREACH s IN Enemy[]
IF Enemy[t].Y > Enemy[s].Y

rendering_enemy(t)
BREAK
ENDIF
NEXT
NEXT

ERROR:

given: tChar
  wants: DGInt
"Anim_Character.gbas"(165) error : GPC0007 wrong argument type : rendering_enemy, arg no: 1



Why???  :blink: