GLBasic forum

Other languages => GLBasic - de => Topic started by: WPShadow on 2008-Aug-07

Title: Static initialisieren
Post by: WPShadow on 2008-Aug-07
Hi!

Stehe mal wieder mit meinem Latein vor einer Mauer:

Ich möchte in einer Funktion einen Static - Wert quasi als Default initialisieren, aber des weiteren umändern. Dabei wird die Funktion immer wieder aufgerufen. Hat da jemand eine Idee für mich?

Per Code sieht das inetwa so aus:

Code (glbasic) Select


WHILE TRUE
    bla()
WEND

FUNCTION bla:
STATIC hui

    hui = Veränderung

ENDFUNCTION



Oder zumindest so ähnlich!

GLB

W.
Title: Re: Static initialisieren
Post by: D2O on 2008-Aug-07
Hmm, ich hoffe ich hab das verstanden.


Code (glbasic) Select
WHILE TRUE
    bla(0)
WEND




FUNCTION bla:pos = 0
STATIC hui AS m


  RETURN hui.a[pos] = veraenderung

ENDFUNCTION

TYPE m
a[10]
ENDTYPE
Title: Re: Static initialisieren
Post by: Moru on 2008-Aug-07
If you want to initialize the value on first run, make a new static that you never change in the code so when the routine starts you can be sure that the value will be 0 on first run. Then init the variable you want and then set first_time to 1 or whatever.

Code (glbasic) Select

WHILE TRUE
    bla()
WEND

FUNCTION bla:
    STATIC hui, first_time

    if first_time = 0
        hui = 42 // init
        first_time = 1
    endif

    hui = Veränderung

ENDFUNCTION
Title: Re: Static initialisieren
Post by: WPShadow on 2008-Aug-07
Great! Thank you!  =D
Title: Re: Static initialisieren
Post by: Kitty Hello on 2008-Aug-07
If you assign something to a static, then the first time it is used this value gets assigned:

Code (glbasic) Select

poo()
poo()
poo()

FUNCTION poo:
STATIC c = 5
   DEBUG c + "\n"
   INC c, 1
ENDFUNCTION


Output:
5
6
7


Title: Re: Static initialisieren
Post by: Moru on 2008-Aug-07
this only works if it is a number or string, cant init with a return value from a function or similar. So I preffer to be safe, when I was using static init the other way my program would work and then stop working on the next update :-)