+ Reply to Thread
Results 1 to 6 of 6

Thread: Small Increment fix/help

  1. #1
    Wire Noob Karroon's Avatar
    Join Date
    May 2010
    Posts
    2

    Default Small Increment fix/help

    I'm trying to make an increment button that will have a clk and increment an inputted variable by another user defined variable.

    If anyone can help me with my crappy gate code (I've never used LUA before and i know this is VERY incorrect) or can help me make a gate system to do this, I'd appreciate that very much.

    Code:
    GateActions["increment"] = {
    	group = "Arithmetic",
    	name = "Increment",
    	inputs = { "A", "B", "Clk"},
    	output = function(gate, A)
    		local clk = ( Clk > 0 )
    		
    
    			if ( clk ) then
    				
    					A = B + A
    					return A
    			end	
    
    
    		
    	end,
    	label = function(Out, A)
    		return "A += " .. B .. " = " .. Out
    	end
    }


    Sorry if this is in the wrong thread, I'm new to the site
    PS: this is not E2

  2. #2
    Ursus maritimus Drunkie's Avatar
    Join Date
    Feb 2009
    Location
    Canada
    Posts
    5,662
    Blog Entries
    1

    Default Re: Small Increment fix/help

    Moved to the appropriate forum.

  3. #3
    Wire Sofaking Grocel's Avatar
    Join Date
    Mar 2008
    Location
    Germany, NRW, Remscheid
    Posts
    751

    Default Re: Small Increment fix/help

    Quote Originally Posted by Karroon View Post
    I'm trying to make an increment button that will have a clk and increment an inputted variable by another user defined variable.

    If anyone can help me with my crappy gate code (I've never used LUA before and i know this is VERY incorrect) or can help me make a gate system to do this, I'd appreciate that very much.

    Code:
    GateActions["increment"] = {
    	group = "Arithmetic",
    	name = "Increment",
    	inputs = { "A", "B", "Clk"},
    	output = function(gate, A)
    		local clk = ( Clk > 0 )
    		
    
    			if ( clk ) then
    				
    					A = B + A
    					return A
    			end	
    
    
    		
    	end,
    	label = function(Out, A)
    		return "A += " .. B .. " = " .. Out
    	end
    }


    Sorry if this is in the wrong thread, I'm new to the site
    PS: this is not E2
    I seen no mistakes in it, but isn't it already there?
    Quote Originally Posted by Wizard of Ass View Post
    The secret phrase in gmod is: Rusty bullet hole
    Im a molecule!

  4. #4
    Master of Mars Magos Mechanicus's Avatar
    Join Date
    May 2008
    Posts
    852

    Default Re: Small Increment fix/help

    The problem is that A is an input. Changing A in this execution doesn't mean it will be different in the next execution, since its value is retrieved from the input each time. You could do it with two clock inputs - one for reading from input A into gate memory and one for adding input B to the value stored in the gate memory.
    I can wire anything directly into anything! I'm the Professor!
    -Professor Hubert Farnsworth

  5. #5
    Wire Noob Karroon's Avatar
    Join Date
    May 2010
    Posts
    2

    Default Re: Small Increment fix/help

    Quote Originally Posted by Grocel View Post
    I seen no mistakes in it, but isn't it already there?
    There is, but its a +1, and what I am shooting for is a gate that will read a value and add/increment it by a static amount when it is pinged(or really allowed) by a clk.

    Quote Originally Posted by Magos Mechanicus View Post
    The problem is that A is an input. Changing A in this execution doesn't mean it will be different in the next execution, since its value is retrieved from the input each time. You could do it with two clock inputs - one for reading from input A into gate memory and one for adding input B to the value stored in the gate memory.
    I'm very bad with LUA, as I'm used to a modified C# (i believe it was). Perchance you could script/show me what you are explaining?

    Drunkie: Moved to the appropriate forum.
    Sorry about that, thanks for the move.

  6. #6
    Master of Mars Magos Mechanicus's Avatar
    Join Date
    May 2008
    Posts
    852

    Default Re: Small Increment fix/help

    What I mean is something like this, I guess:
    Code:
    GateActions["increment"] = {
    	group = "Arithmetic",
    	name = "Increment",
    	inputs = { "A", "Clk", "Reset", "Resetvalue" },
    	output = function(gate, A, Clk, Reset, Resetvalue)
    		local clk = ( Clk > 0 )
    		local reset = ( Reset > 0 )
    		
    		if ( gate.PrevValue ~= clk ) then
    			gate.PrevValue = clk
    			if ( clk ) then
    				if ( gate.Memory == nil ) then
    					gate.Memory = A
    				else
    					gate.Memory = gate.Memory + A
    				end
    			end
    		end
    		
    		if( gate.PrevReset ~= reset ) then
    			gate.PrevReset = reset
    			if ( reset ) then
    				gate.Memory = Resetvalue
    			end
    		end
    		
    		return gate.Memory
    	end,
    	label = function(Out, A)
    		return "LastNum += " .. A .. " = " .. Out
    	end
    }
    It's just the current increment gate with a Resetvalue input added, and it resets gate.Memory to Resetvalue instead of 0 when you trigger Reset. The important thing here, though, is that you use a value that is saved in the gate (i.e. gate.Memory). You were using the variable A, which being an input is not saved in the gate but retrieved from inputs every execution.

    I'm not sure if I see this making any real change though. Both the Increment gate and the up/down counter have an input for how long they step, and if you want to add a constant or something you can always add the incrementer to the other thing with an add gate.
    I can wire anything directly into anything! I'm the Professor!
    -Professor Hubert Farnsworth

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
proceed-collector
proceed-collector
proceed-collector
proceed-collector
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots