Adjusting a var via a var...

BlitzMax Forums/BlitzMax Beginners Area/Adjusting a var via a var...

Ryan Burnside(Posted 2006) [#1]
Ok this is a new problem that I have encountered. This is for a slider in a gui type setting. The value "value" should be THE NAME of the variable it adjusts. Simply put I want to adjust a variable via a variable. So this should take the name of the var they player will adjust via the slider bar. So the player could adjust the "lives" variable if it were passed into the slider's value parameter.
Type slider
	Global slider_list:TList=New TList  
	Field x#,y#,width#,height#,Value#,max_value#
	
	Function Create( x#,y#,width#,height#,Value#,max_value#)
		Local temp:slider= New slider
		temp.x=x
		temp.y=y
		temp.width=width
		temp.height=height
		temp.Value=Value
		temp.max_value=max_value
		ListAddLast(slider_list,temp)
	EndFunction
	Method Update()
		' draw the craps
		draw_gradient(200,200,200,255,255,255,x,y,width,height)
		draw_gradient(120,120,120,200,200,200,x,y,width*(Value/max_value),height)
		' update the mouse
		If MouseX()>=x and MouseY()>=y and MouseX()<=x+width and MouseY()<=y+height and MouseDown(1)
			Value=max_value*((MouseX()-x)/width)
		End If
	End Method
End Type



skidracer(Posted 2006) [#2]
something like this?
Strict

Type test
	Field value:Float Ptr
	Function create:test(value# Var)
		Local t:test=New test
		t.value=Varptr value
		Return t
	End Function
	Method Update()
		value[0]:+1
	End Method
End Type

Local f#

Local t:test=test.Create(f)
t.update
DebugLog "f="+f



Ryan Burnside(Posted 2006) [#3]
I think so, what is a Varptr? I assume that it allows indirect manipulation of a var? If so that's what I need. I'm still a bit confused.


skidracer(Posted 2006) [#4]
Varptr gives you the address of the variable which you need so as to store the variable's address in a pointer.

The var in the parameter list means you are storing the address of the variable passed to the function not the address of a copy (without a var it would mean you are talking about a normal parameter variable which is effectively a local variable that will be lost when the function exits).

The value[0] line is how you reference the variable the pointer is pointing at.

Clear as mud...


Ryan Burnside(Posted 2006) [#5]
I don't think i get it just quite yet. Would somebody be able to patch my code up so I can understand it a bit better? The blitzwiki has nothing on the subject.

Type slider
	Global slider_list:TList=New TList  
	Field x#,y#,width#,height#,Value# ,max_value#
	
	Function Create( x#,y#,width#,height#,Value,max_value#)
		Local temp:slider= New slider
		temp.x=x
		temp.y=y
		temp.width=width
		temp.height=height
		temp.Value=VarPtr Value

		temp.max_value=max_value
		ListAddLast(slider_list,temp)
	EndFunction
	Method Update()
		' draw the craps
		draw_gradient(200,200,200,255,255,255,x,y,width,height)
		draw_gradient(120,120,120,200,200,200,x,y,width*(Value/max_value),height)
		' update the mouse
		If MouseX()>=x and MouseY()>=y and MouseX()<=x+width and MouseY()<=y+height and MouseDown(1)
		' adjust the variable in the value param via the mouse position
			Value[0]=max_value*((MouseX()-x)/width)
		End If
	End Method
End Type



Yan(Posted 2006) [#6]
You need to tell Max that 'slider.Value#' is a pointer to a variable...
Field x#, y#, width#, height#, Value# ptr, max_value#

'slider.Create()' needs to have a reference to the original variable that's passed as 'Value#', otherwise it will just create a local copy...
Function Create(x#, y#, width#, height#, Value# Var, max_value#)