C
C#2y ago
kokars

❔ List string variables

Hello, I'm really new to C# and would like to get help. How is the correct way to give variable for each case name? So it would display like - "WOOD x1" "STONE x2" etc... My code is below. This is for Rust game plugin.
private void CreateButtons(BasePlayer player, BaseContainer parent, BuildingPrivlidge tc)
{
// Title
TextContainer.Create(parent, Anchor.TopLeft, new Offset(60f, -50f, 210f, -3f))
.WithSize(15)
.WithText("BASE UPGRADE")
.WithOutline(m_OutlineWhite);


List<string> u_ButtonNames = new List<string> { "WOOD " + "(resource)", "STONE " + "(resource)", "METAL " + "(resource)", "HQM " + "(resource)" };

// Button Container
ImageContainer.Create(parent, Anchor.FullStretch, new Offset(5f, 5f, -5f, -25f))
.WithStyle(m_PanelStyle)
.WithLayoutGroup(u_ButtonLayout, u_ButtonNames, 0, (int i, string t, BaseContainer buttons, Anchor anchor, Offset offset) =>
{
ImageContainer.Create(buttons, anchor, offset)
.WithStyle(m_ButtonStyle)
.WithChildren(header =>
{
TextContainer.Create(header, Anchor.FullStretch, Offset.zero)
.WithText(t)
.WithAlignment(TextAnchor.MiddleCenter);

ButtonContainer.Create(header, Anchor.FullStretch, Offset.zero)
.WithColor(Color.Clear)
.WithCallback(m_CallbackHandler, arg =>
{
switch (t)
{
case "WOOD": rust.RunClientCommand(player, "chat.say", "/up 1"); break;
case "STONE": rust.RunClientCommand(player, "chat.say", "/up 2"); break;
case "METAL": rust.RunClientCommand(player, "chat.say", "/up 3"); break;
case "HQM": rust.RunClientCommand(player, "chat.say", "/up 4"); break;
}
}, $"{player.UserIDString}.{t}");
});
});
}
private void CreateButtons(BasePlayer player, BaseContainer parent, BuildingPrivlidge tc)
{
// Title
TextContainer.Create(parent, Anchor.TopLeft, new Offset(60f, -50f, 210f, -3f))
.WithSize(15)
.WithText("BASE UPGRADE")
.WithOutline(m_OutlineWhite);


List<string> u_ButtonNames = new List<string> { "WOOD " + "(resource)", "STONE " + "(resource)", "METAL " + "(resource)", "HQM " + "(resource)" };

// Button Container
ImageContainer.Create(parent, Anchor.FullStretch, new Offset(5f, 5f, -5f, -25f))
.WithStyle(m_PanelStyle)
.WithLayoutGroup(u_ButtonLayout, u_ButtonNames, 0, (int i, string t, BaseContainer buttons, Anchor anchor, Offset offset) =>
{
ImageContainer.Create(buttons, anchor, offset)
.WithStyle(m_ButtonStyle)
.WithChildren(header =>
{
TextContainer.Create(header, Anchor.FullStretch, Offset.zero)
.WithText(t)
.WithAlignment(TextAnchor.MiddleCenter);

ButtonContainer.Create(header, Anchor.FullStretch, Offset.zero)
.WithColor(Color.Clear)
.WithCallback(m_CallbackHandler, arg =>
{
switch (t)
{
case "WOOD": rust.RunClientCommand(player, "chat.say", "/up 1"); break;
case "STONE": rust.RunClientCommand(player, "chat.say", "/up 2"); break;
case "METAL": rust.RunClientCommand(player, "chat.say", "/up 3"); break;
case "HQM": rust.RunClientCommand(player, "chat.say", "/up 4"); break;
}
}, $"{player.UserIDString}.{t}");
});
});
}
186 Replies
kokars
kokarsOP2y ago
At the moment it just displays resources as text not values. those values are comming from another file.
ero
ero2y ago
i find it hard to understand what you're asking here
TheRanger
TheRanger2y ago
same
kokars
kokarsOP2y ago
This line List<string> u_ButtonNames = new List<string> { "WOOD " + "(resource)", "STONE " + "(resource)", "METAL " + "(resource)", "HQM " + "(resource)" }; WOOD, STONE ... are names for case break How to pass variable in that name?
TheRanger
TheRanger2y ago
what variable?
kokars
kokarsOP2y ago
from another file
ero
ero2y ago
so are you really asking how to read the contents of a file...?
kokars
kokarsOP2y ago
kokars
kokarsOP2y ago
What you mean how to read contents?
ero
ero2y ago
exactly what i said
kokars
kokarsOP2y ago
I want to know how to pass variable in new List
TheRanger
TheRanger2y ago
what does variable mean to you?
kokars
kokarsOP2y ago
changing number
ero
ero2y ago
if you already have the variable, you'd just do $"WOOD x{amount}"
kokars
kokarsOP2y ago
thanks
ero
ero2y ago
which is crazy if that's actually what you wanted to know because you already use interpolated strings here $"{player.UserIDString}.{t}"
kokars
kokarsOP2y ago
I have only been working with C# like 2/3 days. Sorry I'm just a fronend developer Do you have spare time now? I can show other file what I'm trying to get
kokars
kokarsOP2y ago
uMod - Building Grades by Dana
Allows players to easily upgrade or downgrade an entire building
kokars
kokarsOP2y ago
This is game plugin where my button executes this command - rust.RunClientCommand(player, "chat.say", "/up 1"); ... Here is just plugin when user writes in chat like /up 1 /up 2 etc...
TheRanger
TheRanger2y ago
are u trying to display Wood x1 instead of Wood resource here?
kokars
kokarsOP2y ago
I'm trying to display
wood x
wood x
,
stone x
stone x
TheRanger
TheRanger2y ago
so just wood x not Wood x1 ?
kokars
kokarsOP2y ago
yes
kokars
kokarsOP2y ago
kokars
kokarsOP2y ago
From that buildingGrade plugin I get in chat this x value
TheRanger
TheRanger2y ago
where are the names coming from?
kokars
kokarsOP2y ago
But I want to get it on button What names?
TheRanger
TheRanger2y ago
the names that are written in the buttons in ur image
kokars
kokarsOP2y ago
from this?
TheRanger
TheRanger2y ago
from this
kokars
kokarsOP2y ago
names are from here { $"WOOD x{amount}", $"WOOD x{amount}", $"WOOD x{amount}", $"WOOD x{amount}" };
TheRanger
TheRanger2y ago
thats weird, where is STONE, METAL and HQM?
kokars
kokarsOP2y ago
yes
TheRanger
TheRanger2y ago
?
kokars
kokarsOP2y ago
out sorry
kokars
kokarsOP2y ago
kokars
kokarsOP2y ago
this is original
TheRanger
TheRanger2y ago
where is STONE, METAL and HQM?
kokars
kokarsOP2y ago
kokars
kokarsOP2y ago
Sorry I edited with tha x{amount} and didnt change names
TheRanger
TheRanger2y ago
then why is resource written next to WOOD here?
kokars
kokarsOP2y ago
kokars
kokarsOP2y ago
now it's back where it was before
TheRanger
TheRanger2y ago
ok so apply string interpolation on ur list
kokars
kokarsOP2y ago
I execute with button this command - case "WOOD": rust.RunClientCommand(player, "chat.say", "/up 1"); break; for stone, metal and hqm too And I want to display in each button text + value From that command
kokars
kokarsOP2y ago
kokars
kokarsOP2y ago
TheRanger
TheRanger2y ago
List<string> u_ButtonNames = new List<string> { $"WOOD x{woodAmount}", $"STONE x{stoneAmount}", $"METAL x{metalAmount}", $"HQM x{hqmAmount}" };
kokars
kokarsOP2y ago
That "chat.say", "/up 1.2.3.4" executes from this file
kokars
kokarsOP2y ago
uMod - Building Grades by Dana
Allows players to easily upgrade or downgrade an entire building
TheRanger
TheRanger2y ago
what file, this?
{
"Settings": {
"Use Teams": false,
{
"Settings": {
"Use Teams": false,
kokars
kokarsOP2y ago
yes buildingGrade.cs
kokars
kokarsOP2y ago
If I'm correct than "UpgradeNotEnoughItems" is what I need for each value to get
TheRanger
TheRanger2y ago
did you finish naming the buttons correctly first?
kokars
kokarsOP2y ago
What you mean naming correctly? I can rewrite name on buttons
TheRanger
TheRanger2y ago
like naming them as u intended Wood x1
kokars
kokarsOP2y ago
On that button text even could be now wood but just w
TheRanger
TheRanger2y ago
etc...
kokars
kokarsOP2y ago
kokars
kokarsOP2y ago
this is how I need them
kokars
kokarsOP2y ago
kokars
kokarsOP2y ago
that number is chaning depending on base. it could be 1k or 200k
TheRanger
TheRanger2y ago
can u elaborate? what does that even mean
kokars
kokarsOP2y ago
wait
kokars
kokarsOP2y ago
ero
ero2y ago
it's indiscernible what your problem is like do you not know how to display it, or do you not know how to get that number or
kokars
kokarsOP2y ago
I need to display each chaning value for each button from that BuildingGrades.cs Don't know how to get that value from that other file
ero
ero2y ago
what other file
kokars
kokarsOP2y ago
and to display it in my file
ero
ero2y ago
display what in what file? you're displaying it in the game, no? you're not displaying anything in a file
kokars
kokarsOP2y ago
yes sorry not in file
ero
ero2y ago
that umod page has a discord server of its own why not ask there?
kokars
kokarsOP2y ago
Oh there is no help. toxic and asked like week ago another issue none responded like in dev level
TheRanger
TheRanger2y ago
so where is the value 11500 stored?
TheRanger
TheRanger2y ago
that's not what im asking
kokars
kokarsOP2y ago
That value comes from building block if i remove some wall add some wall it will change
TheRanger
TheRanger2y ago
where is that value originally stored, in a file? in the game itself?
kokars
kokarsOP2y ago
yes
TheRanger
TheRanger2y ago
yes... for file.. or yes.. for in the game?
kokars
kokarsOP2y ago
value comes from game but that value in chat comes from that BuildingGrades.cs file
kokars
kokarsOP2y ago
TheRanger
TheRanger2y ago
where in BuildingGrades.cs?
kokars
kokarsOP2y ago
uMod - Building Grades by Dana
Allows players to easily upgrade or downgrade an entire building
kokars
kokarsOP2y ago
Just to show those values can change
ero
ero2y ago
nothing "comes" from a .cs file. it comes from the code that's executed. the code for it just happens to exist in that file
kokars
kokarsOP2y ago
okey sorry
TheRanger
TheRanger2y ago
looks like its coming from the _missingItems field
kokars
kokarsOP2y ago
yes could be
kokars
kokarsOP2y ago
I could just write in my file this line?
cap5lut
cap5lut2y ago
nope generally looking at the api surface of that plugin there isnt really a chance to get that information unless u copy the code over and do it urself
kokars
kokarsOP2y ago
But how can I extuce that chat command then?
cap5lut
cap5lut2y ago
but for that u would need some modifications, and for that u would need a deeper understanding of c# and the rust plugin ecosystem
kokars
kokarsOP2y ago
Yeah that's the problem that rust plugin ecosystem has no docs really like normal examples
cap5lut
cap5lut2y ago
then its mostly try and error and looking at that building grades plugin
kokars
kokarsOP2y ago
that BuildingGrade plugin works my plugin works You think there is no way to get those values from BuildingGrades in to my gui? because I have seen simmular plugins on other servers, but those are custom written too
cap5lut
cap5lut2y ago
if u get the instance of the plugin somehow, u could try with reflection, but that will not be easy either
TheRanger
TheRanger2y ago
with tracking missingItems it looks like originally it constructs the value from constructionGrade.costToBuild where constructionGrade got its reference from buildingBlock.blockDefinition.GetGrade(targetGrade, 0);
kokars
kokarsOP2y ago
yeah that I see but how to get that value in my plugin to display on button?
cap5lut
cap5lut2y ago
basically u would have to write similar methods in ur plugin to get the desired info
kokars
kokarsOP2y ago
That was the question
TheRanger
TheRanger2y ago
keep tracking where the references are coming from there might be a way
kokars
kokarsOP2y ago
That BuildingGrades is reference in my plugin
kokars
kokarsOP2y ago
here is my all code [PluginReference] private readonly Plugin BuildingGrades; it reads references from grade
cap5lut
cap5lut2y ago
yeah but it provides no public access to the info, so either use reflection to get the info the ugly way or reimplement modified versions of their methods to do so
kokars
kokarsOP2y ago
what you mean by ugly way?
cap5lut
cap5lut2y ago
reflection
cap5lut
cap5lut2y ago
Reflection in .NET - .NET Framework
Review reflection in .NET. Get information about loaded assemblies and the types defined within them, such as classes, interfaces, structures, and enumerations.
kokars
kokarsOP2y ago
so basically write new plugin?
cap5lut
cap5lut2y ago
reimplement modified versions of their methods to do so
would be probably faster, cleaner and more important, independent to code changes in their plugin
kokars
kokarsOP2y ago
ahh will think about it because I don't know C#
cap5lut
cap5lut2y ago
basically, in ur plugin, write some methods to get the cost info, u can use their implementation as reference on how to get that info well all of that implies that u need to learn c# first
kokars
kokarsOP2y ago
understand that, but can't figure out how to start that I can't just copy necasery code in my file? like some funcions etc?
cap5lut
cap5lut2y ago
first of all im not sure if their licensing allowes that, second u would still need to know what to copy and to modify to ur needs -> u need to learn c# anyway
kokars
kokarsOP2y ago
ahh i just need to get value from each tier
cap5lut
cap5lut2y ago
oh, another way would probably be to kindly ask the plugin owners if they could provide the means to access that information, up to them if they do or not
kokars
kokarsOP2y ago
what information do you mean?
cap5lut
cap5lut2y ago
tho thats probably the slowest way to get ur stuff done the values u want to display
kokars
kokarsOP2y ago
but if in that plugin already values display in chat What is the problem to get them in my gui?
cap5lut
cap5lut2y ago
that they dont provide a way to publicly access these values they just use them internally
TheRanger
TheRanger2y ago
looks like ur lucky, BuildingGrades is a singleton that can be accessed from its static field
kokars
kokarsOP2y ago
?
TheRanger
TheRanger2y ago
you can just trace where the reference of the missing items message is coming from
cap5lut
cap5lut2y ago
they have a reference to their plguin, just not the underlying stuff is accessible
kokars
kokarsOP2y ago
kokars
kokarsOP2y ago
I see it from here i guess
TheRanger
TheRanger2y ago
something like BuildingGrades.Instance.Assistant.AllBuildingItems since those properties dont exist, you have to create them to refer to their field
kokars
kokarsOP2y ago
in my plugin?
TheRanger
TheRanger2y ago
tho i cant guarantee it will work the plugin clears the allbuildingItems list at some point
kokars
kokarsOP2y ago
Basically i need private IEnumerator UpgradeBuildingBlocks(BasePlayer player, BuildingGrade.Enum targetGrade, PermissionSettings permissionSettings, bool isAdmin) to work in my plugin
TheRanger
TheRanger2y ago
what does it do? can u even modify BuildingGrades.cs ?
cap5lut
cap5lut2y ago
as i understood its not kokars plugin, kokars wants to write one on top of it, right?
kokars
kokarsOP2y ago
yes i can well I just need it in GUI My buttons already are working with those chat commands from BuildingGrades
TheRanger
TheRanger2y ago
i gave you this hint, go try it
kokars
kokarsOP2y ago
like in my code or in that building grades?
TheRanger
TheRanger2y ago
what do u think?
kokars
kokarsOP2y ago
in my
TheRanger
TheRanger2y ago
you want to get the value and store it in your GUI button, right?
kokars
kokarsOP2y ago
yeah
TheRanger
TheRanger2y ago
it sounds like you didnt get my hint
kokars
kokarsOP2y ago
sorry I'm not backend developer
TheRanger
TheRanger2y ago
i wont write code for you
kokars
kokarsOP2y ago
Can't figure it out yet
kokars
kokarsOP2y ago
something like this?
TheRanger
TheRanger2y ago
nope
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger2y ago
you are suppose to create a static property called Instance in your BuildingGrades class that gets the reference from the field _instance;
kokars
kokarsOP2y ago
in BuildingGrade.cs
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
kokars
kokarsOP2y ago
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger2y ago
u posted ur GUI code somewhere, right?
kokars
kokarsOP2y ago
yes here again
TheRanger
TheRanger2y ago
i wonder if you can get the value through the arguments parentor tc in the method
kokars
kokarsOP2y ago
tc parent is for ui
TheRanger
TheRanger2y ago
can you show the file BuildingPrivlidge.cs ?
TheRanger
TheRanger2y ago
wrong file
kokars
kokarsOP2y ago
?
TheRanger
TheRanger2y ago
BuildingPrivlidge.cs not BuildingGrades.cs
kokars
kokarsOP2y ago
sorry no it is buildinggrades
TheRanger
TheRanger2y ago
where? i cant find it
kokars
kokarsOP2y ago
here
kokars
kokarsOP2y ago
TheRanger
TheRanger2y ago
yes where in the file? which line? i cant find class BuildingPrivlidge
kokars
kokarsOP2y ago
? ou that is from oxide it self
TheRanger
TheRanger2y ago
so its not possible to view that file?
kokars
kokarsOP2y ago
BuildingPrivlidge tc - is needed to access in game toolcupboard
kokars
kokarsOP2y ago
you mean like this?
TheRanger
TheRanger2y ago
what are u trying to show me can u ctrl + left click BuildingPrivlidge ?
kokars
kokarsOP2y ago
next?
TheRanger
TheRanger2y ago
?
kokars
kokarsOP2y ago
I can't open it but "BuildingPrivlidge tc" is only needed to shop gui interface when in game cupboard is opened and that's it for that is no other purpose
TheRanger
TheRanger2y ago
then how would it know which building you are trying to build/upgrade?
kokars
kokarsOP2y ago
When cupboard is placed it automatically detects building BuildingPrivlidge tc = is when player is authorized to toolcupboard
ero
ero2y ago
what a hugely embarrassing typo
TheRanger
TheRanger2y ago
well yes, that class must have some references to those missing blocks any chance tc has a property called allowedConstructionItems ?
kokars
kokarsOP2y ago
that i don't know but will try to find out
kokars
kokarsOP2y ago
Did it
TheRanger
TheRanger2y ago
u figured out how to get the references from tc?
kokars
kokarsOP2y ago
Will show in a while Not at pc right now
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server