Forum

> > CS2D > Scripts > Inserting and Reading from a table
Forums overviewCS2D overview Scripts overviewLog in to reply

English Inserting and Reading from a table

5 replies
To the start Previous 1 Next To the start

old Inserting and Reading from a table

TrialAndError
User Off Offline

Quote
Hello, I have encountered a problem that has bugged me for the past hour.

So I have this
1
2
3
local temp = {} -- I want this to store a temporary storage value

eq = {value=x} -- There x is a number

But before I set it, I want to save it to temp.
1
2
table.insert(temp,eq.value) 
eq.value = y

The problem comes here:
I want to set the original value from temp to eq.value when pressing 2nd Serveraction

This is only example code of what my problem is.

-TheD

old Re: Inserting and Reading from a table

Rainoth
Moderator Off Offline

Quote
So what's your problem?
1
2
3
4
5
6
7
8
9
10
11
temp = {}

addhook("serveraction","_sa")
function _sa(id,b)
	if b == 2 then
		local x = player(id,"x") -- or whatever
		table.insert(temp,x)
		x = x + 50 -- new value or whatever
		print(x) -- new value here
	end
end

Am I missing something here? Either that or you didn't say what your problem was properly. Right now it's something along the lines of "how to save value when pressing 2nd serveraction and then changing it"

old Re: Inserting and Reading from a table

TrialAndError
User Off Offline

Quote
@user Rainoth: I haven't explained it good enough, it happens.

Now I have x and it is 50. So I want to change the x = 20. But before I change it, I want to save the 50 in temp. So the new value would be 20.

But how do I get back the old value (50) from temp and replace it with the 20.

old Re: Inserting and Reading from a table

Rainoth
Moderator Off Offline

Quote
So basically, variable switching? Oh man, it was a task in school I did like 10 times They always ask for that. The most simple way is introducing another variable.

Lets say 'c' is the new variable and 'a' and 'b' are variables you want to switch.

1
2
3
c = a
a = b
b = c
At the beginning (before you assign values) you had a,b,c as:
a b nil (since c wasn't there and a & b had their original values)
After the executed code you have a, b c as:
b, a, a (so a is now b and b is now a)

Sure hope that's the problem you're trying to solve

old Re: Inserting and Reading from a table

VADemon
User Off Offline

Quote
You can switch values in Lua like this:
1
2
3
4
5
x = 5
y = 2
print(x,y)
x, y = y, x
print(x,y)
Same for tables:
1
2
3
4
math.tau = math.pi * 2
print(math.pi, math.tau)
math.pi, math.tau = math.tau, math.pi
print(math.pi, math.tau)
Lua Demo

Your problem with table.insert is that it "inserts" aka sets temp[1] to the value of eq.value not like you expected temp.value = eq.value.
https://www.lua.org/manual/5.1/manual.html#pdf-table.insert
PS: I dislike table.insert. Using the regular built-in syntax is easy and convenient.

old Re: Inserting and Reading from a table

TrialAndError
User Off Offline

Quote
@user Rainoth: and @user VADemon: ty both for your answers, it wasn't really what I had expected but there was another solution, by saving the values before they change value and set them back after.

Beside that, I had already tried those methods before, they kinda worked but not as I wanted them to work.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview