September 25, 2018, 03:04 am

Author Topic: [REQUEST] Index Table (somthin like that)  (Read 330 times)

Offline lucaslulu2012

  • ByB Newbie
  • *
  • Posts: 5
    • View Profile
[REQUEST] Index Table (somthin like that)
« on: August 11, 2018, 07:16 pm »
Im making an e2 EGP for an auto gunshop got the ui figured out, im just trying to learn how to switch between guns on the same EGP, right now i have physical buttons doing it for simplicity, if you could teach me / add to my code to make an array switch between each Gun gunprice and desciption, also if you feel so inclined to, you could make it so the Previous and next buttons work aswell, im still learning so dont go to harsh on me
Code: [Select]
@name EGP GunStore EGP
@inputs EGP:wirelink Reset Inc Dec
@outputs
@persist Index Description:array Gun:array GunPrice:array
@trigger Reset Inc Dec

Profit = 1000
Gun = array("AK-47", "AI AW50", "M24", "ACR")
GunPrice = array(2500, 3000, 2550, 2500)
Description = array("The AK-47 is a Semi-Automatic Rifle, that does a moderate ammount of damage, Popular among thiefs, this gun is cheap and reliable.", "The AI AW50 is a heavy duty Sniper Rifle, Doing a ton of damage, this Rifle is extremely popular among snipers", "the less powerfull m24 does less damage, can pentetrate less, but is faster at getting a shot off, and reloading, this rifle is popular among hitman", "Almost the same as an ak-47 lower damage but higher firerate.")
Credits = "Pyro"
Index=1
Total=(GunPrice[Index, string]:toNumber()+Profit)

#Alignment
EGP:egpRoundedBox(1000,vec2(256,256),vec2(50,50))
#BackGround
EGP:egpRoundedBox(1,vec2(256,256),vec2(480,480))
    EGP:egpColor(1,vec(0,255,255))
    EGP:egpRoundedBoxOutline(2,vec2(256,256),vec2(480,480)) #Outline
        EGP:egpColor(2,vec(90,0,0))
#header 
EGP:egpRoundedBox(3,vec2(0,-200),vec2(480,80))
    EGP:egpParent(3,1)
    EGP:egpAlign(3,0,0)
    EGP:egpColor(3,vec(90,0,0))
    EGP:egpText(4,Gun[Index, string],vec2(0,0)) #Header text
        EGP:egpParent(4,3)
        EGP:egpSize(4,70)
        EGP:egpAlign(4,1,1)
        EGP:egpFont(4, "Coolvetica")
        EGP:egpColor(4, vec(255, 255, 255))
#NextBox
EGP:egpRoundedBox(5,vec2(120,-130),vec2(230,50))
    EGP:egpParent(5,1)
    EGP:egpAlign(5,1,1)
    EGP:egpColor(5,vec(120,0,0))
    EGP:egpText(6,"Next",vec2(0,0)) #Text For Next Box
        EGP:egpParent(6,5)
        EGP:egpSize(6,45)
        EGP:egpAlign(6,1,1)
        EGP:egpFont(6, "Coolvetica")
        EGP:egpColor(6,vec(255,255,255))
#PreviousBox
EGP:egpRoundedBox(7,vec2(-120,-130),vec2(230,50))
    EGP:egpParent(7,1)
    EGP:egpAlign(7,1,1)
    EGP:egpColor(7,vec(120,0,0))
    EGP:egpText(8,"Previous",vec2(0,0)) #Text For Previous Box
        EGP:egpParent(8,7)
        EGP:egpSize(8,45)
        EGP:egpAlign(8,1,1)
        EGP:egpFont(8, "Coolvetica")
        EGP:egpColor(8,vec(255,255,255))
#Description Box
EGP:egpRoundedBox(9,vec2(-120,70),vec2(230,330))
    EGP:egpParent(9,1)
    EGP:egpAlign(9,1,1)
    EGP:egpColor(9,vec(120,0,0))
    EGP:egpText(10,"Description :",vec2(0,-145)) #Text For Description Box
        EGP:egpParent(10,9)
        EGP:egpSize(10,40)
        EGP:egpAlign(10,1,1)
        EGP:egpFont(10,"Coolvetica")
        EGP:egpColor(10,vec(255,255,255))
#Price Box
EGP:egpRoundedBox(11,vec2(120,-55),vec2(230,80))
    EGP:egpParent(11, 1)
    EGP:egpAlign(11,1,1)
    EGP:egpColor(11,vec(120,0,0))
    EGP:egpText(12,"Price:",vec2(0,-15)) #PriceWord
        EGP:egpParent(12,11)
        EGP:egpSize(12,40)
        EGP:egpAlign(12,1,1)
        EGP:egpColor(12,vec(255,255,255))
        EGP:egpFont(12,"Coolvetica")
        EGP:egpText(13,Total:toString(),vec2(0,15)) #Actual Total
            EGP:egpParent(13,11)
            EGP:egpSize(13,40)
            EGP:egpAlign(13,1,1)
            EGP:egpColor(13,vec(255,255,255))
            EGP:egpFont(13,"Coolvetica")
