❔ ✅ Access variable from another script
Im trying to make a project in WFA and i want to access a variable from my main form in a diff script. I looked it up and the only stuff i could find was for unity
68 Replies
i know my code is bad but thats not the point i just want to know how to access the stoneUpg variable in the shop script
if you make the
cookie
field static
you can access from everywhere using typename:
or keeping field as it is without static you can access it on an object of type Form1:
i maent stoneUpg not cookie XD
@voflorian99
the same applies either way
either make it static and u can access it via whats the error its giving u there? nvm
u now have mixed it vice versa, u have the static member, and try to access it as instance member
YourClass.NameOfIt
, or u need to have an instance yourInstanceOfYourClass.NameOfIt
Form1.stoneUpg
would be the way to access it as static membercould we get in a vc and you show me
because my brain cant work rn XD
my english skills regarding speaking/listening are really bad i can only read and write some, so voice is a no-no for me
but its literally just
Form1.stoneUpg
instead of form1.stoneUpg
in that case ._.i tried that already
do u know the difference between the
public
, protected
and private
keywords?no 😦
still VERY new to C#
public
= its accessable from other classes
protected
= its accessable from sub classes (inheritence)
private
= noone else touch that!static int stoneUpg
does not have any of these visibility modifiers, which means it defaults to private
wait you can do public and static
i didnt know that
no more errors
yeah, because they have completely differnt meaning. u can combine them ofc
so what dose stiatic do
i didnt know that
do u know the difference between a class and an instance?
no
XD
okay
lemme take a quick ciggy break (5min) while thinking about how to explain it ;p
this was working 4 mins ago 😭
i dont understand
back
so, lets think about cars
Car
is basically our class, its a description of our thing
all cars have in common that they have 4 tires right?yea
that basically means that they are bound to the type, our class
Car
. thats expresses by using the static
keyword:
so staitc values can not be changed?
now cars usually have colors, u have a red car, a blue, a yellow, etc
mhm
so this is something that is not bound to the type (
Car
) itself, but to each individual car
basically an instance of that type
so now, naturally u can access the tire count via its type Car.TireCount
because its bound to the type
but u can not do the same for the color, right?ahhhhhh
i get it
(ofc this is more pseudo code than real code)
so both
redCar
and yellowCar
are instances (also called objects) of the type Car
ok
my tire count thingy was simply a rough explanation. static members can be changed as well
ok
my explanation was only about where that member belongs to,
static
to the type itself, non-static to each instance/objectso Form1 is a instance?
Form1
is the type, not the instance itself
so here stoneUpg
has to be static
or else the compiler would complainok i get it
new problems
this is in Form1.cs
that part gives the reason, if u scroll down a bit
ik but i have no idea how to fix it
like i closed it
that means that another program has that file opened
how do i find out what has it open
because this is the code
oh, its not even another program, but urs itself
?
please explain
well, u open the file with read permissions using the
StreamReader
, afterwards u try to open the file again with write access, this is bound to faili dont understand sorry
u should open it for read and write acces and simply use it based on that (need to dig through documentation quick)
yea i have no clue what your talking about
what im saying is more or less "u cant use it like that"
ok
if u "open" a file u have choices on how to open it depending on ur needs.
mostly its "split" between read access and write access.
imagine 3 people standing around a piece of paper. as long as everyone only wants to read everything is fine
but now think about if all of those 3 want to write their text onto that piece of paper, letter by letter, it will become a mess
so using a stream reader/writer also implies their intent what they want to do with the file and the OS sorta "locks" the usage to just that.
because u want to read in first place (stream reader) its opened as "i only want to read"
but then u open it also for writing, where it then explodes
so i need to move this line
well, basically u cant use neither StreamReader nor StreamWriter like that
u would have to open the stream with the read+write access and then create the reader and writer from that stream
how
please
tell me how i do that
as u want to also write to the file u need write permissions, these usually also imply read permissions, so it would result in
generally u should create this and the reader/writer in the method directly, not as member (especially not as static member)
yea i give up i have no idea what im doing and i have being trying to fix this all day
generally i would say, u should use some simple console applications for now, to test this stuff. GUI is quite complex and to me it feels like u r lacking quite same basics.
u could also for a "quick and dirty" fix use the
void File.WriteAllText(string path, string content)
and string File.ReadAllText(string path)
methods
but the main issue is stil the mentioned lack
@PhoenixXeme use $close to mark this thread as answered 😉Use the
/close
command to mark a forum thread as answeredseems the bot is broken for now, so just ignore it ;p
more or less its already marked as resolved anyway ;p
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.