coding help, problem with variables

Started by gia, September 20, 2007, 06:46:54 PM

Previous topic - Next topic

gia

Hi, I dont know how you guys learn to code given that the tutorials I've found have very limited topics, haven't found a wiki that gives the specification of all the functions, constants and stuff, at least in english.

I was trying to implement a dash/run that uses its own "energy" which recovers if you aren't dashing. And it works for both backwards and forwards. I managed to make it work without implementing the energy part, as soon as you release the direction the char stops running. The dash starts with a "charge" part and then the dash itself.

My idea was to have var 0 = dash points, var 1 = time to next deduction, var 2 = counter to next increase. States 100,101,102,103 are the dash states (charge and dash, forward and back) and they work perfect when not using vars.

From a tutorial and using kfm as a base, I did this to attemp to add the energy part:

;---------------------------------------------------------------------------
;this method of keeping data is good when you wont use many variables
;---------------------------------------------------------------------------
;Dash has 10 dash points
;As soon as you start charging you lose one point... and start losing points for the duration of the dash,
; a point lasts 16 frames AFTER you lose it
; -> so, while var1 has a value if you start charge then the value resets to 16
;you recover points every 16 frames of not dashing or charging a dash

;var 0 = dash points (0-10)
;var 1 = free dash time (0-16), lower dash points!, we lower if it hits zero
;var 2 = counter to recover dash point (0-16) recover dash points!, we increase if it hits 16

;reduce the free dash time every tick
[State -2]
type = VarAdd
trigger1 = var(1) > 0 ;If you set a value in a trigger alone, as long as its value is greater than 1 (holds true), then it'll work.
var(1) = -1

;increase the time to recover points if we aren't dashing
[State -2]
type = VarAdd
trigger1 = stateno != 100 && stateno != 101 && stateno != 102 && stateno != 103 ;put a list of all the states that refer to dashing
var(2) = 1

;reduce dash points if we must
[State -2]
type = VarAdd
trigger1 = var(1) = 0 && var(0) > 0 && ( stateno = 100 || stateno = 101 || stateno = 102 || stateno = 103 ) ;put a list of all dashing states
var(0) = -1
var(1) = 16

;increase dash points if we must
[State -2]
type = VarAdd
trigger1 = var(2) = 16 && var(0) < 10
var(0) = 1
var(2) = -16

;reset time to recover if we dash
[State -2]
type = VarSet
trigger1 = var(2) && ( stateno = 100 || stateno = 101 || stateno = 102 || stateno = 103 ) ;put a list of all the states that refer to dashing
var(2) = 0


And these commands start everything:

;---------------------------------------------------------------------------
;Run Fwd
;ƒ_ƒbƒVƒ...
[State -1, Dash Fwd]
type = ChangeState
trigger1 = command = "FF" && var(0) > 0
trigger1 = ctrl
value = 100

;---------------------------------------------------------------------------
;Run Back
;Œã'Þƒ_ƒbƒVƒ...
[State -1, Dash Back]
type = ChangeState
trigger1 = command = "BB" && var(0) > 0
trigger1 = ctrl
value = 101






Now the rest of states are on the cns:

The initialization:
;---------------------------------------------------------------------------
; Initialize (at the start of the round)
[Statedef 5900]
type = S

[State 5900, 1] ;Clear all int variables
type = VarRangeSet
trigger1 = roundsexisted = 0
value = 0

[State 5900] ;Set dash points
type = VarSet
trigger1 = roundsexisted = 0
var(0) = 10 ;<-THERE

[State 5900, 2] ;Clear all float variables
type = VarRangeSet
trigger1 = roundsexisted = 0
fvalue = 0

[State 5900, 3] ;Intro for round 1
type = ChangeState
trigger1 = roundno = 1
value = 190




Charging:

;changed from running to run lag
[Statedef 100]
type    = S
movetype= U
physics = S
velset = 0,0
anim = 100
sprpriority = 1

[State 100, 2] ;Prevent run from canceling into walk
type = AssertSpecial
trigger1 = 1
flag = NoWalk

[State 100, 3] ;Prevent from turning
type = AssertSpecial
trigger1 = 1
flag = NoAutoTurn

[State 100, 4]
type = ChangeState
trigger1 = AnimElem = 2
value = 102


;changed from running to run lag
[Statedef 101]
type    = S
movetype= U
physics = S
velset = 0,0
anim = 105
sprpriority = 1

[State 101, 2] ;Prevent run from canceling into walk
type = AssertSpecial
trigger1 = 1
flag = NoWalk

[State 101, 3] ;Prevent from turning
type = AssertSpecial
trigger1 = 1
flag = NoAutoTurn

[State 101, 4]
type = ChangeState
trigger1 = AnimElem = 2
value = 103



Dashing:

;------------------
;Dash Forward
[Statedef 102]
type    = S
physics = S
anim = 101
sprpriority = 1

[State 102, 1]
type = VelSet
trigger1 = 1
x = const(velocity.run.fwd.x)

[State 102, 2] ;Prevent run from canceling into walk
type = AssertSpecial
trigger1 = 1
flag = NoWalk

[State 102, 3] ;Prevent from turning
type = AssertSpecial
trigger1 = 1
flag = NoAutoTurn

[State 102, runoutofpoints]
type = ChangeState
trigger1 = var(0) = 0
value = 0

[State 102, 4]
type = ChangeState
trigger1 = command != "holdfwd"
value = 0







;------------------
;Dash Backward
[Statedef 103]
type    = S
physics = S
anim = 106
sprpriority = 1

[State 103, 1]
type = VelSet
trigger1 = 1
x = -const(velocity.run.fwd.x)

[State 103, 2] ;Prevent run from canceling into walk
type = AssertSpecial
trigger1 = 1
flag = NoWalk

[State 103, 3] ;Prevent from turning
type = AssertSpecial
trigger1 = 1
flag = NoAutoTurn

[State 103, runoutofpoints]
type = ChangeState
trigger1 = var(0) > 0
value = 0

[State 103, 4]
type = ChangeState
trigger1 = command != "holdback"
value = 0


Ingame the character plays charge, starts dashing for like half a second and stops there (idle and inmediately walk since I am holding a direction). Any attempt after the first time ends with the char charging and stopping there (ends charge and goes back to idle and then walk), theres no visible dash not even half a second like the first time. So what am I doing wrong? :(

Bastard Mami

in some parts, the code overcomplicates becasue of using varadd instead of varset, try using varset to reset values.

Cyanide

Not surprised it's dropping out so fast.

Your triggers for the var reduction are continuous.

State -2 triggers every tick. 60 ticks = 1 second. You have the var set to 10. Each tick that reduces by 1. So within 10 ticks (1/6th of a second) your variable is at or below 0 and your character drops out. Try increasing that value or using game%time# = # to change how often the trigger actually goes off.


In M.U.G.E.N there is no magic button

They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance.

gia

Um no, because for that to happen var(1) should be zero all ticks, but at the same time I deduct 1 from var(0) I add 16 to var(1), at least that is what I intended. The code is not complex at all c'mon it is just two counter variables that modify a third, it's just these quirks documented nowhere, well they must be documented somewhere I suppose but they get lost in one of the many tutorials.

On most examples I v'e seen that you could set two or more things after a trigger check (ie ChangeState value=0 ctrl=1) so I thought I could add two variables in the same check. What I get is that VarAdd will only add to the first variable you put and will drop the rest of lines, or at least that's the only explanation I can come up with. I did that twice thinking having duplicate states for the same "if" logic was dumb, you know:


if( x and ( j or k or l or m) )
{
a+=-1;
b+=16;
};
//better than
if( x and ( j or k or l or m) )
{
a+=-1;
};
if( x and ( j or k or l or m) )
{
b+=16;
};


ie. here I do two varadds in the same state, I do this twice

;reduce dash points if we must
[State -2]
type = VarAdd
trigger1 = var(1) = 0 && var(0) > 0 && ( stateno = 100 || stateno = 101 || stateno = 102 || stateno = 103 ) ;put a list of all dashing states
var(0) = -1
var(1) = 16



Testing...
yup, I separated the varadds and now the counters seem to work, and don't worry the second lines are now varsets. Overcomplicating code is having to type that twice... oh whatever, success, it works! now I have to figure how to draw the meter on screen.

Cyanide

If you want to cut it down a little

stateno = [100,103] rather than your long string.

I didn't really read it thoroughly enough to work out what var(16) did. Would probably be easier just to set var(0) to 160 and have it count down.


In M.U.G.E.N there is no magic button

They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance.

Bastard Mami

the overall effect is a bit different, like comparing ints to floats, so it i nice if he did it on purpose, if not your method sounds better.

gia

While breaking my head to figure how to draw the dash bar properly (it didnt spawn in the correct position, then it didnt animate with the value of the variable, then it didnt follow the camera, then etc)... I found the documentation I wanted on the mugen official docs :/, it's just my mugen didn't have that folder, and the online docs I found before were just the "create your character" part of the docs which I thought was all of it, ironically google sent me to the copy of the docs hosted here :) Add some luck (the "parent," syntax which I haven't seen on the docs and somehow noticed while googling some pages in chinese) and all problems solved.

About the three vars, I'm trying to imitate the basic gameplay of the char, I wanted to be able to start "recovering" at the same time you were using your "dash time". If you cancel your dash you still have 16 frames to start it again without losing any dash point, but if you don't you would recover a point 16 frames after you canceled. Now I think about it it can be worked with a single var and using the % operand almost everywhere, however I might need the vars separated if only for sanity so I'll optimize later.

My current problem is this one:
The bar (has letters on the image) spawns facing correctly wether you are p1 or p2 by using TeamSide, it spawns at the correct x,y place too, regardless of where the floor is located on the stage. And it moves with the camera. My problem is that it lags behind the camera both x and y movement, if it is possible to fix that cool, otherwise I'll live with it.


[State 970, setpos]
type = PosSet
trigger1 = 1
x = ifelse( TeamSide = 1, -130.0, 18.0 )
y = parent,Pos Y - parent,ScreenPos Y + 47
As you can see there, y might be hard to understand (yes it works), but x uses constants so it "shouldn't" lag on the x axis, however it does. I suppose the camera moves before the stage's axes do and that would most likely mean I'm out of luck unless there's a way to get the camera position.


The first one is the base position if you quickly switch the images you can see the bar lags on both x and y when jumping/moving






Cyanide

Doubt it makes a difference but you don't need the decimals. Pos on screen is always an integer. No float required.

y = parent,Pos Y - parent,ScreenPos Y + 47

You don't really want parent involved here do you? Parent would be buster, don't want the helper checking on him, it's going to be a solid screen position. You can calculate through pure

0-screenpos Y+47

It may lag less that way as it doesn't involve a changing Y axis all the time. Go figure, mugen is wierd sometimes give it a go.


In M.U.G.E.N there is no magic button

They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance.

gia

Nope didn't work, the stctrl is PosSet so every two frames it works but the frames in the middle ScreenPos is the constant (47), you negate it and add it = 0 so it ends appearing on the floor level. However you are right I missed I didn't need to call the parent as the helper position is enough: Pos Y - SPos Y + 47, however it lags the same, a pity there's no ScreenPosSet... I'll live with it