#CreditBox
EGP:egpRoundedBox(14,vec2(120,40),vec2(230,100))
    EGP:egpParent(14,1)
    EGP:egpAlign(14,1,1)
    EGP:egpColor(14,vec(120,0,0))
    EGP:egpText(15,"Credits:",vec2(0,-30)) #CreditWord
        EGP:egpParent(15,14)
        EGP:egpSize(15,40)
        EGP:egpAlign(15,1,1)
        EGP:egpColor(15,vec(255,255,255))
        EGP:egpFont(15,"Coolvetica")
        EGP:egpText(16,Credits,vec2(0,20)) #Credits
            EGP:egpParent(16,14)
            EGP:egpSize(16,39)
            EGP:egpAlign(16,1,1)
            EGP:egpColor(16,vec(255,255,255))
            EGP:egpFont(16,"Coolvetica")


#reset
if(Reset==1){
    EGP:egpClear()
    Index = 1
}

if(Inc){
    Index += 1
}
I tried but it dosent work and it f'ed up my gun prices to, if you could make this work that would be great!

Offline Scriptis

  • ByB Rookie
  • **
  • Posts: 69
    • View Profile
Re: [REQUEST] Index Table (somthin like that)
« Reply #1 on: August 13, 2018, 03:12 am »
Using a set of an arrays like that is a programming antipattern. It's something you should avoid doing. I did it for ages, and once I figured out a better way, I immediately stopped using it.

I'm not demanding that you stop doing it, but here's something to consider:

Code: [Select]
@name Gun Store
@inputs Reset Up Down
@persist Index
@persist Shipments:table Profit

if (first()) {
    Profit = 1000

    Shipments = table(
        table(
            "Name" = "AK-47",
            "Price" = 2500,
            "Description" = "The AK-47 is a Semi-Automatic Rifle, that does a moderate ammount of damage, Popular among thiefs, this gun is cheap and reliable."
        ),

        table(
            "Name" = "AI AW50",
            "Price" = 3000,
            "Description" = "The AI AW50 is a heavy duty Sniper Rifle, Doing a ton of damage, this Rifle is extremely popular among snipers"
        ),

        table(
            "Name" = "M24",
            "Price" = 2550,
            "Description" = "the less powerfull m24 does less damage, can pentetrate less, but is faster at getting a shot off, and reloading, this rifle is popular among hitman"
        ),

        table(
            "Name" = "ACR",
            "Price" = 2500,
            "Description" = "Almost the same as an ak-47 lower damage but higher firerate"
        )
    )
}

if (~Up && Up) {
    # Index must not go above the number of shipments we have
    Index = min(Index + 1, Shipments:count())
}

if (~Down && Down) {
    # Nor below zero
    Index = max(Index - 1, 0)
}

if (~Reset && Reset) {
    Index = 0
}

local Gun = Shipments[Index, table]

# Debug code
printTable(Gun)

# Name = Gun["Name", string]
# Description = Gun["Description", string]
# Price = Gun["Price", number] + Profit

# EGP rendering nonsense goes here

Some notes here:
  • if (~Up && Up) { ... } - If up triggered the current execution (i.e., changed value) and is true (i.e., is pressed/active)
  • local - Completely optional in this example, but useful when you're building something crazy. Prevents variables from overwriting anything in @persist and introduces some scoping; especially helpful in UDFs, which we'll see in a bit.
  • Shipments[Index, table] - Table accessor. You can treat this like a regular variable, and even assign values to it with =. Index can be a number or a string, and table is the type you're retrieving from (or setting to) the table. Tables can contain contain anything, even more tables!

As for detecting if a button has been pressed, wire EGPs have an output called User:entity. With a bit of math and some trickery, we get this:
Code: [Select]
@name EGP Button Demo
@inputs Scr:wirelink
@persist User:entity

# The trick here is to wire this to the EGP's User:entity,
# so that the E2 runs and this is set whenever the screen
# is actually used (i.e., somebody presses E on it)

@inputs UserTrigger:entity

interval(100)

if (first()) {
    # User-defined function (UDF) to determine if a point is in
    # an EGP object (specifically, a rectangle around it)
    # if it looks scary, remember that it isn't--it creates a function less complicated
    # than egpBox :)

    function number wirelink:egpHover(Index:number, User:entity) {
        local Center = This:egpPos(Index)
        local Radius = This:egpSize(Index) / 2

        # Return 1 (true) if...

        return inrange(
            # The user's cursor is in range...
            This:egpCursor(User),

            # ...of the top left of the object...
            Center - Radius,

            # ...and the bottom right.
            Center + Radius
        )
    }

    # Little debug box

    Scr:egpBox(1, vec2(256), vec2(32))
}

if (findCanQuery()) {
    # Set User to the nearest player.
    findByClass("player")
    User = findClosest(Scr:entity():pos())
}

# Make the box darker if the user is hovering over it.

Scr:egpColor(1, vec(Scr:egpHover(1, User) ? 172 : 255))

# Print a message if the user presses the button.

if (Scr:egpHover(1, User) && UserTrigger == User) {
    print("Button pressed!")
}

Which should get you the rest of the way. If you don't get how all of it works yet, that's alright, just keep fiddling with it until you do. The Wiremod discord is a great resource!

A good place to go from here is to swap out the @inputs in the first E2 I've listed with the EGP buttons of the second. It'll take some doing, but it shouldn't be too hard. That should solve what you were aiming for in your post, good luck!